Rev 1612 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1612 | Rev 1821 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | /*####################################################################################### |
1 | /*####################################################################################### |
2 | Decodieren eines RC Summen Signals oder Spektrum Empfänger-Satellit |
2 | Decodieren eines RC Summen Signals oder Spektrum Empfänger-Satellit |
3 | #######################################################################################*/ |
3 | #######################################################################################*/ |
Line 4... | Line 4... | ||
4 | 4 | ||
Line 5... | Line 5... | ||
5 | #include "Spectrum.h" |
5 | #include "Spectrum.h" |
Line 6... | Line 6... | ||
6 | 6 | ||
7 | //--------------------------------------------------------------// |
7 | //--------------------------------------------------------------// |
8 | - | ||
9 | //--------------------------------------------------------------// |
8 | |
10 | void SpektrumBinding(void) |
9 | //--------------------------------------------------------------// |
11 | { |
10 | void SpektrumBinding(void) { |
12 | unsigned int timerTimeout = SetDelay(10000); // Timeout 10 sec. |
11 | unsigned int timerTimeout = SetDelay(10000); // Timeout 10 sec. |
13 | unsigned char connected = 0; |
12 | unsigned char connected = 0; |
14 | unsigned int delaycounter; |
13 | unsigned int delaycounter; |
15 | 14 | ||
16 | UCSR1B &= ~(1 << RXCIE1); // disable rx-interrupt |
15 | UCSR1B &= ~(1 << RXCIE1); // disable rx-interrupt |
17 | UCSR1B &= ~(1<<RXEN1); // disable Uart-Rx |
16 | UCSR1B &= ~(1 << RXEN1); // disable Uart-Rx |
18 | PORTD &= ~(1 << PORTD2); // disable pull-up |
17 | PORTD &= ~(1 << PORTD2); // disable pull-up |
19 | 18 | ||
20 | printf("\n\rPlease connect Spektrum receiver for binding NOW..."); |
19 | printf("\n\rPlease connect Spektrum receiver for binding NOW..."); |
21 | 20 | ||
22 | while(!CheckDelay(timerTimeout)) |
- | |
23 | { |
- | |
24 | if (PIND & (1 << PORTD2)) { timerTimeout = SetDelay(90); connected = 1; break; } |
21 | while (!CheckDelay(timerTimeout)) { |
25 | } |
- | |
26 | - | ||
27 | if (connected) |
22 | if (PIND & (1 << PORTD2)) { |
28 | { |
- | |
29 | 23 | timerTimeout = SetDelay(90); |
|
30 | printf("ok.\n\r"); |
- | |
31 | DDRD |= (1 << DDD2); // Rx as output |
- | |
32 | - | ||
33 | while(!CheckDelay(timerTimeout)); // delay after startup of RX |
24 | connected = 1; |
34 | for (delaycounter = 0; delaycounter < 100; delaycounter++) PORTD |= (1 << PORTD2); |
- | |
35 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
- | |
36 | - | ||
37 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
- | |
38 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2); |
- | |
39 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
- | |
40 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
- | |
41 | - | ||
42 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
- | |
43 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2); |
25 | break; |
44 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
26 | } |
45 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
- | |
46 | - | ||
47 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
27 | } |
48 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2); |
- | |
49 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
- | |
50 | 28 | ||
51 | } |
- | |
52 | else |
- | |
53 | { printf("Timeout.\n\r"); |
- | |
54 | - | ||
55 | 29 | if (connected) { |
|
56 | } |
- | |
Line -... | Line 30... | ||
- | 30 | ||
- | 31 | printf("ok.\n\r"); |
|
- | 32 | DDRD |= (1 << DDD2); // Rx as output |
|
- | 33 | ||
- | 34 | while (!CheckDelay(timerTimeout)) |
|
- | 35 | ; // delay after startup of RX |
|
- | 36 | for (delaycounter = 0; delaycounter < 100; delaycounter++) |
|
- | 37 | PORTD |= (1 << PORTD2); |
|
- | 38 | for (delaycounter = 0; delaycounter < 400; delaycounter++) |
|
- | 39 | PORTD &= ~(1 << PORTD2); |
|
- | 40 | ||
- | 41 | for (delaycounter = 0; delaycounter < 10; delaycounter++) |
|
- | 42 | PORTD |= (1 << PORTD2); |
|
- | 43 | for (delaycounter = 0; delaycounter < 10; delaycounter++) |
|
- | 44 | PORTD &= ~(1 << PORTD2); |
|
- | 45 | for (delaycounter = 0; delaycounter < 400; delaycounter++) |
|
- | 46 | PORTD |= (1 << PORTD2); |
|
- | 47 | for (delaycounter = 0; delaycounter < 400; delaycounter++) |
|
- | 48 | PORTD &= ~(1 << PORTD2); |
|
- | 49 | ||
- | 50 | for (delaycounter = 0; delaycounter < 10; delaycounter++) |
|
- | 51 | PORTD |= (1 << PORTD2); |
|
- | 52 | for (delaycounter = 0; delaycounter < 10; delaycounter++) |
|
- | 53 | PORTD &= ~(1 << PORTD2); |
|
- | 54 | for (delaycounter = 0; delaycounter < 400; delaycounter++) |
|
- | 55 | PORTD |= (1 << PORTD2); |
|
- | 56 | for (delaycounter = 0; delaycounter < 400; delaycounter++) |
|
- | 57 | PORTD &= ~(1 << PORTD2); |
|
- | 58 | ||
- | 59 | for (delaycounter = 0; delaycounter < 10; delaycounter++) |
|
- | 60 | PORTD |= (1 << PORTD2); |
|
- | 61 | for (delaycounter = 0; delaycounter < 10; delaycounter++) |
|
- | 62 | PORTD &= ~(1 << PORTD2); |
|
- | 63 | for (delaycounter = 0; delaycounter < 400; delaycounter++) |
|
- | 64 | PORTD |= (1 << PORTD2); |
|
- | 65 | ||
- | 66 | } else { |
|
- | 67 | printf("Timeout.\n\r"); |
|
- | 68 | ||
- | 69 | } |
|
57 | 70 | ||
58 | DDRD &= ~(1 << DDD2); // RX as input |
71 | DDRD &= ~(1 << DDD2); // RX as input |
Line 59... | Line 72... | ||
59 | PORTD &= ~(1 << PORTD2); |
72 | PORTD &= ~(1 << PORTD2); |
60 | 73 | ||
61 | Uart1Init(); // init Uart again |
74 | Uart1Init(); // init Uart again |
62 | } |
75 | } |
63 | 76 | ||
64 | //############################################################################ |
77 | //############################################################################ |
65 | // zum Decodieren des Spektrum Satelliten wird USART1 benutzt. |
78 | // zum Decodieren des Spektrum Satelliten wird USART1 benutzt. |
66 | // USART1 initialisation from killagreg |
79 | // USART1 initialisation from killagreg |
67 | void Uart1Init(void) |
80 | void Uart1Init(void) |
68 | //############################################################################ |
81 | //############################################################################ |
69 | { |
82 | { |
70 | // -- Start of USART1 initialisation for Spekturm seriell-mode |
83 | // -- Start of USART1 initialisation for Spekturm seriell-mode |
71 | // USART1 Control and Status Register A, B, C and baud rate register |
84 | // USART1 Control and Status Register A, B, C and baud rate register |
72 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * 115200) - 1); |
85 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK / (8 * 115200) - 1); |
Line 80... | Line 93... | ||
80 | // set RXD1 (PD2) as an input pin |
93 | // set RXD1 (PD2) as an input pin |
81 | PORTD |= (1 << PORTD2); |
94 | PORTD |= (1 << PORTD2); |
82 | DDRD &= ~(1 << DDD2); |
95 | DDRD &= ~(1 << DDD2); |
83 | // USART0 Baud Rate Register |
96 | // USART0 Baud Rate Register |
84 | // set clock divider |
97 | // set clock divider |
85 | 98 | ||
86 | UBRR1H = (uint8_t)(ubrr>>8); |
99 | UBRR1H = (uint8_t) (ubrr >> 8); |
87 | UBRR1L = (uint8_t)ubrr; |
100 | UBRR1L = (uint8_t) ubrr; |
88 | // enable double speed operation |
101 | // enable double speed operation |
89 | UCSR1A |= (1 << U2X1); |
102 | UCSR1A |= (1 << U2X1); |
90 | // enable receiver and transmitter |
103 | // enable receiver and transmitter |
91 | //UCSR1B = (1<<RXEN1)|(1<<TXEN1); |
104 | //UCSR1B = (1<<RXEN1)|(1<<TXEN1); |
Line 92... | Line -... | ||
92 | - | ||
93 | - | ||
94 | 105 | ||
95 | 106 | ||
96 | UCSR1B = (1<<RXEN1); |
107 | UCSR1B = (1 << RXEN1); |
97 | // set asynchronous mode |
108 | // set asynchronous mode |
98 | UCSR1C &= ~(1 << UMSEL11); |
109 | UCSR1C &= ~(1 << UMSEL11); |
99 | UCSR1C &= ~(1 << UMSEL10); |
110 | UCSR1C &= ~(1 << UMSEL10); |
100 | // no parity |
111 | // no parity |
101 | UCSR1C &= ~(1 << UPM11); |
112 | UCSR1C &= ~(1 << UPM11); |
102 | UCSR1C &= ~(1 << UPM10); |
113 | UCSR1C &= ~(1 << UPM10); |
103 | // 1 stop bit |
114 | // 1 stop bit |
104 | UCSR1C &= ~(1 << USBS1); |
115 | UCSR1C &= ~(1 << USBS1); |
105 | // 8-bit |
116 | // 8-bit |
106 | UCSR1B &= ~(1 << UCSZ12); |
117 | UCSR1B &= ~(1 << UCSZ12); |
107 | UCSR1C |= (1 << UCSZ11); |
118 | UCSR1C |= (1 << UCSZ11); |
108 | UCSR1C |= (1 << UCSZ10); |
119 | UCSR1C |= (1 << UCSZ10); |
- | 120 | // flush receive buffer explicit |
|
109 | // flush receive buffer explicit |
121 | while (UCSR1A & (1 << RXC1)) |
110 | while(UCSR1A & (1<<RXC1)) UDR1; |
122 | UDR1; |
111 | // enable RX-interrupts at the end |
123 | // enable RX-interrupts at the end |
112 | UCSR1B |= (1 << RXCIE1); |
124 | UCSR1B |= (1 << RXCIE1); |
113 | // -- End of USART1 initialisation |
125 | // -- End of USART1 initialisation |
114 | return; |
126 | return; |
115 | } |
127 | } |
116 | 128 | ||
117 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
129 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
118 | // + Copyright (c) Rainer Walther |
130 | // + Copyright (c) Rainer Walther |
119 | // + RC-routines from original MK rc.c (c) H&I |
131 | // + RC-routines from original MK rc.c (c) H&I |
Line 184... | Line 196... | ||
184 | // |
196 | // |
185 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
197 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
Line 186... | Line 198... | ||
186 | 198 | ||
187 | //############################################################################ |
199 | //############################################################################ |
188 | //Diese Routine startet und inizialisiert den USART1 für seriellen Spektrum satellite reciever |
200 | //Diese Routine startet und inizialisiert den USART1 für seriellen Spektrum satellite reciever |
189 | SIGNAL(USART1_RX_vect) |
201 | SIGNAL( USART1_RX_vect) |
190 | //############################################################################ |
202 | //############################################################################ |
191 | { |
203 | { |
- | 204 | static unsigned int Sync = 0, FrameCnt = 0, ByteHigh = 0, ReSync = 1, Frame2 = |
|
192 | static unsigned int Sync=0, FrameCnt=0, ByteHigh=0, ReSync=1, Frame2=0, FrameTimer; |
205 | 0, FrameTimer; |
193 | unsigned int Channel, index; |
206 | unsigned int Channel, index; |
194 | signed int signal, tmp; |
207 | signed int signal, tmp; |
195 | int bCheckDelay; |
208 | int bCheckDelay; |
196 | uint8_t c; |
209 | uint8_t c; |
197 | 210 | ||
198 | c = UDR1; // get data byte |
211 | c = UDR1; // get data byte |
199 | 212 | ||
200 | if (ReSync == 1) |
- | |
201 | { |
213 | if (ReSync == 1) { |
202 | // wait for beginning of new frame |
214 | // wait for beginning of new frame |
203 | ReSync = 0; |
215 | ReSync = 0; |
204 | 216 | ||
205 | FrameTimer = SetDelay(7); // minimum 7ms zwischen den frames |
217 | FrameTimer = SetDelay(7); // minimum 7ms zwischen den frames |
206 | FrameCnt = 0; |
218 | FrameCnt = 0; |
207 | Sync = 0; |
219 | Sync = 0; |
208 | ByteHigh = 0; |
- | |
209 | } |
220 | ByteHigh = 0; |
210 | else |
- | |
211 | { |
221 | } else { |
212 | bCheckDelay = CheckDelay(FrameTimer); |
222 | bCheckDelay = CheckDelay(FrameTimer); |
213 | if ( Sync == 0 ) |
- | |
214 | { |
223 | if (Sync == 0) { |
215 | if(bCheckDelay) |
- | |
216 | { |
224 | if (bCheckDelay) { |
217 | // nach einer Pause von mind. 7ms erstes Sync-Character gefunden |
225 | // nach einer Pause von mind. 7ms erstes Sync-Character gefunden |
218 | // Zeichen ignorieren, da Bedeutung unbekannt |
226 | // Zeichen ignorieren, da Bedeutung unbekannt |
219 | Sync = 1; |
227 | Sync = 1; |
- | 228 | FrameCnt++; |
|
- | 229 | } else { |
|
- | 230 | // Zeichen kam vor Ablauf der 7ms Sync-Pause |
|
220 | FrameCnt ++; |
231 | // warten auf erstes Sync-Zeichen |
- | 232 | } |
|
- | 233 | } else if ((Sync == 1) && !bCheckDelay) { |
|
221 | } |
234 | // zweites Sync-Character ignorieren, Bedeutung unbekannt |
222 | else |
235 | Sync = 2; |
- | 236 | FrameCnt++; |
|
- | 237 | } else if ((Sync == 2) && !bCheckDelay) { |
|
- | 238 | // Datenbyte high |
|
- | 239 | ByteHigh = c; |
|
- | 240 | ||
- | 241 | if (FrameCnt == 2) { |
|
223 | { |
242 | // is 1st Byte of Channel-data |
- | 243 | // Frame 1 with Channel 1-7 comming next |
|
- | 244 | Frame2 = 0; |
|
224 | // Zeichen kam vor Ablauf der 7ms Sync-Pause |
245 | if (ByteHigh & 0x80) { |
- | 246 | // DS9: Frame 2 with Channel 8-9 comming next |
|
- | 247 | Frame2 = 1; |
|
225 | // warten auf erstes Sync-Zeichen |
248 | } |
226 | } |
249 | } |
- | 250 | Sync = 3; |
|
227 | } |
251 | FrameCnt++; |
228 | else if((Sync == 1) && !bCheckDelay) |
252 | } else if ((Sync == 3) && !bCheckDelay) { |
- | 253 | // Datenbyte low |
|
229 | { |
254 | |
230 | // zweites Sync-Character ignorieren, Bedeutung unbekannt |
255 | // High-Byte for next channel comes next |
231 | Sync = 2; |
256 | Sync = 2; |
232 | FrameCnt ++; |
257 | FrameCnt++; |
233 | } |
258 | |
234 | else if((Sync == 2) && !bCheckDelay) |
259 | index = (ByteHigh >> 2) & 0x0f; |
235 | { |
260 | index++; |
236 | // Datenbyte high |
261 | Channel = (ByteHigh << 8) | c; |
- | 262 | signal = Channel & 0x3ff; |
|
- | 263 | signal -= 0x200; // Offset, range 0x000..0x3ff? |
|
237 | ByteHigh = c; |
264 | signal = signal / 3; // scaling to fit PPM resolution |
238 | 265 | ||
239 | if (FrameCnt == 2) |
- | |
240 | { |
266 | if (index >= 0 && index <= 10) { |
241 | // is 1st Byte of Channel-data |
267 | // Stabiles Signal |
242 | // Frame 1 with Channel 1-7 comming next |
268 | if (abs(signal - PPM_in[index]) < 6) { |
243 | Frame2 = 0; |
269 | if (SenderOkay < 200) |
244 | if(ByteHigh & 0x80) |
270 | SenderOkay += 10; |
245 | { |
- | |
246 | // DS9: Frame 2 with Channel 8-9 comming next |
271 | else |
247 | Frame2 = 1; |
272 | SenderOkay = 200; |
248 | } |
- | |
249 | } |
- | |
250 | Sync = 3; |
- | |
251 | FrameCnt ++; |
- | |
252 | } |
273 | } |
253 | else if((Sync == 3) && !bCheckDelay) |
- | |
254 | { |
274 | tmp = (3 * (PPM_in[index]) + signal) / 4; |
255 | // Datenbyte low |
- | |
256 | - | ||
257 | // High-Byte for next channel comes next |
275 | if (tmp > signal + 1) |
258 | Sync = 2; |
- | |
259 | FrameCnt ++; |
- | |
260 | 276 | tmp--; |
|
261 | index = (ByteHigh >> 2) & 0x0f; |
277 | else if (tmp < signal - 1) |
262 | index ++; |
278 | tmp++; |
263 | Channel = (ByteHigh << 8) | c; |
- | |
264 | signal = Channel & 0x3ff; |
- | |
265 | signal -= 0x200; // Offset, range 0x000..0x3ff? |
- | |
266 | signal = signal/3; // scaling to fit PPM resolution |
- | |
267 | 279 | if (SenderOkay >= 180) |
|
268 | if(index >= 0 && index <= 10) |
280 | PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3; |
269 | { |
- | |
270 | // Stabiles Signal |
- | |
271 | if(abs(signal - PPM_in[index]) < 6) { if(SenderOkay < 200) SenderOkay += 10; else SenderOkay = 200;} |
- | |
272 | tmp = (3 * (PPM_in[index]) + signal) / 4; |
- | |
273 | if(tmp > signal+1) tmp--; else |
- | |
274 | if(tmp < signal-1) tmp++; |
- | |
275 | if(SenderOkay >= 180) PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3; |
281 | else |
276 | else PPM_diff[index] = 0; |
282 | PPM_diff[index] = 0; |
277 | PPM_in[index] = tmp; |
283 | PPM_in[index] = tmp; |
- | 284 | } |
|
- | 285 | } else { |
|
- | 286 | // hier stimmt was nicht: neu synchronisieren |
|
- | 287 | ReSync = 1; |
|
- | 288 | FrameCnt = 0; |
|
278 | } |
289 | Frame2 = 0; |
279 | } |
- | |
280 | else |
- | |
281 | { |
- | |
282 | // hier stimmt was nicht: neu synchronisieren |
- | |
283 | ReSync = 1; |
- | |
284 | FrameCnt = 0; |
- | |
285 | Frame2 = 0; |
- | |
286 | } |
290 | } |
287 | 291 | ||
288 | // 16 Bytes per frame |
292 | // 16 Bytes per frame |
289 | if(FrameCnt >= 16) |
- | |
290 | { |
293 | if (FrameCnt >= 16) { |
291 | // Frame complete |
294 | // Frame complete |
292 | if(Frame2 == 0) |
- | |
293 | { |
295 | if (Frame2 == 0) { |
294 | // Null bedeutet: Neue Daten |
296 | // Null bedeutet: Neue Daten |
295 | // nur beim ersten Frame (CH 0-7) setzen |
297 | // nur beim ersten Frame (CH 0-7) setzen |
296 | NewPpmData = 0; |
298 | NewPpmData = 0; |
297 | } |
- | |
298 | - | ||
299 | // new frame next, nach fruehestens 7ms erwartet |
- | |
300 | FrameCnt = 0; |
- | |
301 | Frame2 = 0; |
- | |
302 | Sync = 0; |
- | |
303 | } |
- | |
304 | // Zeit bis zum nächsten Zeichen messen |
- | |
305 | FrameTimer = SetDelay(7); |
- | |
306 | } |
- | |
Line -... | Line 299... | ||
- | 299 | } |
|
- | 300 | ||
- | 301 | // new frame next, nach fruehestens 7ms erwartet |
|
- | 302 | FrameCnt = 0; |
|
- | 303 | Frame2 = 0; |
|
- | 304 | Sync = 0; |
|
- | 305 | } |
|
- | 306 | // Zeit bis zum nächsten Zeichen messen |
|
- | 307 | FrameTimer = SetDelay(7); |