Rev 775 | Rev 778 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
775 | - | 1 | // ************************************************************ |
776 | - | 2 | // ** Spektrum Diversity - use up to 4 satellite receivers ** |
775 | - | 3 | // ************************************************************ |
4 | // ** Target: An Atmel ATtiny2313 (RC-Oscillator @ 8 MHz) ** |
||
5 | // ** controls a 74HC151 Multiplexer ** |
||
6 | // ************************************************************ |
||
776 | - | 7 | // ** It monitors the data from 4 satellite receivers and ** |
775 | - | 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 | |||
776 | - | 29 | #define LED_OFF PORTD |= 1 << PORTD0 |
30 | #define LED_ON PORTD &= ~(1 << PORTD0) |
||
775 | - | 31 | |
32 | volatile unsigned int TimerEvent; |
||
33 | |||
34 | |||
35 | void SelectSat(unsigned char sat) |
||
36 | { |
||
776 | - | 37 | PORTD = (PORTD & 0b00000001) | (sat<<3); // Select the Input for 74HC151 |
775 | - | 38 | } |
39 | |||
40 | void ResetTimer(void) |
||
41 | { |
||
42 | TCNT1H = 0; |
||
43 | TCNT1L = 0; |
||
44 | TimerEvent = 0; |
||
45 | } |
||
46 | |||
47 | |||
776 | - | 48 | void Binding(void) // Binding sequence for DX7 |
49 | { |
||
50 | unsigned char i = 0; |
||
775 | - | 51 | |
776 | - | 52 | DDRB = 0b11110000; // Sat-Pins to output |
53 | _delay_ms(80); // Let them boot up |
||
54 | |||
55 | for (i=0;i<3;i++) // send three negative 100µs pulses to all sat's |
||
56 | { |
||
57 | PORTB = 0b00000000; |
||
58 | _delay_us(100); |
||
59 | PORTB = 0b11110000; |
||
60 | _delay_us(100); |
||
61 | } |
||
62 | |||
63 | _delay_ms(1000); |
||
64 | DDRB = 0b00000000; // Sat-Pins to input after 1sec |
||
65 | } |
||
66 | |||
67 | |||
68 | void Testing(void) // Self Test |
||
775 | - | 69 | { |
776 | - | 70 | unsigned char i = 0; |
71 | unsigned char error = 0; |
||
72 | |||
73 | DDRB = 0b11110000; // PORT B OUTPUT FOR SATELLITES, INPUT FOR FEEDBACK |
||
74 | PORTB = 0b00000000; |
||
75 | |||
76 | for(i=0;i<4;i++) // Test muxxing of High+Low on every Sat-Input |
||
77 | { |
||
78 | SelectSat(i); |
||
79 | PORTB |= 1 << (i+4); |
||
80 | if (!(PINB & (1<<PB3))) error++; |
||
81 | PORTB &= ~(1 << (i+4)); |
||
82 | if (PINB & (1<<PB3)) error++; |
||
83 | } |
||
84 | |||
85 | while(1) // Never return |
||
86 | { |
||
87 | if (error == 0) // When no error occured, flash around |
||
88 | { |
||
89 | LED_ON; |
||
90 | _delay_ms(100); |
||
91 | LED_OFF; |
||
92 | _delay_ms(100); |
||
93 | } |
||
94 | } |
||
95 | } |
||
96 | |||
97 | |||
98 | ISR(TIMER1_OVF_vect) // Triggered every 8,192 msec |
||
99 | { |
||
775 | - | 100 | TimerEvent++; |
101 | } |
||
102 | |||
103 | |||
104 | int main(void) |
||
105 | { |
||
106 | unsigned char i = 0; |
||
776 | - | 107 | unsigned char j = 0; |
108 | unsigned char active[4]; |
||
109 | unsigned char sat = 4; |
||
775 | - | 110 | |
776 | - | 111 | DDRB = 0b00000000; // PORT B INPUT FOR SATELLITES AND FEEDBACK |
112 | DDRD = 0b0011001; // PORT D OUTPUT FOR MUX AND LED, INPUT FOR SWITCH & TEST |
||
113 | PORTB = 0b11110000; // PORT B PULLUP's FOR (unused) SATELLITES |
||
114 | PORTD = 0b1100001; // PORT D PULLUP's FOR SWITCH & TEST |
||
775 | - | 115 | |
776 | - | 116 | for (i=0;i<4;i++) active[i] = 0; // Reset active-array |
117 | |||
118 | if (!(PIND & (1<<PD6))) Binding(); // Initiate Binding when Bind-Button is pressed |
||
119 | if (!(PIND & (1<<PD5))) Testing(); // Initiate Self-Test when Test-Pad is low |
||
120 | |||
121 | TCCR1B = ( 1 << CS10 ); // Timer1 Prescaler = 1 -> 8,192 msec |
||
775 | - | 122 | TIMSK = ( 1 << TOIE1 ); // Timer1 Overflow Interrupt Enable |
123 | sei(); // Global Interrupts enable |
||
124 | |||
125 | ResetTimer(); |
||
776 | - | 126 | while((PINB & 0b11110000) == 0b11110000) // Wait for first signal (SYNC to frame) |
775 | - | 127 | { |
128 | if (TimerEvent == 10) LED_ON; // Blink while waiting... |
||
129 | if (TimerEvent == 20) |
||
130 | { |
||
131 | LED_OFF; |
||
132 | TimerEvent = 0; |
||
133 | } |
||
134 | } |
||
135 | |||
136 | LED_OFF; |
||
137 | |||
138 | ResetTimer(); |
||
139 | while(1) // Check active satellites for 3 seconds |
||
140 | { |
||
141 | if (TimerEvent == 20) LED_ON; // Blink (slower) while waiting... |
||
142 | if (TimerEvent == 40) |
||
143 | { |
||
144 | LED_OFF; |
||
145 | TimerEvent = 0; |
||
146 | j++; |
||
147 | } |
||
148 | |||
776 | - | 149 | for (i=0;i<4;i++) |
775 | - | 150 | { |
776 | - | 151 | if (!(PINB & (1<<(i+4)))) |
775 | - | 152 | { |
153 | active[i] = 1; |
||
776 | - | 154 | if (sat == 4) // Select first active satellite (only once) |
775 | - | 155 | { |
156 | sat = i; |
||
157 | SelectSat(i); |
||
158 | } |
||
159 | |||
160 | } |
||
161 | } |
||
162 | |||
163 | if (j == 9) break; // 9 * 40 * 8ms = ~3sec |
||
164 | } |
||
165 | |||
166 | LED_OFF; |
||
167 | _delay_ms(1000); |
||
168 | |||
776 | - | 169 | for (i=0;i<4;i++) // Flash once for every active satellite |
775 | - | 170 | { |
171 | if (active[i] == 1) |
||
172 | { |
||
173 | LED_ON; |
||
776 | - | 174 | _delay_ms(100); |
775 | - | 175 | LED_OFF; |
176 | _delay_ms(200); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | |||
181 | |||
182 | |||
183 | while(1) // Main-Loop |
||
184 | { |
||
185 | ResetTimer(); |
||
186 | |||
776 | - | 187 | while((PINB & 0b11110000) == 0b11110000) // Wait for first signal (SYNC to frame) |
775 | - | 188 | { |
189 | if (TimerEvent > 3) break; // (max. 3*8=24ms) |
||
190 | } |
||
191 | |||
776 | - | 192 | for (i=0;i<4;i++) active[i] = 0; // Reset active-array |
775 | - | 193 | ResetTimer(); |
194 | |||
195 | while(TimerEvent < 1) // Check active satellites (for 1*8=8ms) |
||
196 | { |
||
197 | |||
776 | - | 198 | for (i=0;i<4;i++) |
775 | - | 199 | { |
776 | - | 200 | if (!(PINB & (1<<(i+4)))) |
775 | - | 201 | { |
202 | active[i] = 1; |
||
203 | } |
||
204 | } |
||
205 | } |
||
206 | |||
207 | |||
208 | if (active[sat] == 0) // Detect fail on active satellite |
||
209 | { |
||
210 | LED_ON; // Failure-LED ON (never goes off again) |
||
776 | - | 211 | for (i=0;i<4;i++) // Select lowest active satellite |
775 | - | 212 | { |
213 | if (active[i] == 1) |
||
214 | { |
||
215 | sat = i; |
||
216 | SelectSat(sat); |
||
217 | break; |
||
218 | } |
||
219 | } |
||
220 | } |
||
776 | - | 221 | |
222 | |||
775 | - | 223 | } |
776 | - | 224 | |
775 | - | 225 | } |
776 | - | 226 |