Subversion Repositories Projects

Compare Revisions

Ignore whitespace Rev 774 → Rev 775

/Spektrum-Diversity/Software/diversity.c
0,0 → 1,193
// ************************************************************
// ** Spektrum Diversity - use up to 8 satellite receivers **
// ************************************************************
// ** Target: An Atmel ATtiny2313 (RC-Oscillator @ 8 MHz) **
// ** controls a 74HC151 Multiplexer **
// ************************************************************
// ** It monitors the data from 8 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 **
// ************************************************************
 
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
 
#define LED_OFF PORTD |= 1 << PORTD3
#define LED_ON PORTD &= ~(1 << PORTD3)
 
 
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;
}
 
void ResetTimer(void)
{
TCNT1H = 0;
TCNT1L = 0;
TimerEvent = 0;
}
 
 
 
ISR(TIMER1_OVF_vect) // Triggered every 4,096 msec
{
TimerEvent++;
}
 
 
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
 
 
TCCR1B = ( 1 << CS10 ); // Timer1 Prescaler 1 = 4,096 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)
{
if (TimerEvent == 10) LED_ON; // Blink while waiting...
if (TimerEvent == 20)
{
LED_OFF;
TimerEvent = 0;
}
}
 
LED_OFF;
 
ResetTimer();
while(1) // Check active satellites for 3 seconds
{
if (TimerEvent == 20) LED_ON; // Blink (slower) while waiting...
if (TimerEvent == 40)
{
LED_OFF;
TimerEvent = 0;
j++;
}
 
for (i=0;i<8;i++)
{
if (!(PINB & (1<<i)))
{
active[i] = 1;
if (sat == 9) // Select first active satellite (only once)
{
sat = i;
SelectSat(i);
}
 
}
}
if (j == 9) break; // 9 * 40 * 8ms = ~3sec
}
 
LED_OFF;
_delay_ms(1000);
 
for (i=0;i<8;i++) // Flash once for every active satellite
{
if (active[i] == 1)
{
LED_ON;
_delay_ms(200);
LED_OFF;
_delay_ms(200);
}
}
 
 
 
 
while(1) // Main-Loop
{
ResetTimer();
 
while(PINB == 255) // 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
ResetTimer();
 
while(TimerEvent < 1) // Check active satellites (for 1*8=8ms)
{
 
for (i=0;i<8;i++)
{
if (!(PINB & (1<<i)))
{
active[i] = 1;
}
}
}
 
 
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
{
if (active[i] == 1)
{
sat = i;
SelectSat(sat);
break;
}
}
}
}
}