Rev 1424 | Rev 1431 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1424 | Rev 1426 | ||
---|---|---|---|
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 | #######################################################################################*/ |
4 | 4 | ||
5 | #include "Spectrum.h" |
5 | #include "Spektrum.h" |
6 | #include "main.h" |
6 | #include "main.h" |
7 | unsigned char SpektrumTimer = 0; |
7 | unsigned char SpektrumTimer = 0; |
8 | 8 | ||
9 | //--------------------------------------------------------------// |
9 | //--------------------------------------------------------------// |
10 | 10 | ||
11 | //--------------------------------------------------------------// |
11 | //--------------------------------------------------------------// |
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 |
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); |
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 | ||
60 | DDRD &= ~(1 << DDD2); // RX as input |
60 | DDRD &= ~(1 << DDD2); // RX as input |
61 | PORTD &= ~(1 << PORTD2); |
61 | PORTD &= ~(1 << PORTD2); |
62 | 62 | ||
63 | SpektrumUartInit(); // init Uart again |
63 | SpektrumUartInit(); // init Uart again |
64 | } |
64 | } |
65 | 65 | ||
66 | //############################################################################ |
66 | //############################################################################ |
67 | // USART1 initialisation from killagreg |
67 | // USART1 initialisation from killagreg |
68 | void SpektrumUartInit(void) |
68 | void SpektrumUartInit(void) |
69 | //############################################################################ |
69 | //############################################################################ |
70 | { |
70 | { |
71 | // -- Start of USART1 initialisation for Spekturm seriell-mode |
71 | // -- Start of USART1 initialisation for Spekturm seriell-mode |
72 | // USART1 Control and Status Register A, B, C and baud rate register |
72 | // USART1 Control and Status Register A, B, C and baud rate register |
73 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * 115200) - 1); |
73 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * 115200) - 1); |
74 | // disable RX-Interrupt |
74 | // disable RX-Interrupt |
75 | UCSR1B &= ~(1 << RXCIE1); |
75 | UCSR1B &= ~(1 << RXCIE1); |
76 | // disable TX-Interrupt |
76 | // disable TX-Interrupt |
77 | UCSR1B &= ~(1 << TXCIE1); |
77 | UCSR1B &= ~(1 << TXCIE1); |
78 | // disable DRE-Interrupt |
78 | // disable DRE-Interrupt |
79 | UCSR1B &= ~(1 << UDRIE1); |
79 | UCSR1B &= ~(1 << UDRIE1); |
80 | // set direction of RXD1 and TXD1 pins |
80 | // set direction of RXD1 and TXD1 pins |
81 | // set RXD1 (PD2) as an input pin |
81 | // set RXD1 (PD2) as an input pin |
82 | PORTD |= (1 << PORTD2); |
82 | PORTD |= (1 << PORTD2); |
83 | DDRD &= ~(1 << DDD2); |
83 | DDRD &= ~(1 << DDD2); |
84 | // USART0 Baud Rate Register |
84 | // USART0 Baud Rate Register |
85 | // set clock divider |
85 | // set clock divider |
86 | 86 | ||
87 | UBRR1H = (uint8_t)(ubrr>>8); |
87 | UBRR1H = (uint8_t)(ubrr>>8); |
88 | UBRR1L = (uint8_t)ubrr; |
88 | UBRR1L = (uint8_t)ubrr; |
89 | // enable double speed operation |
89 | // enable double speed operation |
90 | UCSR1A |= (1 << U2X1); |
90 | UCSR1A |= (1 << U2X1); |
91 | // enable receiver and transmitter |
91 | // enable receiver and transmitter |
92 | //UCSR1B = (1<<RXEN1)|(1<<TXEN1); |
92 | //UCSR1B = (1<<RXEN1)|(1<<TXEN1); |
93 | 93 | ||
94 | UCSR1B = (1<<RXEN1); |
94 | UCSR1B = (1<<RXEN1); |
95 | // set asynchronous mode |
95 | // set asynchronous mode |
96 | UCSR1C &= ~(1 << UMSEL11); |
96 | UCSR1C &= ~(1 << UMSEL11); |
97 | UCSR1C &= ~(1 << UMSEL10); |
97 | UCSR1C &= ~(1 << UMSEL10); |
98 | // no parity |
98 | // no parity |
99 | UCSR1C &= ~(1 << UPM11); |
99 | UCSR1C &= ~(1 << UPM11); |
100 | UCSR1C &= ~(1 << UPM10); |
100 | UCSR1C &= ~(1 << UPM10); |
101 | // 1 stop bit |
101 | // 1 stop bit |
102 | UCSR1C &= ~(1 << USBS1); |
102 | UCSR1C &= ~(1 << USBS1); |
103 | // 8-bit |
103 | // 8-bit |
104 | UCSR1B &= ~(1 << UCSZ12); |
104 | UCSR1B &= ~(1 << UCSZ12); |
105 | UCSR1C |= (1 << UCSZ11); |
105 | UCSR1C |= (1 << UCSZ11); |
106 | UCSR1C |= (1 << UCSZ10); |
106 | UCSR1C |= (1 << UCSZ10); |
107 | // flush receive buffer explicit |
107 | // flush receive buffer explicit |
108 | while(UCSR1A & (1<<RXC1)) UDR1; |
108 | while(UCSR1A & (1<<RXC1)) UDR1; |
109 | // enable RX-interrupts at the end |
109 | // enable RX-interrupts at the end |
110 | UCSR1B |= (1 << RXCIE1); |
110 | UCSR1B |= (1 << RXCIE1); |
111 | // -- End of USART1 initialisation |
111 | // -- End of USART1 initialisation |
112 | return; |
112 | return; |
113 | } |
113 | } |
114 | 114 | ||
115 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
115 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
116 | // + Copyright (c) Rainer Walther |
116 | // + Copyright (c) Rainer Walther |
117 | // + RC-routines from original MK rc.c (c) H&I |
117 | // + RC-routines from original MK rc.c (c) H&I |
118 | // + Useful infos from Walter: http://www.rcgroups.com/forums/showthread.php?t=714299&page=2 |
118 | // + Useful infos from Walter: http://www.rcgroups.com/forums/showthread.php?t=714299&page=2 |
119 | // + only for non-profit use |
119 | // + only for non-profit use |
120 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
120 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
121 | // |
121 | // |
122 | // 20080808 rw Modified for Spektrum AR6100 (PPM) |
122 | // 20080808 rw Modified for Spektrum AR6100 (PPM) |
123 | // 20080823 rw Add Spektrum satellite receiver on USART1 (644P only) |
123 | // 20080823 rw Add Spektrum satellite receiver on USART1 (644P only) |
124 | // 20081213 rw Add support for Spektrum DS9 Air-Tx-Module (9 channels) |
124 | // 20081213 rw Add support for Spektrum DS9 Air-Tx-Module (9 channels) |
125 | // Replace AR6100-coding with original composit-signal routines |
125 | // Replace AR6100-coding with original composit-signal routines |
126 | // |
126 | // |
127 | // --- |
127 | // --- |
128 | // Entweder Summensignal ODER Spektrum-Receiver anschließen. Nicht beides gleichzeitig betreiben! |
128 | // Entweder Summensignal ODER Spektrum-Receiver anschließen. Nicht beides gleichzeitig betreiben! |
129 | // Binding is not implemented. Bind with external Receiver. |
129 | // Binding is not implemented. Bind with external Receiver. |
130 | // Servo output J3, J4, J5 not serviced |
130 | // Servo output J3, J4, J5 not serviced |
131 | // |
131 | // |
132 | // Anschuß Spektrum Receiver |
132 | // Anschuß Spektrum Receiver |
133 | // Orange: 3V von der FC (keinesfalls an 5V anschließen!) |
133 | // Orange: 3V von der FC (keinesfalls an 5V anschließen!) |
134 | // Schwarz: GND |
134 | // Schwarz: GND |
135 | // Grau: RXD1 (Pin 3) auf 10-Pol FC-Stecker |
135 | // Grau: RXD1 (Pin 3) auf 10-Pol FC-Stecker |
136 | // |
136 | // |
137 | // --- |
137 | // --- |
138 | // Satellite-Reciever connected on USART1: |
138 | // Satellite-Reciever connected on USART1: |
139 | // |
139 | // |
140 | // DX7/DX6i: One data-frame at 115200 baud every 22ms. |
140 | // DX7/DX6i: One data-frame at 115200 baud every 22ms. |
141 | // DX7se: One data-frame at 115200 baud every 11ms. |
141 | // DX7se: One data-frame at 115200 baud every 11ms. |
142 | // byte1: unknown |
142 | // byte1: unknown |
143 | // byte2: unknown |
143 | // byte2: unknown |
144 | // byte3: and byte4: channel data (FLT-Mode) |
144 | // byte3: and byte4: channel data (FLT-Mode) |
145 | // byte5: and byte6: channel data (Roll) |
145 | // byte5: and byte6: channel data (Roll) |
146 | // byte7: and byte8: channel data (Nick) |
146 | // byte7: and byte8: channel data (Nick) |
147 | // byte9: and byte10: channel data (Gier) |
147 | // byte9: and byte10: channel data (Gier) |
148 | // byte11: and byte12: channel data (Gear Switch) |
148 | // byte11: and byte12: channel data (Gear Switch) |
149 | // byte13: and byte14: channel data (Gas) |
149 | // byte13: and byte14: channel data (Gas) |
150 | // byte15: and byte16: channel data (AUX2) |
150 | // byte15: and byte16: channel data (AUX2) |
151 | // |
151 | // |
152 | // DS9 (9 Channel): One data-frame at 115200 baud every 11ms, alternating frame 1/2 for CH1-7 / CH8-9 |
152 | // DS9 (9 Channel): One data-frame at 115200 baud every 11ms, alternating frame 1/2 for CH1-7 / CH8-9 |
153 | // 1st Frame: |
153 | // 1st Frame: |
154 | // byte1: unknown |
154 | // byte1: unknown |
155 | // byte2: unknown |
155 | // byte2: unknown |
156 | // byte3: and byte4: channel data |
156 | // byte3: and byte4: channel data |
157 | // byte5: and byte6: channel data |
157 | // byte5: and byte6: channel data |
158 | // byte7: and byte8: channel data |
158 | // byte7: and byte8: channel data |
159 | // byte9: and byte10: channel data |
159 | // byte9: and byte10: channel data |
160 | // byte11: and byte12: channel data |
160 | // byte11: and byte12: channel data |
161 | // byte13: and byte14: channel data |
161 | // byte13: and byte14: channel data |
162 | // byte15: and byte16: channel data |
162 | // byte15: and byte16: channel data |
163 | // 2nd Frame: |
163 | // 2nd Frame: |
164 | // byte1: unknown |
164 | // byte1: unknown |
165 | // byte2: unknown |
165 | // byte2: unknown |
166 | // byte3: and byte4: channel data |
166 | // byte3: and byte4: channel data |
167 | // byte5: and byte6: channel data |
167 | // byte5: and byte6: channel data |
168 | // byte7: and byte8: 0xffff |
168 | // byte7: and byte8: 0xffff |
169 | // byte9: and byte10: 0xffff |
169 | // byte9: and byte10: 0xffff |
170 | // byte11: and byte12: 0xffff |
170 | // byte11: and byte12: 0xffff |
171 | // byte13: and byte14: 0xffff |
171 | // byte13: and byte14: 0xffff |
172 | // byte15: and byte16: 0xffff |
172 | // byte15: and byte16: 0xffff |
173 | // |
173 | // |
174 | // Each channel data (16 bit= 2byte, first msb, second lsb) is arranged as: |
174 | // Each channel data (16 bit= 2byte, first msb, second lsb) is arranged as: |
175 | // |
175 | // |
176 | // Bits: F 0 C3 C2 C1 C0 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 |
176 | // Bits: F 0 C3 C2 C1 C0 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 |
177 | // |
177 | // |
178 | // 0 means a '0' bit |
178 | // 0 means a '0' bit |
179 | // F: 1 = indicates beginning of 2nd frame for CH8-9 (DS9 only) |
179 | // F: 1 = indicates beginning of 2nd frame for CH8-9 (DS9 only) |
180 | // C3 to C0 is the channel number. 0 to 9 (4 bit, as assigned in the transmitter) |
180 | // C3 to C0 is the channel number. 0 to 9 (4 bit, as assigned in the transmitter) |
181 | // D9 to D0 is the channel data (10 bit) 0xaa..0x200..0x356 for 100% transmitter-travel |
181 | // D9 to D0 is the channel data (10 bit) 0xaa..0x200..0x356 for 100% transmitter-travel |
182 | // |
182 | // |
183 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
183 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
184 | 184 | ||
185 | #define MIN_FRAMEGAP 68 // 7ms |
185 | #define MIN_FRAMEGAP 68 // 7ms |
186 | #define MAX_BYTEGAP 3 // 310us |
186 | #define MAX_BYTEGAP 3 // 310us |
187 | 187 | ||
188 | 188 | ||
189 | 189 | ||
190 | 190 | ||
191 | //############################################################################ |
191 | //############################################################################ |
192 | //Diese Routine startet und inizialisiert den USART1 für seriellen Spektrum satellite reciever |
192 | //Diese Routine startet und inizialisiert den USART1 für seriellen Spektrum satellite reciever |
193 | //SIGNAL(USART1_RX_vect) |
193 | //SIGNAL(USART1_RX_vect) |
194 | //############################################################################ |
194 | //############################################################################ |
195 | void SpektrumParser(unsigned char c) |
195 | void SpektrumParser(unsigned char c) |
196 | { |
196 | { |
197 | static unsigned char Sync=0, FrameCnt=0, ByteHigh=0, ReSync=1, Frame2=0; |
197 | static unsigned char Sync=0, FrameCnt=0, ByteHigh=0, ReSync=1, Frame2=0; |
198 | unsigned int Channel, index; |
198 | unsigned int Channel, index; |
199 | signed int signal, tmp; |
199 | signed int signal, tmp; |
200 | int bCheckDelay; |
200 | int bCheckDelay; |
201 | 201 | ||
202 | // c = UDR1; // get data byte |
202 | // c = UDR1; // get data byte |
203 | if(ReSync == 1) |
203 | if(ReSync == 1) |
204 | { |
204 | { |
205 | // wait for beginning of new frame |
205 | // wait for beginning of new frame |
206 | ReSync = 0; |
206 | ReSync = 0; |
207 | SpektrumTimer = MIN_FRAMEGAP; |
207 | SpektrumTimer = MIN_FRAMEGAP; |
208 | FrameCnt = 0; |
208 | FrameCnt = 0; |
209 | Sync = 0; |
209 | Sync = 0; |
210 | ByteHigh = 0; |
210 | ByteHigh = 0; |
211 | } |
211 | } |
212 | else |
212 | else |
213 | { |
213 | { |
214 | if(!SpektrumTimer) bCheckDelay = 1; else bCheckDelay = 0;//CheckDelay(FrameTimer); |
214 | if(!SpektrumTimer) bCheckDelay = 1; else bCheckDelay = 0;//CheckDelay(FrameTimer); |
215 | if ( Sync == 0 ) |
215 | if ( Sync == 0 ) |
216 | { |
216 | { |
217 | if(bCheckDelay) |
217 | if(bCheckDelay) |
218 | { |
218 | { |
219 | // nach einer Pause von mind. 7ms erstes Sync-Character gefunden |
219 | // nach einer Pause von mind. 7ms erstes Sync-Character gefunden |
220 | // Zeichen ignorieren, da Bedeutung unbekannt |
220 | // Zeichen ignorieren, da Bedeutung unbekannt |
221 | Sync = 1; |
221 | Sync = 1; |
222 | FrameCnt ++; |
222 | FrameCnt ++; |
223 | SpektrumTimer = MAX_BYTEGAP; |
223 | SpektrumTimer = MAX_BYTEGAP; |
224 | } |
224 | } |
225 | else |
225 | else |
226 | { |
226 | { |
227 | // Zeichen kam vor Ablauf der 7ms Sync-Pause |
227 | // Zeichen kam vor Ablauf der 7ms Sync-Pause |
228 | // warten auf erstes Sync-Zeichen |
228 | // warten auf erstes Sync-Zeichen |
229 | SpektrumTimer = MIN_FRAMEGAP; |
229 | SpektrumTimer = MIN_FRAMEGAP; |
230 | FrameCnt = 0; |
230 | FrameCnt = 0; |
231 | Sync = 0; |
231 | Sync = 0; |
232 | ByteHigh = 0; |
232 | ByteHigh = 0; |
233 | } |
233 | } |
234 | } |
234 | } |
235 | else if((Sync == 1) && !bCheckDelay) |
235 | else if((Sync == 1) && !bCheckDelay) |
236 | { |
236 | { |
237 | // zweites Sync-Character ignorieren, Bedeutung unbekannt |
237 | // zweites Sync-Character ignorieren, Bedeutung unbekannt |
238 | Sync = 2; |
238 | Sync = 2; |
239 | FrameCnt ++; |
239 | FrameCnt ++; |
240 | SpektrumTimer = MAX_BYTEGAP; |
240 | SpektrumTimer = MAX_BYTEGAP; |
241 | } |
241 | } |
242 | else if((Sync == 2) && !bCheckDelay) |
242 | else if((Sync == 2) && !bCheckDelay) |
243 | { |
243 | { |
244 | SpektrumTimer = MAX_BYTEGAP; |
244 | SpektrumTimer = MAX_BYTEGAP; |
245 | // Datenbyte high |
245 | // Datenbyte high |
246 | ByteHigh = c; |
246 | ByteHigh = c; |
247 | if (FrameCnt == 2) |
247 | if (FrameCnt == 2) |
248 | { |
248 | { |
249 | // is 1st Byte of Channel-data |
249 | // is 1st Byte of Channel-data |
250 | // Frame 1 with Channel 1-7 comming next |
250 | // Frame 1 with Channel 1-7 comming next |
251 | Frame2 = 0; |
251 | Frame2 = 0; |
252 | if(ByteHigh & 0x80) |
252 | if(ByteHigh & 0x80) |
253 | { |
253 | { |
254 | // DS9: Frame 2 with Channel 8-9 comming next |
254 | // DS9: Frame 2 with Channel 8-9 comming next |
255 | Frame2 = 1; |
255 | Frame2 = 1; |
256 | } |
256 | } |
257 | } |
257 | } |
258 | Sync = 3; |
258 | Sync = 3; |
259 | FrameCnt ++; |
259 | FrameCnt ++; |
260 | } |
260 | } |
261 | else if((Sync == 3) && !bCheckDelay) |
261 | else if((Sync == 3) && !bCheckDelay) |
262 | { |
262 | { |
263 | // Datenbyte low |
263 | // Datenbyte low |
264 | // High-Byte for next channel comes next |
264 | // High-Byte for next channel comes next |
265 | SpektrumTimer = MAX_BYTEGAP; |
265 | SpektrumTimer = MAX_BYTEGAP; |
266 | Sync = 2; |
266 | Sync = 2; |
267 | FrameCnt ++; |
267 | FrameCnt ++; |
268 | index = (ByteHigh >> 2) & 0x0f; |
268 | index = (ByteHigh >> 2) & 0x0f; |
269 | index++; |
269 | index++; |
270 | Channel = ((unsigned int)ByteHigh << 8) | c; |
270 | Channel = ((unsigned int)ByteHigh << 8) | c; |
271 | signal = Channel & 0x3ff; |
271 | signal = Channel & 0x3ff; |
272 | signal -= 0x200; // Offset, range 0x000..0x3ff? |
272 | signal -= 0x200; // Offset, range 0x000..0x3ff? |
273 | signal = signal/3; // scaling to fit PPM resolution |
273 | signal = signal/3; // scaling to fit PPM resolution |
274 | 274 | ||
275 | if(index > 0 && index < 13) |
275 | if(index > 0 && index < 13) |
276 | { |
276 | { |
277 | // Stabiles Signal |
277 | // Stabiles Signal |
278 | if(abs(signal - PPM_in[index]) < 6) |
278 | if(abs(signal - PPM_in[index]) < 6) |
279 | { |
279 | { |
280 | if(SenderOkay < 200) SenderOkay += 10; |
280 | if(SenderOkay < 200) SenderOkay += 10; |
281 | else |
281 | else |
282 | { |
282 | { |
283 | SenderOkay = 200; |
283 | SenderOkay = 200; |
284 | TIMSK1 &= ~_BV(ICIE1); // disable PPM-Input |
284 | TIMSK1 &= ~_BV(ICIE1); // disable PPM-Input |
285 | } |
285 | } |
286 | } |
286 | } |
287 | tmp = (3 * (PPM_in[index]) + signal) / 4; |
287 | tmp = (3 * (PPM_in[index]) + signal) / 4; |
288 | if(tmp > signal+1) tmp--; else |
288 | if(tmp > signal+1) tmp--; else |
289 | if(tmp < signal-1) tmp++; |
289 | if(tmp < signal-1) tmp++; |
290 | if(SenderOkay >= 180) PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3; |
290 | if(SenderOkay >= 180) PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3; |
291 | else PPM_diff[index] = 0; |
291 | else PPM_diff[index] = 0; |
292 | PPM_in[index] = tmp; |
292 | PPM_in[index] = tmp; |
293 | } |
293 | } |
294 | else if(index > 17) ReSync = 1; // hier stimmt was nicht: neu synchronisieren |
294 | else if(index > 17) ReSync = 1; // hier stimmt was nicht: neu synchronisieren |
295 | } |
295 | } |
296 | else |
296 | else |
297 | { |
297 | { |
298 | // hier stimmt was nicht: neu synchronisieren |
298 | // hier stimmt was nicht: neu synchronisieren |
299 | ReSync = 1; |
299 | ReSync = 1; |
300 | FrameCnt = 0; |
300 | FrameCnt = 0; |
301 | Frame2 = 0; |
301 | Frame2 = 0; |
302 | // new frame next, nach fruehestens 7ms erwartet |
302 | // new frame next, nach fruehestens 7ms erwartet |
303 | SpektrumTimer = MIN_FRAMEGAP; |
303 | SpektrumTimer = MIN_FRAMEGAP; |
304 | } |
304 | } |
305 | 305 | ||
306 | // 16 Bytes eingetroffen -> Komplett |
306 | // 16 Bytes eingetroffen -> Komplett |
307 | if(FrameCnt >= 16) |
307 | if(FrameCnt >= 16) |
308 | { |
308 | { |
309 | // Frame complete |
309 | // Frame complete |
310 | if(Frame2 == 0) |
310 | if(Frame2 == 0) |
311 | { |
311 | { |
312 | // Null bedeutet: Neue Daten |
312 | // Null bedeutet: Neue Daten |
313 | // nur beim ersten Frame (CH 0-7) setzen |
313 | // nur beim ersten Frame (CH 0-7) setzen |
314 | if(!ReSync) NewPpmData = 0; |
314 | if(!ReSync) NewPpmData = 0; |
315 | } |
315 | } |
316 | FrameCnt = 0; |
316 | FrameCnt = 0; |
317 | Frame2 = 0; |
317 | Frame2 = 0; |
318 | Sync = 0; |
318 | Sync = 0; |
319 | SpektrumTimer = MIN_FRAMEGAP; |
319 | SpektrumTimer = MIN_FRAMEGAP; |
320 | } |
320 | } |
321 | } |
321 | } |
322 | } |
322 | } |
323 | 323 | ||
324 | 324 | ||
325 | 325 |