Subversion Repositories Projects

Compare Revisions

Ignore whitespace Rev 777 → Rev 778

/Spektrum-Diversity/Software/diversity.c
1,26 → 1,31
// ************************************************************
// ** 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 4 satellite receivers and **
// ** connects a valid one to the output via the multiplexer **
// ************************************************************
// ** LED-Modes during startup (in chronological order) **
// ** ************************ **
// ** LED fast blink: Waiting for first datapulse **
// ** LED slow blink: Waiting 3 seconds while counting sat's **
// ** LED flashes: Indicates the number of active sat's **
// ** **
// ** LED-Modes during operation **
// ** ************************** **
// ** LED OFF: Everything is fine **
// ** LED ON: FAILURE - The first selected sat had lost sync **
// ************************************************************
// ** (c) 2010-0xFFFF Stefan Pendsa **
// ** License: don't know - use it and have fun **
// ************************************************************
// *************************************************************
// ** 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 4 satellite receivers and **
// ** connects a valid one to the output via the multiplexer **
// *************************************************************
// ** LED-Modes during startup (in chronological order) **
// ** ************************ **
// ** LED fast blink: Waiting for first datapulse **
// ** LED slow blink: Waiting 3 seconds while counting sat's **
// ** LED flashes: Indicates the number of active sat's **
// ** **
// ** LED-Modes during operation **
// ** ************************** **
// ** LED OFF: Everything is fine **
// ** LED ON: FAILURE - The first selected sat had lost sync **
// ** **
// ** LED-Modes during Self-Test **
// ** ************************** **
// ** LED flashes at 1 Hz: Everything is fine **
// ** LED flashes some times: FAILURE **
// *************************************************************
// ** (c) 2010-0xFFFF Stefan Pendsa **
// ** License: don't know - use it and have fun **
// *************************************************************
 
#include <avr/io.h>
#include <avr/interrupt.h>
32,11 → 37,21
volatile unsigned int TimerEvent;
 
 
 
ISR(TIMER1_OVF_vect) // Triggered every 8,192 msec
{
TimerEvent++;
}
 
 
 
void SelectSat(unsigned char sat)
{
PORTD = (PORTD & 0b00000001) | (sat<<3); // Select the Input for 74HC151
}
 
 
 
void ResetTimer(void)
{
TCNT1H = 0;
45,6 → 60,7
}
 
 
 
void Binding(void) // Binding sequence for DX7
{
unsigned char i = 0;
65,42 → 81,63
}
 
 
 
void Testing(void) // Self Test
{
unsigned char error = 0;
unsigned char i = 0;
unsigned char error = 0;
 
DDRB = 0b11110000; // PORT B OUTPUT FOR SATELLITES, INPUT FOR FEEDBACK
PORTB = 0b00000000;
DDRB = 0b11110000; // Port B Output for satellites, Input for feedback
 
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++;
}
PORTB = 0b01010000; // Test Pattern
SelectSat(0);
if (!(PINB & (1<<PB3))) error++;
SelectSat(1);
if (PINB & (1<<PB3)) error++;
SelectSat(2);
if (!(PINB & (1<<PB3))) error++;
SelectSat(3);
if (PINB & (1<<PB3)) error++;
 
PORTB = 0b10100000; // Another (inverted) Test Pattern
SelectSat(0);
if (PINB & (1<<PB3)) error++;
SelectSat(1);
if (!(PINB & (1<<PB3))) error++;
SelectSat(2);
if (PINB & (1<<PB3)) error++;
SelectSat(3);
if (!(PINB & (1<<PB3))) error++;
 
DDRB = 0b00000000; // Port B Input again
 
while(1) // Never return
{
if (error == 0) // When no error occured, flash around
if (error == 0) // When no error occured -> LED flashes at 1 Hz
{
LED_ON;
_delay_ms(100);
LED_OFF;
_delay_ms(100);
_delay_ms(900);
}
else
{
for (i=0;i<error;i++) // When error occured -> Flash once for every error
{
LED_ON;
_delay_ms(100);
LED_OFF;
_delay_ms(400);
}
_delay_ms(1000);
}
 
}
 
}
 
 
ISR(TIMER1_OVF_vect) // Triggered every 8,192 msec
{
TimerEvent++;
}
 
 
int main(void)
{
unsigned char i = 0;
108,15 → 145,15
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
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 on
 
for (i=0;i<4;i++) active[i] = 0; // Reset active-array
 
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
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
132,11 → 169,10
TimerEvent = 0;
}
}
 
LED_OFF;
 
ResetTimer();
while(1) // Check active satellites for 3 seconds
while(1) // Check active satellites for 5 seconds
{
if (TimerEvent == 20) LED_ON; // Blink (slower) while waiting...
if (TimerEvent == 40)
160,7 → 196,7
}
}
if (j == 9) break; // 9 * 40 * 8ms = ~3sec
if (j == 16) break; // 16 * 40 * 8ms = ~5sec
}
 
LED_OFF;
173,7 → 209,7
LED_ON;
_delay_ms(100);
LED_OFF;
_delay_ms(200);
_delay_ms(400);
}
}
 
182,16 → 218,15
 
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)
{
if (TimerEvent > 3) break; // (max. 3*8=24ms)
}
for (i=0;i<4;i++) active[i] = 0; // Reset active-array
ResetTimer();
 
while(TimerEvent < 1) // Check active satellites (for 1*8=8ms)
{
 
204,10 → 239,9
}
}
 
 
if (active[sat] == 0) // Detect fail on active satellite
{
LED_ON; // Failure-LED ON (never goes off again)
LED_ON; // Failure-LED ON
for (i=0;i<4;i++) // Select lowest active satellite
{
if (active[i] == 1)
219,8 → 253,7
}
}
 
 
if (!(PIND & (1<<PD6))) LED_OFF; // Reset Failure-LED when Bind-Button is pressed
}
 
}