Rev 776 | Rev 780 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 776 | Rev 778 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | // ************************************************************ |
1 | // ************************************************************* |
2 | // ** Spektrum Diversity - use up to 4 satellite receivers ** |
2 | // ** Spektrum Diversity - use up to 4 satellite receivers ** |
3 | // ************************************************************ |
3 | // ************************************************************* |
4 | // ** Target: An Atmel ATtiny2313 (RC-Oscillator @ 8 MHz) ** |
4 | // ** Target: An Atmel ATtiny2313 (RC-Oscillator @ 8 MHz) ** |
5 | // ** controls a 74HC151 Multiplexer ** |
5 | // ** controls a 74HC151 Multiplexer ** |
6 | // ************************************************************ |
6 | // ************************************************************* |
7 | // ** It monitors the data from 4 satellite receivers and ** |
7 | // ** It monitors the data from 4 satellite receivers and ** |
8 | // ** connects a valid one to the output via the multiplexer ** |
8 | // ** connects a valid one to the output via the multiplexer ** |
9 | // ************************************************************ |
9 | // ************************************************************* |
10 | // ** LED-Modes during startup (in chronological order) ** |
10 | // ** LED-Modes during startup (in chronological order) ** |
11 | // ** ************************ ** |
11 | // ** ************************ ** |
12 | // ** LED fast blink: Waiting for first datapulse ** |
12 | // ** LED fast blink: Waiting for first datapulse ** |
13 | // ** LED slow blink: Waiting 3 seconds while counting sat's ** |
13 | // ** LED slow blink: Waiting 3 seconds while counting sat's ** |
14 | // ** LED flashes: Indicates the number of active sat's ** |
14 | // ** LED flashes: Indicates the number of active sat's ** |
15 | // ** ** |
15 | // ** ** |
16 | // ** LED-Modes during operation ** |
16 | // ** LED-Modes during operation ** |
17 | // ** ************************** ** |
17 | // ** ************************** ** |
18 | // ** LED OFF: Everything is fine ** |
18 | // ** LED OFF: Everything is fine ** |
19 | // ** LED ON: FAILURE - The first selected sat had lost sync ** |
19 | // ** LED ON: FAILURE - The first selected sat had lost sync ** |
- | 20 | // ** ** |
|
- | 21 | // ** LED-Modes during Self-Test ** |
|
- | 22 | // ** ************************** ** |
|
- | 23 | // ** LED flashes at 1 Hz: Everything is fine ** |
|
- | 24 | // ** LED flashes some times: FAILURE ** |
|
20 | // ************************************************************ |
25 | // ************************************************************* |
21 | // ** (c) 2010-0xFFFF Stefan Pendsa ** |
26 | // ** (c) 2010-0xFFFF Stefan Pendsa ** |
22 | // ** License: don't know - use it and have fun ** |
27 | // ** License: don't know - use it and have fun ** |
23 | // ************************************************************ |
28 | // ************************************************************* |
Line 24... | Line 29... | ||
24 | 29 | ||
25 | #include <avr/io.h> |
30 | #include <avr/io.h> |
26 | #include <avr/interrupt.h> |
31 | #include <avr/interrupt.h> |
Line 30... | Line 35... | ||
30 | #define LED_ON PORTD &= ~(1 << PORTD0) |
35 | #define LED_ON PORTD &= ~(1 << PORTD0) |
Line 31... | Line 36... | ||
31 | 36 | ||
Line -... | Line 37... | ||
- | 37 | volatile unsigned int TimerEvent; |
|
- | 38 | ||
- | 39 | ||
- | 40 | ||
- | 41 | ISR(TIMER1_OVF_vect) // Triggered every 8,192 msec |
|
- | 42 | { |
|
- | 43 | TimerEvent++; |
|
- | 44 | } |
|
32 | volatile unsigned int TimerEvent; |
45 | |
33 | 46 | ||
34 | 47 | ||
35 | void SelectSat(unsigned char sat) |
48 | void SelectSat(unsigned char sat) |
Line -... | Line 49... | ||
- | 49 | { |
|
- | 50 | PORTD = (PORTD & 0b00000001) | (sat<<3); // Select the Input for 74HC151 |
|
36 | { |
51 | } |
37 | PORTD = (PORTD & 0b00000001) | (sat<<3); // Select the Input for 74HC151 |
52 | |
38 | } |
53 | |
39 | 54 | ||
40 | void ResetTimer(void) |
55 | void ResetTimer(void) |
41 | { |
56 | { |
Line -... | Line 57... | ||
- | 57 | TCNT1H = 0; |
|
42 | TCNT1H = 0; |
58 | TCNT1L = 0; |
43 | TCNT1L = 0; |
59 | TimerEvent = 0; |
44 | TimerEvent = 0; |
60 | } |
Line 45... | Line 61... | ||
45 | } |
61 | |
Line 63... | Line 79... | ||
63 | _delay_ms(1000); |
79 | _delay_ms(1000); |
64 | DDRB = 0b00000000; // Sat-Pins to input after 1sec |
80 | DDRB = 0b00000000; // Sat-Pins to input after 1sec |
65 | } |
81 | } |
Line -... | Line 82... | ||
- | 82 | ||
66 | 83 | ||
67 | 84 | ||
68 | void Testing(void) // Self Test |
- | |
69 | { |
85 | void Testing(void) // Self Test |
- | 86 | { |
|
Line 70... | Line 87... | ||
70 | unsigned char i = 0; |
87 | unsigned char error = 0; |
71 | unsigned char error = 0; |
- | |
Line 72... | Line 88... | ||
72 | 88 | unsigned char i = 0; |
|
- | 89 | ||
- | 90 | DDRB = 0b11110000; // Port B Output for satellites, Input for feedback |
|
- | 91 | ||
- | 92 | PORTB = 0b01010000; // Test Pattern |
|
- | 93 | SelectSat(0); |
|
- | 94 | if (!(PINB & (1<<PB3))) error++; |
|
- | 95 | SelectSat(1); |
|
- | 96 | if (PINB & (1<<PB3)) error++; |
|
73 | DDRB = 0b11110000; // PORT B OUTPUT FOR SATELLITES, INPUT FOR FEEDBACK |
97 | SelectSat(2); |
- | 98 | if (!(PINB & (1<<PB3))) error++; |
|
74 | PORTB = 0b00000000; |
99 | SelectSat(3); |
75 | 100 | if (PINB & (1<<PB3)) error++; |
|
- | 101 | ||
76 | for(i=0;i<4;i++) // Test muxxing of High+Low on every Sat-Input |
102 | PORTB = 0b10100000; // Another (inverted) Test Pattern |
- | 103 | SelectSat(0); |
|
77 | { |
104 | if (PINB & (1<<PB3)) error++; |
- | 105 | SelectSat(1); |
|
78 | SelectSat(i); |
106 | if (!(PINB & (1<<PB3))) error++; |
79 | PORTB |= 1 << (i+4); |
107 | SelectSat(2); |
- | 108 | if (PINB & (1<<PB3)) error++; |
|
Line 80... | Line 109... | ||
80 | if (!(PINB & (1<<PB3))) error++; |
109 | SelectSat(3); |
81 | PORTB &= ~(1 << (i+4)); |
110 | if (!(PINB & (1<<PB3))) error++; |
82 | if (PINB & (1<<PB3)) error++; |
111 | |
83 | } |
112 | DDRB = 0b00000000; // Port B Input again |
84 | 113 | ||
85 | while(1) // Never return |
114 | while(1) // Never return |
86 | { |
115 | { |
- | 116 | if (error == 0) // When no error occured -> LED flashes at 1 Hz |
|
- | 117 | { |
|
- | 118 | LED_ON; |
|
- | 119 | _delay_ms(100); |
|
- | 120 | LED_OFF; |
|
- | 121 | _delay_ms(900); |
|
- | 122 | } |
|
87 | if (error == 0) // When no error occured, flash around |
123 | else |
- | 124 | { |
|
- | 125 | for (i=0;i<error;i++) // When error occured -> Flash once for every error |
|
- | 126 | { |
|
- | 127 | LED_ON; |
|
88 | { |
128 | _delay_ms(100); |
89 | LED_ON; |
- | |
90 | _delay_ms(100); |
- | |
Line -... | Line 129... | ||
- | 129 | LED_OFF; |
|
Line 91... | Line -... | ||
91 | LED_OFF; |
- | |
92 | _delay_ms(100); |
- | |
93 | } |
- | |
94 | } |
130 | _delay_ms(400); |
Line -... | Line 131... | ||
- | 131 | } |
|
95 | } |
132 | _delay_ms(1000); |
96 | 133 | } |
|
97 | 134 | ||
98 | ISR(TIMER1_OVF_vect) // Triggered every 8,192 msec |
135 | } |
99 | { |
136 | |
100 | TimerEvent++; |
137 | } |
Line 101... | Line 138... | ||
101 | } |
138 | |
102 | 139 | ||
103 | 140 | ||
104 | int main(void) |
141 | int main(void) |
Line 105... | Line 142... | ||
105 | { |
142 | { |
Line 106... | Line -... | ||
106 | unsigned char i = 0; |
- | |
107 | unsigned char j = 0; |
143 | unsigned char i = 0; |
- | 144 | unsigned char j = 0; |
|
Line 108... | Line 145... | ||
108 | unsigned char active[4]; |
145 | unsigned char active[4]; |
109 | unsigned char sat = 4; |
146 | unsigned char sat = 4; |
110 | 147 | ||
Line 130... | Line 167... | ||
130 | { |
167 | { |
131 | LED_OFF; |
168 | LED_OFF; |
132 | TimerEvent = 0; |
169 | TimerEvent = 0; |
133 | } |
170 | } |
134 | } |
171 | } |
135 | - | ||
136 | LED_OFF; |
172 | LED_OFF; |
Line 137... | Line 173... | ||
137 | 173 | ||
138 | ResetTimer(); |
174 | ResetTimer(); |
139 | while(1) // Check active satellites for 3 seconds |
175 | while(1) // Check active satellites for 5 seconds |
140 | { |
176 | { |
141 | if (TimerEvent == 20) LED_ON; // Blink (slower) while waiting... |
177 | if (TimerEvent == 20) LED_ON; // Blink (slower) while waiting... |
142 | if (TimerEvent == 40) |
178 | if (TimerEvent == 40) |
143 | { |
179 | { |
Line 158... | Line 194... | ||
158 | } |
194 | } |
Line 159... | Line 195... | ||
159 | 195 | ||
160 | } |
196 | } |
Line 161... | Line 197... | ||
161 | } |
197 | } |
162 | 198 | ||
Line 163... | Line 199... | ||
163 | if (j == 9) break; // 9 * 40 * 8ms = ~3sec |
199 | if (j == 16) break; // 16 * 40 * 8ms = ~5sec |
164 | } |
200 | } |
Line 171... | Line 207... | ||
171 | if (active[i] == 1) |
207 | if (active[i] == 1) |
172 | { |
208 | { |
173 | LED_ON; |
209 | LED_ON; |
174 | _delay_ms(100); |
210 | _delay_ms(100); |
175 | LED_OFF; |
211 | LED_OFF; |
176 | _delay_ms(200); |
212 | _delay_ms(400); |
177 | } |
213 | } |
178 | } |
214 | } |
Line 179... | Line 215... | ||
179 | 215 | ||
180 | 216 | ||
181 | 217 | ||
Line -... | Line 218... | ||
- | 218 | ||
182 | 219 | while(1) // Main-Loop |
|
183 | while(1) // Main-Loop |
220 | { |
184 | { |
221 | for (i=0;i<4;i++) active[i] = 0; // Reset active-array |
185 | ResetTimer(); |
222 | |
Line 186... | Line -... | ||
186 | - | ||
187 | while((PINB & 0b11110000) == 0b11110000) // Wait for first signal (SYNC to frame) |
223 | ResetTimer(); |
188 | { |
- | |
189 | if (TimerEvent > 3) break; // (max. 3*8=24ms) |
224 | while((PINB & 0b11110000) == 0b11110000) // Wait for first signal (SYNC to frame) |
190 | } |
225 | { |
Line 191... | Line 226... | ||
191 | 226 | if (TimerEvent > 3) break; // (max. 3*8=24ms) |
|
192 | for (i=0;i<4;i++) active[i] = 0; // Reset active-array |
227 | } |
Line 202... | Line 237... | ||
202 | active[i] = 1; |
237 | active[i] = 1; |
203 | } |
238 | } |
204 | } |
239 | } |
205 | } |
240 | } |
Line 206... | Line -... | ||
206 | - | ||
207 | 241 | ||
208 | if (active[sat] == 0) // Detect fail on active satellite |
242 | if (active[sat] == 0) // Detect fail on active satellite |
209 | { |
243 | { |
210 | LED_ON; // Failure-LED ON (never goes off again) |
244 | LED_ON; // Failure-LED ON |
211 | for (i=0;i<4;i++) // Select lowest active satellite |
245 | for (i=0;i<4;i++) // Select lowest active satellite |
212 | { |
246 | { |
213 | if (active[i] == 1) |
247 | if (active[i] == 1) |
214 | { |
248 | { |
Line 217... | Line 251... | ||
217 | break; |
251 | break; |
218 | } |
252 | } |
219 | } |
253 | } |
220 | } |
254 | } |
Line 221... | Line -... | ||
221 | - | ||
- | 255 | ||
222 | 256 | if (!(PIND & (1<<PD6))) LED_OFF; // Reset Failure-LED when Bind-Button is pressed |
|
Line 223... | Line 257... | ||
223 | } |
257 | } |
224 | - |