Rev 775 | Rev 778 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 775 | Rev 776 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | // ************************************************************ |
1 | // ************************************************************ |
2 | // ** Spektrum Diversity - use up to 8 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 8 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 ** |
Line 24... | Line 24... | ||
24 | 24 | ||
25 | #include <avr/io.h> |
25 | #include <avr/io.h> |
26 | #include <avr/interrupt.h> |
26 | #include <avr/interrupt.h> |
Line 27... | Line 27... | ||
27 | #include <util/delay.h> |
27 | #include <util/delay.h> |
28 | 28 | ||
29 | #define LED_OFF PORTD |= 1 << PORTD3 |
- | |
Line 30... | Line 29... | ||
30 | #define LED_ON PORTD &= ~(1 << PORTD3) |
29 | #define LED_OFF PORTD |= 1 << PORTD0 |
Line 31... | Line 30... | ||
31 | 30 | #define LED_ON PORTD &= ~(1 << PORTD0) |
|
32 | 31 | ||
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 | 0 -> 2 -> 2 |
- | |
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; |
32 | volatile unsigned int TimerEvent; |
57 | else if (sat == 6) sat = 3; |
33 | |
Line 58... | Line 34... | ||
58 | else if (sat == 7) sat = 7; |
34 | |
59 | 35 | void SelectSat(unsigned char sat) |
|
60 | 36 | { |
|
61 | PORTD = (PORTD & 0b00001000) | sat; |
37 | PORTD = (PORTD & 0b00000001) | (sat<<3); // Select the Input for 74HC151 |
62 | } |
38 | } |
63 | 39 | ||
Line -... | Line 40... | ||
- | 40 | void ResetTimer(void) |
|
- | 41 | { |
|
- | 42 | TCNT1H = 0; |
|
- | 43 | TCNT1L = 0; |
|
- | 44 | TimerEvent = 0; |
|
- | 45 | } |
|
- | 46 | ||
- | 47 | ||
- | 48 | void Binding(void) // Binding sequence for DX7 |
|
- | 49 | { |
|
- | 50 | unsigned char i = 0; |
|
- | 51 | ||
- | 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 |
|
- | 69 | { |
|
- | 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 | { |
|
Line 64... | Line 89... | ||
64 | void ResetTimer(void) |
89 | LED_ON; |
65 | { |
90 | _delay_ms(100); |
66 | TCNT1H = 0; |
91 | LED_OFF; |
67 | TCNT1L = 0; |
92 | _delay_ms(100); |
Line 68... | Line 93... | ||
68 | TimerEvent = 0; |
93 | } |
69 | } |
94 | } |
70 | 95 | } |
|
71 | 96 | ||
72 | 97 | ||
73 | ISR(TIMER1_OVF_vect) // Triggered every 4,096 msec |
98 | ISR(TIMER1_OVF_vect) // Triggered every 8,192 msec |
- | 99 | { |
|
74 | { |
100 | TimerEvent++; |
75 | TimerEvent++; |
101 | } |
76 | } |
102 | |
- | 103 | ||
- | 104 | int main(void) |
|
- | 105 | { |
|
Line -... | Line 106... | ||
- | 106 | unsigned char i = 0; |
|
- | 107 | unsigned char j = 0; |
|
Line 77... | Line 108... | ||
77 | 108 | unsigned char active[4]; |
|
78 | 109 | unsigned char sat = 4; |
|
79 | int main(void) |
110 | |
Line 80... | Line -... | ||
80 | { |
- | |
81 | unsigned char i = 0; |
- | |
82 | unsigned int j = 0; |
111 | DDRB = 0b00000000; // PORT B INPUT FOR SATELLITES AND FEEDBACK |
83 | unsigned char active[8]; |
112 | DDRD = 0b0011001; // PORT D OUTPUT FOR MUX AND LED, INPUT FOR SWITCH & TEST |
84 | unsigned char sat = 9; |
113 | PORTB = 0b11110000; // PORT B PULLUP's FOR (unused) SATELLITES |
85 | DDRB = 0b00000000; // PORT A INPUT FOR SATELLITES |
114 | PORTD = 0b1100001; // PORT D PULLUP's FOR SWITCH & TEST |
86 | DDRD = 0b00001111; // PORT B OUTPUT FOR MUX AND LED |
115 | |
87 | PORTB = 0b11111111; // PORT A PULLUP's FOR (unused) SATELLITES |
116 | for (i=0;i<4;i++) active[i] = 0; // Reset active-array |
88 | 117 | ||
Line 115... | Line 144... | ||
115 | LED_OFF; |
144 | LED_OFF; |
116 | TimerEvent = 0; |
145 | TimerEvent = 0; |
117 | j++; |
146 | j++; |
118 | } |
147 | } |
Line 119... | Line 148... | ||
119 | 148 | ||
120 | for (i=0;i<8;i++) |
149 | for (i=0;i<4;i++) |
121 | { |
150 | { |
122 | if (!(PINB & (1<<i))) |
151 | if (!(PINB & (1<<(i+4)))) |
123 | { |
152 | { |
124 | active[i] = 1; |
153 | active[i] = 1; |
125 | if (sat == 9) // Select first active satellite (only once) |
154 | if (sat == 4) // Select first active satellite (only once) |
126 | { |
155 | { |
127 | sat = i; |
156 | sat = i; |
128 | SelectSat(i); |
157 | SelectSat(i); |
Line 135... | Line 164... | ||
135 | } |
164 | } |
Line 136... | Line 165... | ||
136 | 165 | ||
137 | LED_OFF; |
166 | LED_OFF; |
Line 138... | Line 167... | ||
138 | _delay_ms(1000); |
167 | _delay_ms(1000); |
139 | 168 | ||
140 | for (i=0;i<8;i++) // Flash once for every active satellite |
169 | for (i=0;i<4;i++) // Flash once for every active satellite |
141 | { |
170 | { |
142 | if (active[i] == 1) |
171 | if (active[i] == 1) |
143 | { |
172 | { |
144 | LED_ON; |
173 | LED_ON; |
145 | _delay_ms(200); |
174 | _delay_ms(100); |
146 | LED_OFF; |
175 | LED_OFF; |
147 | _delay_ms(200); |
176 | _delay_ms(200); |
Line 153... | Line 182... | ||
153 | 182 | ||
154 | while(1) // Main-Loop |
183 | while(1) // Main-Loop |
155 | { |
184 | { |
Line 156... | Line 185... | ||
156 | ResetTimer(); |
185 | ResetTimer(); |
157 | 186 | ||
158 | while(PINB == 255) // Wait for first signal (SYNC to frame) |
187 | while((PINB & 0b11110000) == 0b11110000) // Wait for first signal (SYNC to frame) |
159 | { |
188 | { |
Line 160... | Line 189... | ||
160 | if (TimerEvent > 3) break; // (max. 3*8=24ms) |
189 | if (TimerEvent > 3) break; // (max. 3*8=24ms) |
161 | } |
190 | } |
Line 162... | Line 191... | ||
162 | 191 | ||
163 | for (i=0;i<8;i++) active[i] = 0; // Reset active-array |
192 | for (i=0;i<4;i++) active[i] = 0; // Reset active-array |
Line 164... | Line 193... | ||
164 | ResetTimer(); |
193 | ResetTimer(); |
165 | 194 | ||
166 | while(TimerEvent < 1) // Check active satellites (for 1*8=8ms) |
195 | while(TimerEvent < 1) // Check active satellites (for 1*8=8ms) |
167 | { |
196 | { |
168 | 197 | ||
169 | for (i=0;i<8;i++) |
198 | for (i=0;i<4;i++) |
170 | { |
199 | { |
171 | if (!(PINB & (1<<i))) |
200 | if (!(PINB & (1<<(i+4)))) |
Line 172... | Line 201... | ||
172 | { |
201 | { |
173 | active[i] = 1; |
202 | active[i] = 1; |
174 | } |
203 | } |
175 | } |
204 | } |
176 | } |
205 | } |
177 | 206 | ||
178 | 207 | ||
179 | if (active[sat] == 0) // Detect fail on active satellite |
208 | if (active[sat] == 0) // Detect fail on active satellite |
180 | { |
209 | { |
181 | LED_ON; // Failure-LED ON (never goes off again) |
210 | LED_ON; // Failure-LED ON (never goes off again) |
182 | for (i=0;i<8;i++) // Select lowest active satellite |
211 | for (i=0;i<4;i++) // Select lowest active satellite |
183 | { |
212 | { |
184 | if (active[i] == 1) |
213 | if (active[i] == 1) |
- | 214 | { |
|
- | 215 | sat = i; |
|
185 | { |
216 | SelectSat(sat); |
- | 217 | break; |
|
186 | sat = i; |
218 | } |
- | 219 | } |