Rev 1377 | Rev 1419 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1377 | Rev 1391 | ||
---|---|---|---|
Line 12... | Line 12... | ||
12 | void SpektrumBinding(void) |
12 | void SpektrumBinding(void) |
13 | { |
13 | { |
14 | unsigned int timerTimeout = SetDelay(10000); // Timeout 10 sec. |
14 | unsigned int timerTimeout = SetDelay(10000); // Timeout 10 sec. |
15 | unsigned char connected = 0; |
15 | unsigned char connected = 0; |
16 | unsigned int delaycounter; |
16 | unsigned int delaycounter; |
17 | 17 | ||
18 | UCSR1B &= ~(1 << RXCIE1); // disable rx-interrupt |
18 | UCSR1B &= ~(1 << RXCIE1); // disable rx-interrupt |
19 | UCSR1B &= ~(1<<RXEN1); // disable Uart-Rx |
19 | UCSR1B &= ~(1<<RXEN1); // disable Uart-Rx |
20 | PORTD &= ~(1 << PORTD2); // disable pull-up |
20 | PORTD &= ~(1 << PORTD2); // disable pull-up |
21 | 21 | ||
22 | printf("\n\rPlease connect Spektrum receiver for binding NOW..."); |
22 | printf("\n\rPlease connect Spektrum receiver for binding NOW..."); |
23 | 23 | ||
24 | while(!CheckDelay(timerTimeout)) |
24 | while(!CheckDelay(timerTimeout)) |
25 | { |
25 | { |
26 | if (PIND & (1 << PORTD2)) { timerTimeout = SetDelay(90); connected = 1; break; } |
26 | if (PIND & (1 << PORTD2)) { timerTimeout = SetDelay(90); connected = 1; break; } |
27 | } |
27 | } |
28 | 28 | ||
29 | if (connected) |
29 | if (connected) |
30 | { |
30 | { |
31 | 31 | ||
32 | printf("ok.\n\r"); |
32 | printf("ok.\n\r"); |
33 | DDRD |= (1 << DDD2); // Rx as output |
33 | DDRD |= (1 << DDD2); // Rx as output |
Line 34... | Line 34... | ||
34 | 34 | ||
35 | while(!CheckDelay(timerTimeout)); // delay after startup of RX |
35 | while(!CheckDelay(timerTimeout)); // delay after startup of RX |
36 | for (delaycounter = 0; delaycounter < 100; delaycounter++) PORTD |= (1 << PORTD2); |
36 | for (delaycounter = 0; delaycounter < 100; delaycounter++) PORTD |= (1 << PORTD2); |
37 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
37 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
38 | 38 | ||
39 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
39 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
40 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2); |
40 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2); |
41 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
41 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
42 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
42 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
43 | 43 | ||
44 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
44 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
45 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2); |
45 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2); |
46 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
46 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
Line 47... | Line 47... | ||
47 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
47 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
48 | 48 | ||
49 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
49 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
50 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2); |
50 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2); |
51 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
51 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
52 | 52 | ||
53 | } |
53 | } |
54 | else |
54 | else |
55 | { printf("Timeout.\n\r"); |
55 | { printf("Timeout.\n\r"); |
56 | 56 | ||
57 | 57 | ||
58 | } |
58 | } |
59 | 59 | ||
Line 60... | Line 60... | ||
60 | DDRD &= ~(1 << DDD2); // RX as input |
60 | DDRD &= ~(1 << DDD2); // RX as input |
61 | PORTD &= ~(1 << PORTD2); |
61 | PORTD &= ~(1 << PORTD2); |
Line 62... | Line 62... | ||
62 | 62 | ||
Line 82... | Line 82... | ||
82 | // set RXD1 (PD2) as an input pin |
82 | // set RXD1 (PD2) as an input pin |
83 | PORTD |= (1 << PORTD2); |
83 | PORTD |= (1 << PORTD2); |
84 | DDRD &= ~(1 << DDD2); |
84 | DDRD &= ~(1 << DDD2); |
85 | // USART0 Baud Rate Register |
85 | // USART0 Baud Rate Register |
86 | // set clock divider |
86 | // set clock divider |
87 | 87 | ||
88 | UBRR1H = (uint8_t)(ubrr>>8); |
88 | UBRR1H = (uint8_t)(ubrr>>8); |
89 | UBRR1L = (uint8_t)ubrr; |
89 | UBRR1L = (uint8_t)ubrr; |
90 | // enable double speed operation |
90 | // enable double speed operation |
91 | UCSR1A |= (1 << U2X1); |
91 | UCSR1A |= (1 << U2X1); |
92 | // enable receiver and transmitter |
92 | // enable receiver and transmitter |
93 | //UCSR1B = (1<<RXEN1)|(1<<TXEN1); |
93 | //UCSR1B = (1<<RXEN1)|(1<<TXEN1); |
94 | 94 | ||
95 | UCSR1B = (1<<RXEN1); |
95 | UCSR1B = (1<<RXEN1); |
96 | // set asynchronous mode |
96 | // set asynchronous mode |
97 | UCSR1C &= ~(1 << UMSEL11); |
97 | UCSR1C &= ~(1 << UMSEL11); |
98 | UCSR1C &= ~(1 << UMSEL10); |
98 | UCSR1C &= ~(1 << UMSEL10); |
99 | // no parity |
99 | // no parity |
Line 110... | Line 110... | ||
110 | // enable RX-interrupts at the end |
110 | // enable RX-interrupts at the end |
111 | UCSR1B |= (1 << RXCIE1); |
111 | UCSR1B |= (1 << RXCIE1); |
112 | // -- End of USART1 initialisation |
112 | // -- End of USART1 initialisation |
113 | return; |
113 | return; |
114 | } |
114 | } |
115 | 115 | ||
116 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
116 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
117 | // + Copyright (c) Rainer Walther |
117 | // + Copyright (c) Rainer Walther |
118 | // + RC-routines from original MK rc.c (c) H&I |
118 | // + RC-routines from original MK rc.c (c) H&I |
119 | // + Useful infos from Walter: http://www.rcgroups.com/forums/showthread.php?t=714299&page=2 |
119 | // + Useful infos from Walter: http://www.rcgroups.com/forums/showthread.php?t=714299&page=2 |
120 | // + only for non-profit use |
120 | // + only for non-profit use |
Line 158... | Line 158... | ||
158 | // byte5: and byte6: channel data |
158 | // byte5: and byte6: channel data |
159 | // byte7: and byte8: channel data |
159 | // byte7: and byte8: channel data |
160 | // byte9: and byte10: channel data |
160 | // byte9: and byte10: channel data |
161 | // byte11: and byte12: channel data |
161 | // byte11: and byte12: channel data |
162 | // byte13: and byte14: channel data |
162 | // byte13: and byte14: channel data |
163 | // byte15: and byte16: channel data |
163 | // byte15: and byte16: channel data |
164 | // 2nd Frame: |
164 | // 2nd Frame: |
165 | // byte1: unknown |
165 | // byte1: unknown |
166 | // byte2: unknown |
166 | // byte2: unknown |
167 | // byte3: and byte4: channel data |
167 | // byte3: and byte4: channel data |
168 | // byte5: and byte6: channel data |
168 | // byte5: and byte6: channel data |
Line 205... | Line 205... | ||
205 | FrameCnt = 0; |
205 | FrameCnt = 0; |
206 | Sync = 0; |
206 | Sync = 0; |
207 | ByteHigh = 0; |
207 | ByteHigh = 0; |
208 | } |
208 | } |
209 | else |
209 | else |
210 | { |
210 | { |
211 | if(!SpektrumTimer) bCheckDelay = 1; else bCheckDelay = 0;//CheckDelay(FrameTimer); |
211 | if(!SpektrumTimer) bCheckDelay = 1; else bCheckDelay = 0;//CheckDelay(FrameTimer); |
212 | if ( Sync == 0 ) |
212 | if ( Sync == 0 ) |
213 | { |
213 | { |
214 | if(bCheckDelay) |
214 | if(bCheckDelay) |
215 | { |
215 | { |
Line 249... | Line 249... | ||
249 | if(ByteHigh & 0x80) |
249 | if(ByteHigh & 0x80) |
250 | { |
250 | { |
251 | // DS9: Frame 2 with Channel 8-9 comming next |
251 | // DS9: Frame 2 with Channel 8-9 comming next |
252 | Frame2 = 1; |
252 | Frame2 = 1; |
253 | } |
253 | } |
254 | } |
254 | } |
255 | Sync = 3; |
255 | Sync = 3; |
256 | FrameCnt ++; |
256 | FrameCnt ++; |
257 | } |
257 | } |
258 | else if((Sync == 3) && !bCheckDelay) |
258 | else if((Sync == 3) && !bCheckDelay) |
259 | { |
259 | { |
Line 266... | Line 266... | ||
266 | index++; |
266 | index++; |
267 | Channel = ((unsigned int)ByteHigh << 8) | c; |
267 | Channel = ((unsigned int)ByteHigh << 8) | c; |
268 | signal = Channel & 0x3ff; |
268 | signal = Channel & 0x3ff; |
269 | signal -= 0x200; // Offset, range 0x000..0x3ff? |
269 | signal -= 0x200; // Offset, range 0x000..0x3ff? |
270 | signal = signal/3; // scaling to fit PPM resolution |
270 | signal = signal/3; // scaling to fit PPM resolution |
271 | 271 | ||
272 | if(index >= 0 && index <= 13) |
272 | if(index > 0 && index < 13) |
273 | { |
273 | { |
274 | // Stabiles Signal |
274 | // Stabiles Signal |
275 | if(abs(signal - PPM_in[index]) < 6) |
275 | if(abs(signal - PPM_in[index]) < 6) |
276 | { |
276 | { |
277 | if(SenderOkay < 200) SenderOkay += 10; |
277 | if(SenderOkay < 200) SenderOkay += 10; |
278 | else |
278 | else |
279 | { |
279 | { |
280 | SenderOkay = 200; |
280 | SenderOkay = 200; |
281 | TIMSK1 &= ~_BV(ICIE1); // disable PPM-Input |
281 | TIMSK1 &= ~_BV(ICIE1); // disable PPM-Input |
282 | } |
282 | } |
283 | } |
283 | } |
284 | tmp = (3 * (PPM_in[index]) + signal) / 4; |
284 | tmp = (3 * (PPM_in[index]) + signal) / 4; |
285 | if(tmp > signal+1) tmp--; else |
285 | if(tmp > signal+1) tmp--; else |
286 | if(tmp < signal-1) tmp++; |
286 | if(tmp < signal-1) tmp++; |
287 | if(SenderOkay >= 180) PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3; |
287 | if(SenderOkay >= 180) PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3; |
288 | else PPM_diff[index] = 0; |
288 | else PPM_diff[index] = 0; |
289 | PPM_in[index] = tmp; |
289 | PPM_in[index] = tmp; |
290 | } |
290 | } |
291 | else if(index > 17) ReSync = 1; // hier stimmt was nicht: neu synchronisieren |
291 | else if(index > 17) ReSync = 1; // hier stimmt was nicht: neu synchronisieren |
292 | } |
292 | } |
Line 297... | Line 297... | ||
297 | FrameCnt = 0; |
297 | FrameCnt = 0; |
298 | Frame2 = 0; |
298 | Frame2 = 0; |
299 | // new frame next, nach fruehestens 7ms erwartet |
299 | // new frame next, nach fruehestens 7ms erwartet |
300 | SpektrumTimer = MIN_FRAMEGAP; |
300 | SpektrumTimer = MIN_FRAMEGAP; |
301 | } |
301 | } |
302 | 302 | ||
303 | // 16 Bytes eingetroffen -> Komplett |
303 | // 16 Bytes eingetroffen -> Komplett |
304 | if(FrameCnt >= 16) |
304 | if(FrameCnt >= 16) |
305 | { |
305 | { |
306 | // Frame complete |
306 | // Frame complete |
307 | if(Frame2 == 0) |
307 | if(Frame2 == 0) |
308 | { |
308 | { |
309 | // Null bedeutet: Neue Daten |
309 | // Null bedeutet: Neue Daten |
310 | // nur beim ersten Frame (CH 0-7) setzen |
310 | // nur beim ersten Frame (CH 0-7) setzen |
311 | if(!ReSync) NewPpmData = 0; |
311 | if(!ReSync) NewPpmData = 0; |
312 | } |
312 | } |
313 | FrameCnt = 0; |
313 | FrameCnt = 0; |
314 | Frame2 = 0; |
314 | Frame2 = 0; |
315 | Sync = 0; |
315 | Sync = 0; |
316 | SpektrumTimer = MIN_FRAMEGAP; |
316 | SpektrumTimer = MIN_FRAMEGAP; |
317 | } |
317 | } |
318 | } |
318 | } |
319 | } |
319 | } |