Rev 780 | Rev 931 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 780 | Rev 791 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | // *************************************************************** |
1 | // *************************************************************** |
2 | // ** Spektrum Diversity v1.0 - use up to 4 satellite receivers ** |
2 | // ** Spektrum Diversity v1.3 - 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 ** |
11 | // ** ************************ ** |
11 | // ** ************************ ** |
12 | // ** LED fast blink: Waiting for first datapulse ** |
12 | // ** LED fast flash: 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 | // ** ** |
13 | // ** ** |
16 | // ** LED-Modes during operation ** |
14 | // ** LED-Modes during operation ** |
17 | // ** ************************** ** |
15 | // ** ************************** ** |
18 | // ** LED OFF: Everything is fine ** |
16 | // ** LED OFF: Everything is fine ** |
19 | // ** LED ON: FAILURE - The first selected sat had lost sync ** |
17 | // ** LED ON: FAILURE - The first selected sat had lost sync ** |
20 | // ** ** |
18 | // ** ** |
21 | // ** LED-Modes after Self-Test ** |
19 | // ** LED-Modes after Self-Test and pressed button ** |
22 | // ** ************************* ** |
20 | // ** ******************************************** ** |
23 | // ** LED flashes at 1 Hz: Everything is fine ** |
21 | // ** LED flashes at 1 Hz: Everything is fine ** |
24 | // ** LED flashes some times: Times flashed -> damaged Channel# ** |
22 | // ** LED flashes some times: Times flashed -> damaged Channel# ** |
25 | // ** LED off: check voltage(regulator) or firmware ** |
23 | // ** LED off: check voltage(regulator), button or firmware ** |
26 | // *************************************************************** |
24 | // *************************************************************** |
27 | // ** (c) 2010-0xFFFF Stefan Pendsa ** |
25 | // ** (c) 2010-0xFFFF Stefan Pendsa ** |
28 | // ** License: don't know - use it and have fun ** |
26 | // ** License: don't know - use it and have fun ** |
29 | // *************************************************************** |
27 | // *************************************************************** |
Line 30... | Line 28... | ||
30 | 28 | ||
- | 29 | #include <avr/io.h> |
|
31 | #include <avr/io.h> |
30 | #include <avr/eeprom.h> |
32 | #include <avr/interrupt.h> |
31 | #include <avr/interrupt.h> |
Line -... | Line 32... | ||
- | 32 | #include <util/delay.h> |
|
33 | #include <util/delay.h> |
33 | |
34 | 34 | ||
Line 35... | Line 35... | ||
35 | #define LED_OFF PORTD |= 1 << PD0 |
35 | #define LED_OFF PORTD |= 1 << PD0 |
Line -... | Line 36... | ||
- | 36 | #define LED_ON PORTD &= ~(1 << PD0) |
|
- | 37 | ||
- | 38 | volatile unsigned int TimerEvent; |
|
- | 39 | ||
- | 40 | unsigned char eedummy EEMEM; // Dummy-Variable for Address 0 in EEPROM |
|
Line 36... | Line 41... | ||
36 | #define LED_ON PORTD &= ~(1 << PD0) |
41 | unsigned char eecheck EEMEM; // Check-Variable |
37 | 42 | unsigned char bind; // Bind-Pulse-Counter (in RAM) |
|
38 | volatile unsigned int TimerEvent; |
43 | unsigned char eebind EEMEM; // Bind-Pulse-Counter (in EEPROM) |
Line 61... | Line 66... | ||
61 | TimerEvent = 0; |
66 | TimerEvent = 0; |
62 | } |
67 | } |
Line 63... | Line 68... | ||
63 | 68 | ||
64 | 69 | ||
65 | 70 | ||
Line 66... | Line 71... | ||
66 | void Binding(void) // Binding sequence for DX7 |
71 | void Binding(void) // Binding sequence |
67 | { |
72 | { |
Line 68... | Line 73... | ||
68 | unsigned char i = 0; |
73 | unsigned char i = 0; |
69 | 74 | ||
70 | DDRB = 0b11110000; // Sat-Pins to output |
75 | DDRB = 0b11110000; // Sat-Pins to output |
71 | _delay_ms(80); // Let them time to boot up |
76 | _delay_ms(80); // Let them time to boot up |
72 | 77 | ||
73 | for (i=0;i<3;i++) // send three negative 100µs pulses to all sat's |
78 | for (i=0;i<bind;i++) // Send negative 100µs pulses to all sat's |
74 | { |
79 | { |
Line -... | Line 80... | ||
- | 80 | PORTB = 0b00000000; |
|
- | 81 | _delay_us(100); |
|
- | 82 | PORTB = 0b11110000; |
|
75 | PORTB = 0b00000000; |
83 | _delay_us(100); |
- | 84 | } |
|
- | 85 | ||
- | 86 | for (i=0;i<bind;i++) // Flash the number of used pulses to the LED |
|
- | 87 | { |
|
76 | _delay_us(100); |
88 | LED_ON; |
- | 89 | _delay_ms(100); |
|
- | 90 | LED_OFF; |
|
- | 91 | _delay_ms(250); |
|
- | 92 | } |
|
- | 93 | ||
77 | PORTB = 0b11110000; |
94 | DDRB = 0b00000000; // Sat-Pins to input again |
Line 78... | Line 95... | ||
78 | _delay_us(100); |
95 | |
Line 111... | Line 128... | ||
111 | SelectSat(3); |
128 | SelectSat(3); |
112 | if (!(PINB & (1<<PB3))) error = 4; |
129 | if (!(PINB & (1<<PB3))) error = 4; |
Line 113... | Line 130... | ||
113 | 130 | ||
Line -... | Line 131... | ||
- | 131 | DDRB = 0b00000000; // Port B Input again |
|
- | 132 | ||
114 | DDRB = 0b00000000; // Port B Input again |
133 | while (PIND & (1<<PD6)); // Wait for Bind-Switch |
115 | 134 | ||
116 | while(1) // Never return |
135 | while(1) // Never return |
117 | { |
136 | { |
118 | if (error == 0) // When no error occured -> LED flashes at 1 Hz |
137 | if (error == 0) // When no error occured -> LED flashes at 1 Hz |
Line 141... | Line 160... | ||
141 | 160 | ||
142 | 161 | ||
143 | int main(void) |
162 | int main(void) |
144 | { |
- | |
145 | unsigned char i = 0; |
163 | { |
146 | unsigned char j = 0; |
164 | unsigned char i = 0; |
Line 147... | Line 165... | ||
147 | unsigned char active[4]; |
165 | unsigned char active[4]; |
148 | unsigned char sat = 4; |
166 | unsigned char sat = 99; |
149 | 167 | ||
150 | DDRB = 0b00000000; // Port B Input for satellites and feedback |
168 | DDRB = 0b00000000; // Port B Input for satellites and feedback |
Line 151... | Line 169... | ||
151 | DDRD = 0b0011001; // Port D Output for MUX and LED, Input for Switch & Test |
169 | DDRD = 0b0011001; // Port D Output for MUX and LED, Input for Switch & Test |
Line -... | Line 170... | ||
- | 170 | PORTB = 0b11110000; // Port B Pullup's for (unused) satellites |
|
- | 171 | PORTD = 0b1100001; // Port D Pullup's for Switch & Test, LED off |
|
- | 172 | ||
- | 173 | for (i=0;i<4;i++) active[i] = 0; // Reset active-array |
|
- | 174 | ||
- | 175 | ||
- | 176 | if (eeprom_read_byte(&eecheck) != 0x42) // Invalid Data in EEPROM -> initialize |
|
- | 177 | { |
|
- | 178 | eeprom_write_byte(&eecheck, 0x42); |
|
- | 179 | bind = 3; |
|
152 | PORTB = 0b11110000; // Port B Pullup's for (unused) satellites |
180 | eeprom_write_byte(&eebind, bind); |
153 | PORTD = 0b1100001; // Port D Pullup's for Switch & Test, LED off |
181 | } |
Line 154... | Line 182... | ||
154 | 182 | else bind = eeprom_read_byte(&eebind); // Valid Data in EEPROM -> read bind-pulse-counter |
|
Line 155... | Line 183... | ||
155 | for (i=0;i<4;i++) active[i] = 0; // Reset active-array |
183 | |
156 | 184 | ||
157 | if (!(PIND & (1<<PD5))) Testing(); // Initiate Self-Test when Test-Pad is low |
185 | if (!(PIND & (1<<PD5))) Testing(); // Initiate Self-Test when Test-Pad is low |
Line 158... | Line 186... | ||
158 | if (!(PIND & (1<<PD6))) Binding(); // Initiate Binding when Bind-Button is pressed |
186 | if (!(PIND & (1<<PD6))) Binding(); // Initiate Binding when Bind-Button is pressed |
159 | 187 | ||
160 | _delay_ms(100); |
188 | _delay_ms(100); |
161 | 189 | ||
162 | TCCR1B = ( 1 << CS10 ); // Timer1 Prescaler = 1 -> 8,192 msec |
190 | TCCR1B = ( 1 << CS10 ); // Timer1 Prescaler = 1 -> 8,192 msec |
163 | TIMSK = ( 1 << TOIE1 ); // Timer1 Overflow Interrupt Enable |
191 | TIMSK = ( 1 << TOIE1 ); // Timer1 Overflow Interrupt Enable |
164 | sei(); // Global Interrupts enable |
192 | sei(); // Global Interrupts enable |
165 | 193 | ||
166 | ResetTimer(); |
194 | ResetTimer(); |
167 | while((PINB & 0b11110000) == 0b11110000) // Wait for first signal (SYNC to frame) |
- | |
168 | { |
- | |
169 | if (TimerEvent == 10) LED_ON; // Blink while waiting... |
- | |
170 | if (TimerEvent == 20) |
- | |
171 | { |
- | |
172 | LED_OFF; |
- | |
173 | TimerEvent = 0; |
- | |
174 | } |
- | |
175 | } |
- | |
176 | LED_OFF; |
- | |
177 | - | ||
178 | ResetTimer(); |
- | |
179 | while(1) // Check active satellites for 5 seconds |
- | |
Line 180... | Line 195... | ||
180 | { |
195 | while(sat == 99) // Wait for first signal |
181 | if (TimerEvent == 20) LED_ON; // Blink (slower) while waiting... |
196 | { |
182 | if (TimerEvent == 40) |
197 | if (TimerEvent == 10) LED_ON; // Blink while waiting... |
183 | { |
198 | if (TimerEvent == 20) |
184 | LED_OFF; |
199 | { |
185 | TimerEvent = 0; |
- | |
186 | j++; |
- | |
187 | } |
200 | LED_OFF; |
188 | 201 | TimerEvent = 0; |
|
189 | for (i=0;i<4;i++) |
202 | } |
190 | { |
- | |
191 | if (!(PINB & (1<<(i+4)))) |
203 | |
192 | { |
204 | for (i=0;i<4;i++) // Select first active Satellite |
193 | active[i] = 1; |
- | |
194 | if (sat == 4) // Select first active satellite (only once) |
- | |
195 | { |
205 | { |
Line 196... | Line 206... | ||
196 | sat = i; |
206 | if (!(PINB & (1<<(i+4)))) |
197 | SelectSat(i); |
- | |
198 | } |
- | |
199 | - | ||
200 | } |
- | |
201 | } |
- | |
202 | - | ||
203 | if (j == 16) break; // 16 * 40 * 8ms = ~5sec |
- | |
204 | } |
- | |
205 | - | ||
206 | LED_OFF; |
- | |
207 | _delay_ms(1000); |
- | |
208 | - | ||
209 | for (i=0;i<4;i++) // Flash once for every active satellite |
- | |
210 | { |
- | |
211 | if (active[i] == 1) |
- | |
Line 212... | Line 207... | ||
212 | { |
207 | { |
213 | LED_ON; |
208 | active[i] = 1; |
214 | _delay_ms(100); |
209 | sat = i; |
Line 231... | Line 226... | ||
231 | } |
226 | } |
Line 232... | Line 227... | ||
232 | 227 | ||
233 | ResetTimer(); |
228 | ResetTimer(); |
234 | while(TimerEvent < 1) // Check active satellites (for 1*8=8ms) |
229 | while(TimerEvent < 1) // Check active satellites (for 1*8=8ms) |
235 | { |
- | |
236 | 230 | { |
|
237 | for (i=0;i<4;i++) |
231 | for (i=0;i<4;i++) |
238 | { |
232 | { |
239 | if (!(PINB & (1<<(i+4)))) |
233 | if (!(PINB & (1<<(i+4)))) |
240 | { |
234 | { |
Line 243... | Line 237... | ||
243 | } |
237 | } |
244 | } |
238 | } |
Line 245... | Line 239... | ||
245 | 239 | ||
246 | if (active[sat] == 0) // Detect fail on active satellite |
240 | if (active[sat] == 0) // Detect fail on active satellite |
247 | { |
- | |
248 | LED_ON; // Failure-LED ON |
241 | { |
249 | for (i=0;i<4;i++) // Select lowest active satellite |
242 | for (i=0;i<4;i++) // Select lowest active satellite |
250 | { |
243 | { |
251 | if (active[i] == 1) |
244 | if (active[i] == 1) |
252 | { |
245 | { |
253 | sat = i; |
246 | sat = i; |
254 | SelectSat(sat); |
247 | SelectSat(sat); |
255 | break; |
248 | break; |
256 | } |
249 | } |
- | 250 | } |
|
257 | } |
251 | LED_ON; // Failure-LED ON |
Line 258... | Line 252... | ||
258 | } |
252 | } |
259 | 253 |