Subversion Repositories Projects

Rev

Rev 776 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
775 - 1
// ************************************************************
2
// ** Spektrum Diversity - use up to 8 satellite receivers   **
3
// ************************************************************
4
// ** Target: An Atmel ATtiny2313 (RC-Oscillator @ 8 MHz)    **
5
// **         controls a 74HC151 Multiplexer                 **
6
// ************************************************************
7
// ** It monitors the data from 8 satellite receivers and    **
8
// ** connects a valid one to the output via the multiplexer **
9
// ************************************************************
10
// ** LED-Modes during startup (in chronological order)      **
11
// ** ************************                               **
12
// ** LED fast blink: Waiting for first datapulse            **
13
// ** LED slow blink: Waiting 3 seconds while counting sat's **
14
// ** LED flashes: Indicates the number of active sat's      **
15
// **                                                        **
16
// ** LED-Modes during operation                             **
17
// ** **************************                             **
18
// ** LED OFF: Everything is fine                            **
19
// ** LED ON: FAILURE - The first selected sat had lost sync **
20
// ************************************************************
21
// ** (c) 2010-0xFFFF Stefan Pendsa                          **
22
// ** License: don't know - use it and have fun              **
23
// ************************************************************
24
 
25
#include <avr/io.h>
26
#include <avr/interrupt.h>
27
#include <util/delay.h>
28
 
29
#define LED_OFF	PORTD |= 1 << PORTD3
30
#define LED_ON	PORTD &= ~(1 << PORTD3)
31
 
32
 
33
volatile unsigned int TimerEvent;
34
 
35
 
36
void SelectSat(unsigned char sat)
37
{
38
	/*
39
	Re-Assignment due to easier routing on layout: sat -> mux-channel# -> mux-input (seen from µC)
40
 
41
	1 -> 1 -> 4
42
	2 -> 0 -> 0
43
	3 -> 3 -> 6
44
	4 -> 4 -> 1
45
	5 -> 5 -> 5
46
	6 -> 6 -> 3
47
	7 -> 7 -> 7
48
	*/
49
 
50
 
51
	if (sat == 0) sat = 2;
52
	else if (sat == 1) sat = 4;
53
	else if (sat == 2) sat = 0;
54
	else if (sat == 3) sat = 6;
55
	else if (sat == 4) sat = 1;
56
	else if (sat == 5) sat = 5;
57
	else if (sat == 6) sat = 3;
58
	else if (sat == 7) sat = 7;
59
 
60
 
61
	PORTD = (PORTD & 0b00001000) | sat;
62
}
63
 
64
void ResetTimer(void)
65
{
66
	TCNT1H = 0;
67
	TCNT1L = 0;
68
	TimerEvent = 0;
69
}
70
 
71
 
72
 
73
ISR(TIMER1_OVF_vect)										// Triggered every 4,096 msec
74
{
75
	TimerEvent++;
76
}
77
 
78
 
79
int main(void)
80
{
81
	unsigned char i = 0;
82
	unsigned int j = 0;
83
	unsigned char active[8];
84
	unsigned char sat = 9;
85
	DDRB = 0b00000000;										// PORT A INPUT FOR SATELLITES
86
	DDRD = 0b00001111;										// PORT B OUTPUT FOR MUX AND LED
87
	PORTB = 0b11111111;										// PORT A PULLUP's FOR (unused) SATELLITES
88
 
89
 
90
	TCCR1B = ( 1 << CS10 );									// Timer1 Prescaler 1 = 4,096 msec
91
	TIMSK = ( 1 << TOIE1 );									// Timer1 Overflow Interrupt Enable
92
	sei();													// Global Interrupts enable
93
 
94
	for (i=0;i<8;i++) active[i] = 0;						// Reset active-array
95
 
96
	ResetTimer();
97
	while(PINB == 255)										// Wait for first signal (SYNC to frame)
98
	{
99
		if (TimerEvent == 10) LED_ON;						// Blink while waiting...
100
		if (TimerEvent == 20)
101
		{
102
			LED_OFF;
103
			TimerEvent = 0;
104
		}
105
	}
106
 
107
	LED_OFF;
108
 
109
	ResetTimer();
110
	while(1)												// Check active satellites for 3 seconds
111
	{
112
		if (TimerEvent == 20) LED_ON;						// Blink (slower) while waiting...
113
		if (TimerEvent == 40)
114
		{
115
			LED_OFF;
116
			TimerEvent = 0;
117
			j++;
118
		}
119
 
120
		for (i=0;i<8;i++)
121
		{
122
			if (!(PINB & (1<<i)))
123
			{
124
				active[i] = 1;
125
				if (sat == 9)								// Select first active satellite (only once)
126
				{
127
					sat = i;
128
					SelectSat(i);
129
				}
130
 
131
			}
132
		}
133
 
134
		if (j == 9) break;									// 9 * 40 * 8ms = ~3sec
135
	}
136
 
137
	LED_OFF;
138
	_delay_ms(1000);
139
 
140
	for (i=0;i<8;i++)										// Flash once for every active satellite
141
	{
142
		if (active[i] == 1)
143
		{
144
			LED_ON;
145
			_delay_ms(200);
146
			LED_OFF;
147
			_delay_ms(200);
148
		}
149
	}
150
 
151
 
152
 
153
 
154
	while(1)												// Main-Loop
155
	{
156
		ResetTimer();
157
 
158
		while(PINB == 255)									// Wait for first signal (SYNC to frame)
159
		{
160
			if (TimerEvent > 3) break;						// (max. 3*8=24ms)
161
		}
162
 
163
		for (i=0;i<8;i++) active[i] = 0;					// Reset active-array
164
		ResetTimer();
165
 
166
		while(TimerEvent < 1)								// Check active satellites (for 1*8=8ms)
167
		{
168
 
169
			for (i=0;i<8;i++)
170
			{
171
				if (!(PINB & (1<<i)))
172
				{
173
					active[i] = 1;
174
				}
175
			}
176
		}
177
 
178
 
179
		if (active[sat] == 0)								// Detect fail on active satellite
180
		{
181
			LED_ON;											// Failure-LED ON (never goes off again)
182
			for (i=0;i<8;i++)								// Select lowest active satellite
183
			{
184
				if (active[i] == 1)
185
				{
186
					sat = i;
187
					SelectSat(sat);
188
					break;
189
				}
190
			}
191
		}
192
	}
193
}