Subversion Repositories Projects

Compare Revisions

Ignore whitespace Rev 791 → Rev 931

/Spektrum-Diversity/Software/diversity.c
1,5 → 1,5
// ***************************************************************
// ** Spektrum Diversity v1.3 - use up to 4 satellite receivers **
// ** Spektrum Diversity v2.0 - use up to 4 satellite receivers **
// ***************************************************************
// ** Target: An Atmel ATtiny2313 (RC-Oscillator @ 8 MHz) **
// ** controls a 74HC151 Multiplexer **
9,12 → 9,12
// ***************************************************************
// ** LED-Modes during startup **
// ** ************************ **
// ** LED fast flash: Waiting for first datapulse **
// ** LED fast flash: Waiting for first signal **
// ** **
// ** LED-Modes during operation **
// ** ************************** **
// ** LED OFF: Everything is fine **
// ** LED ON: FAILURE - The first selected sat had lost sync **
// ** LED flash 1x - 4x: Shows which channel is active every 2s **
// ** LED ON after flash: FAILURE - A used signal was lost **
// ** **
// ** LED-Modes after Self-Test and pressed button **
// ** ******************************************** **
35,7 → 35,10
#define LED_OFF PORTD |= 1 << PD0
#define LED_ON PORTD &= ~(1 << PD0)
 
volatile unsigned int TimerEvent;
volatile unsigned char Timer0Event = 0;
volatile unsigned int Timer1Event;
volatile unsigned char BlinkCode = 0;
volatile unsigned char failure = 0;
 
unsigned char eedummy EEMEM; // Dummy-Variable for Address 0 in EEPROM
unsigned char eecheck EEMEM; // Check-Variable
44,13 → 47,35
 
 
 
ISR(TIMER0_OVF_vect) // Triggered every 32,768 msec
{
if (Timer0Event > 64) // Max. 64*32 msec = 2,097152 sec
{
Timer0Event = 0;
LED_OFF;
}
if(Timer0Event == 0 && BlinkCode > 0) LED_ON;
else if(Timer0Event == 4 && BlinkCode > 0) LED_OFF;
else if(Timer0Event == 8 && BlinkCode > 1) LED_ON;
else if(Timer0Event == 12 && BlinkCode > 1) LED_OFF;
else if(Timer0Event == 16 && BlinkCode > 2) LED_ON;
else if(Timer0Event == 20 && BlinkCode > 2) LED_OFF;
else if(Timer0Event == 24 && BlinkCode > 3) LED_ON;
else if(Timer0Event == 28 && BlinkCode > 3) LED_OFF;
else if(Timer0Event == 35 && failure == 1) LED_ON;
else if(Timer0Event == 55 && failure == 1) LED_OFF;
 
Timer0Event++;
}
 
 
ISR(TIMER1_OVF_vect) // Triggered every 8,192 msec
{
TimerEvent++;
Timer1Event++;
}
 
 
 
void SelectSat(unsigned char sat)
{
PORTD = (PORTD & 0b1100111) | (sat << 3); // Select the input for 74HC151
59,16 → 84,16
 
 
 
void ResetTimer(void)
void ResetTimer1(void)
{
TCNT1H = 0;
TCNT1L = 0;
TimerEvent = 0;
Timer1Event = 0;
}
 
 
 
void Binding(void) // Binding sequence
void Binding(void) // Binding sequence (for Spektrum sats only)
{
unsigned char i = 0;
 
163,16 → 188,16
{
unsigned char i = 0;
unsigned char active[4];
unsigned char active_lo[4];
unsigned char active_hi[4];
unsigned char sat = 99;
 
 
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, LED off
 
for (i=0;i<4;i++) active[i] = 0; // Reset active-array
 
 
if (eeprom_read_byte(&eecheck) != 0x42) // Invalid Data in EEPROM -> initialize
{
eeprom_write_byte(&eecheck, 0x42);
185,73 → 210,111
if (!(PIND & (1<<PD5))) Testing(); // Initiate Self-Test when Test-Pad is low
if (!(PIND & (1<<PD6))) Binding(); // Initiate Binding when Bind-Button is pressed
 
 
for (i=0;i<4;i++) // Reset active-arrays
{
active[i] = 0;
active_lo[i] = 0;
active_hi[i] = 0;
}
 
_delay_ms(100);
 
TCCR0B = ( 1 << CS00 ) | ( 1 << CS02 ); // Timer0 Prescaler = 1024 -> 32,768 msec
TCCR1B = ( 1 << CS10 ); // Timer1 Prescaler = 1 -> 8,192 msec
TIMSK = ( 1 << TOIE1 ); // Timer1 Overflow Interrupt Enable
TIMSK = ( 1 << TOIE0 ) | ( 1 << TOIE1 ); // Timer0+1 Overflow Interrupt Enable
sei(); // Global Interrupts enable
 
ResetTimer();
ResetTimer1();
while(sat == 99) // Wait for first signal
{
if (TimerEvent == 10) LED_ON; // Blink while waiting...
if (TimerEvent == 20)
if (Timer1Event == 10) LED_ON; // Blink while waiting
if (Timer1Event == 20)
{
LED_OFF;
TimerEvent = 0;
Timer1Event = 0;
}
 
for (i=0;i<4;i++) // Select first active Satellite
while(Timer1Event < 3) // Check active satellites (for 3*8=24ms)
{
if (!(PINB & (1<<(i+4))))
for (i=0;i<4;i++)
{
active[i] = 1;
if (PINB & (1<<(i+4))) active_hi[i] = 1;
else active_lo[i] = 1;
}
}
 
for (i=0;i<4;i++) // When an input had low AND high signals, mark it as active
{
if (active_lo[i] == 1 && active_hi[i] == 1) active[i] = 1;
}
 
 
for (i=0;i<4;i++) // Select first active satellite
{
if (active[i] == 1)
{
SelectSat(i);
sat = i;
SelectSat(sat);
BlinkCode = i+1;
break;
}
}
 
}
 
LED_OFF;
 
 
 
while(1) // Main-Loop
{
for (i=0;i<4;i++) active[i] = 0; // Reset active-array
 
ResetTimer();
while((PINB & 0b11110000) == 0b11110000) // Wait for first signal (SYNC to frame)
for (i=0;i<4;i++) // Reset active-arrays
{
if (TimerEvent > 3) break; // (max. 3*8=24ms)
active[i] = 0;
active_lo[i] = 0;
active_hi[i] = 0;
}
ResetTimer();
while(TimerEvent < 1) // Check active satellites (for 1*8=8ms)
 
ResetTimer1();
while(Timer1Event < 3) // Check active satellites (for 3*8=24ms)
{
for (i=0;i<4;i++)
{
if (!(PINB & (1<<(i+4))))
{
active[i] = 1;
}
if (PINB & (1<<(i+4))) active_hi[i] = 1;
else active_lo[i] = 1;
}
}
 
if (active[sat] == 0) // Detect fail on active satellite
 
for (i=0;i<4;i++) // When an input had low AND high signals, mark it as active
{
for (i=0;i<4;i++) // Select lowest active satellite
if (active_lo[i] == 1 && active_hi[i] == 1) active[i] = 1;
}
 
if (active[0] == 0 && active[1] == 0 && active[2] == 0 && active[3] == 0 && sat != 99)
{
failure = 1; // Set Failure-LED when the signal is lost completely
BlinkCode = 0;
sat = 99;
}
 
for (i=0;i<4;i++) // Select active satellite (priorized)
{
if (active[i] == 1)
{
if (active[i] == 1)
{
sat = i;
SelectSat(sat);
break;
}
SelectSat(i);
if (sat != i) failure = 1; // Set Failure-LED when the active satellite changes
sat = i;
BlinkCode = i+1;
break;
}
LED_ON; // Failure-LED ON
}
 
if (!(PIND & (1<<PD6))) LED_OFF; // Reset Failure-LED when Bind-Button is pressed
 
 
if (!(PIND & (1<<PD6))) failure = 0; // Reset Failure-LED when Bind-Button is pressed
}
 
}