1,10 → 1,10 |
// ************************************************************ |
// ** Spektrum Diversity - use up to 8 satellite receivers ** |
// ** Spektrum Diversity - use up to 4 satellite receivers ** |
// ************************************************************ |
// ** Target: An Atmel ATtiny2313 (RC-Oscillator @ 8 MHz) ** |
// ** controls a 74HC151 Multiplexer ** |
// ************************************************************ |
// ** It monitors the data from 8 satellite receivers and ** |
// ** It monitors the data from 4 satellite receivers and ** |
// ** connects a valid one to the output via the multiplexer ** |
// ************************************************************ |
// ** LED-Modes during startup (in chronological order) ** |
26,39 → 26,15 |
#include <avr/interrupt.h> |
#include <util/delay.h> |
|
#define LED_OFF PORTD |= 1 << PORTD3 |
#define LED_ON PORTD &= ~(1 << PORTD3) |
#define LED_OFF PORTD |= 1 << PORTD0 |
#define LED_ON PORTD &= ~(1 << PORTD0) |
|
|
volatile unsigned int TimerEvent; |
|
|
void SelectSat(unsigned char sat) |
{ |
/* |
Re-Assignment due to easier routing on layout: sat -> mux-channel# -> mux-input (seen from µC) |
0 -> 2 -> 2 |
1 -> 1 -> 4 |
2 -> 0 -> 0 |
3 -> 3 -> 6 |
4 -> 4 -> 1 |
5 -> 5 -> 5 |
6 -> 6 -> 3 |
7 -> 7 -> 7 |
*/ |
|
|
if (sat == 0) sat = 2; |
else if (sat == 1) sat = 4; |
else if (sat == 2) sat = 0; |
else if (sat == 3) sat = 6; |
else if (sat == 4) sat = 1; |
else if (sat == 5) sat = 5; |
else if (sat == 6) sat = 3; |
else if (sat == 7) sat = 7; |
|
|
PORTD = (PORTD & 0b00001000) | sat; |
PORTD = (PORTD & 0b00000001) | (sat<<3); // Select the Input for 74HC151 |
} |
|
void ResetTimer(void) |
69,9 → 45,58 |
} |
|
|
void Binding(void) // Binding sequence for DX7 |
{ |
unsigned char i = 0; |
|
ISR(TIMER1_OVF_vect) // Triggered every 4,096 msec |
DDRB = 0b11110000; // Sat-Pins to output |
_delay_ms(80); // Let them boot up |
|
for (i=0;i<3;i++) // send three negative 100µs pulses to all sat's |
{ |
PORTB = 0b00000000; |
_delay_us(100); |
PORTB = 0b11110000; |
_delay_us(100); |
} |
|
_delay_ms(1000); |
DDRB = 0b00000000; // Sat-Pins to input after 1sec |
} |
|
|
void Testing(void) // Self Test |
{ |
unsigned char i = 0; |
unsigned char error = 0; |
|
DDRB = 0b11110000; // PORT B OUTPUT FOR SATELLITES, INPUT FOR FEEDBACK |
PORTB = 0b00000000; |
|
for(i=0;i<4;i++) // Test muxxing of High+Low on every Sat-Input |
{ |
SelectSat(i); |
PORTB |= 1 << (i+4); |
if (!(PINB & (1<<PB3))) error++; |
PORTB &= ~(1 << (i+4)); |
if (PINB & (1<<PB3)) error++; |
} |
|
while(1) // Never return |
{ |
if (error == 0) // When no error occured, flash around |
{ |
LED_ON; |
_delay_ms(100); |
LED_OFF; |
_delay_ms(100); |
} |
} |
} |
|
|
ISR(TIMER1_OVF_vect) // Triggered every 8,192 msec |
{ |
TimerEvent++; |
} |
|
79,22 → 104,26 |
int main(void) |
{ |
unsigned char i = 0; |
unsigned int j = 0; |
unsigned char active[8]; |
unsigned char sat = 9; |
DDRB = 0b00000000; // PORT A INPUT FOR SATELLITES |
DDRD = 0b00001111; // PORT B OUTPUT FOR MUX AND LED |
PORTB = 0b11111111; // PORT A PULLUP's FOR (unused) SATELLITES |
unsigned char j = 0; |
unsigned char active[4]; |
unsigned char sat = 4; |
|
DDRB = 0b00000000; // PORT B INPUT FOR SATELLITES AND FEEDBACK |
DDRD = 0b0011001; // PORT D OUTPUT FOR MUX AND LED, INPUT FOR SWITCH & TEST |
PORTB = 0b11110000; // PORT B PULLUP's FOR (unused) SATELLITES |
PORTD = 0b1100001; // PORT D PULLUP's FOR SWITCH & TEST |
|
TCCR1B = ( 1 << CS10 ); // Timer1 Prescaler 1 = 4,096 msec |
for (i=0;i<4;i++) active[i] = 0; // Reset active-array |
|
if (!(PIND & (1<<PD6))) Binding(); // Initiate Binding when Bind-Button is pressed |
if (!(PIND & (1<<PD5))) Testing(); // Initiate Self-Test when Test-Pad is low |
|
TCCR1B = ( 1 << CS10 ); // Timer1 Prescaler = 1 -> 8,192 msec |
TIMSK = ( 1 << TOIE1 ); // Timer1 Overflow Interrupt Enable |
sei(); // Global Interrupts enable |
|
for (i=0;i<8;i++) active[i] = 0; // Reset active-array |
|
ResetTimer(); |
while(PINB == 255) // Wait for first signal (SYNC to frame) |
while((PINB & 0b11110000) == 0b11110000) // Wait for first signal (SYNC to frame) |
{ |
if (TimerEvent == 10) LED_ON; // Blink while waiting... |
if (TimerEvent == 20) |
117,12 → 146,12 |
j++; |
} |
|
for (i=0;i<8;i++) |
for (i=0;i<4;i++) |
{ |
if (!(PINB & (1<<i))) |
if (!(PINB & (1<<(i+4)))) |
{ |
active[i] = 1; |
if (sat == 9) // Select first active satellite (only once) |
if (sat == 4) // Select first active satellite (only once) |
{ |
sat = i; |
SelectSat(i); |
137,12 → 166,12 |
LED_OFF; |
_delay_ms(1000); |
|
for (i=0;i<8;i++) // Flash once for every active satellite |
for (i=0;i<4;i++) // Flash once for every active satellite |
{ |
if (active[i] == 1) |
{ |
LED_ON; |
_delay_ms(200); |
_delay_ms(100); |
LED_OFF; |
_delay_ms(200); |
} |
155,20 → 184,20 |
{ |
ResetTimer(); |
|
while(PINB == 255) // Wait for first signal (SYNC to frame) |
while((PINB & 0b11110000) == 0b11110000) // Wait for first signal (SYNC to frame) |
{ |
if (TimerEvent > 3) break; // (max. 3*8=24ms) |
} |
|
for (i=0;i<8;i++) active[i] = 0; // Reset active-array |
for (i=0;i<4;i++) active[i] = 0; // Reset active-array |
ResetTimer(); |
|
while(TimerEvent < 1) // Check active satellites (for 1*8=8ms) |
{ |
|
for (i=0;i<8;i++) |
for (i=0;i<4;i++) |
{ |
if (!(PINB & (1<<i))) |
if (!(PINB & (1<<(i+4)))) |
{ |
active[i] = 1; |
} |
179,7 → 208,7 |
if (active[sat] == 0) // Detect fail on active satellite |
{ |
LED_ON; // Failure-LED ON (never goes off again) |
for (i=0;i<8;i++) // Select lowest active satellite |
for (i=0;i<4;i++) // Select lowest active satellite |
{ |
if (active[i] == 1) |
{ |
189,5 → 218,9 |
} |
} |
} |
|
|
} |
|
} |
|