Rev 1322 | Rev 1391 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1322 | Rev 1377 | ||
---|---|---|---|
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 "Spectrum.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 | Uart1Init(); // init Uart again |
63 | Uart1Init(); // init Uart again |
64 | } |
64 | } |
65 | 65 | ||
66 | //############################################################################ |
66 | //############################################################################ |
67 | // zum Decodieren des Spektrum Satelliten wird USART1 benutzt. |
67 | // zum Decodieren des Spektrum Satelliten wird USART1 benutzt. |
68 | // USART1 initialisation from killagreg |
68 | // USART1 initialisation from killagreg |
69 | void Uart1Init(void) |
69 | void Uart1Init(void) |
70 | //############################################################################ |
70 | //############################################################################ |
71 | { |
71 | { |
72 | // -- Start of USART1 initialisation for Spekturm seriell-mode |
72 | // -- Start of USART1 initialisation for Spekturm seriell-mode |
73 | // USART1 Control and Status Register A, B, C and baud rate register |
73 | // USART1 Control and Status Register A, B, C and baud rate register |
74 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * 115200) - 1); |
74 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * 115200) - 1); |
75 | // disable RX-Interrupt |
75 | // disable RX-Interrupt |
76 | UCSR1B &= ~(1 << RXCIE1); |
76 | UCSR1B &= ~(1 << RXCIE1); |
77 | // disable TX-Interrupt |
77 | // disable TX-Interrupt |
78 | UCSR1B &= ~(1 << TXCIE1); |
78 | UCSR1B &= ~(1 << TXCIE1); |
79 | // disable DRE-Interrupt |
79 | // disable DRE-Interrupt |
80 | UCSR1B &= ~(1 << UDRIE1); |
80 | UCSR1B &= ~(1 << UDRIE1); |
81 | // set direction of RXD1 and TXD1 pins |
81 | // set direction of RXD1 and TXD1 pins |
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 |
100 | UCSR1C &= ~(1 << UPM11); |
100 | UCSR1C &= ~(1 << UPM11); |
101 | UCSR1C &= ~(1 << UPM10); |
101 | UCSR1C &= ~(1 << UPM10); |
102 | // 1 stop bit |
102 | // 1 stop bit |
103 | UCSR1C &= ~(1 << USBS1); |
103 | UCSR1C &= ~(1 << USBS1); |
104 | // 8-bit |
104 | // 8-bit |
105 | UCSR1B &= ~(1 << UCSZ12); |
105 | UCSR1B &= ~(1 << UCSZ12); |
106 | UCSR1C |= (1 << UCSZ11); |
106 | UCSR1C |= (1 << UCSZ11); |
107 | UCSR1C |= (1 << UCSZ10); |
107 | UCSR1C |= (1 << UCSZ10); |
108 | // flush receive buffer explicit |
108 | // flush receive buffer explicit |
109 | while(UCSR1A & (1<<RXC1)) UDR1; |
109 | while(UCSR1A & (1<<RXC1)) UDR1; |
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 |
121 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
121 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
122 | // |
122 | // |
123 | // 20080808 rw Modified for Spektrum AR6100 (PPM) |
123 | // 20080808 rw Modified for Spektrum AR6100 (PPM) |
124 | // 20080823 rw Add Spektrum satellite receiver on USART1 (644P only) |
124 | // 20080823 rw Add Spektrum satellite receiver on USART1 (644P only) |
125 | // 20081213 rw Add support for Spektrum DS9 Air-Tx-Module (9 channels) |
125 | // 20081213 rw Add support for Spektrum DS9 Air-Tx-Module (9 channels) |
126 | // Replace AR6100-coding with original composit-signal routines |
126 | // Replace AR6100-coding with original composit-signal routines |
127 | // |
127 | // |
128 | // --- |
128 | // --- |
129 | // Entweder Summensignal ODER Spektrum-Receiver anschließen. Nicht beides gleichzeitig betreiben! |
129 | // Entweder Summensignal ODER Spektrum-Receiver anschließen. Nicht beides gleichzeitig betreiben! |
130 | // Binding is not implemented. Bind with external Receiver. |
130 | // Binding is not implemented. Bind with external Receiver. |
131 | // Servo output J3, J4, J5 not serviced |
131 | // Servo output J3, J4, J5 not serviced |
132 | // |
132 | // |
133 | // Anschuß Spektrum Receiver |
133 | // Anschuß Spektrum Receiver |
134 | // Orange: 3V von der FC (keinesfalls an 5V anschließen!) |
134 | // Orange: 3V von der FC (keinesfalls an 5V anschließen!) |
135 | // Schwarz: GND |
135 | // Schwarz: GND |
136 | // Grau: RXD1 (Pin 3) auf 10-Pol FC-Stecker |
136 | // Grau: RXD1 (Pin 3) auf 10-Pol FC-Stecker |
137 | // |
137 | // |
138 | // --- |
138 | // --- |
139 | // Satellite-Reciever connected on USART1: |
139 | // Satellite-Reciever connected on USART1: |
140 | // |
140 | // |
141 | // DX7/DX6i: One data-frame at 115200 baud every 22ms. |
141 | // DX7/DX6i: One data-frame at 115200 baud every 22ms. |
142 | // DX7se: One data-frame at 115200 baud every 11ms. |
142 | // DX7se: One data-frame at 115200 baud every 11ms. |
143 | // byte1: unknown |
143 | // byte1: unknown |
144 | // byte2: unknown |
144 | // byte2: unknown |
145 | // byte3: and byte4: channel data (FLT-Mode) |
145 | // byte3: and byte4: channel data (FLT-Mode) |
146 | // byte5: and byte6: channel data (Roll) |
146 | // byte5: and byte6: channel data (Roll) |
147 | // byte7: and byte8: channel data (Nick) |
147 | // byte7: and byte8: channel data (Nick) |
148 | // byte9: and byte10: channel data (Gier) |
148 | // byte9: and byte10: channel data (Gier) |
149 | // byte11: and byte12: channel data (Gear Switch) |
149 | // byte11: and byte12: channel data (Gear Switch) |
150 | // byte13: and byte14: channel data (Gas) |
150 | // byte13: and byte14: channel data (Gas) |
151 | // byte15: and byte16: channel data (AUX2) |
151 | // byte15: and byte16: channel data (AUX2) |
152 | // |
152 | // |
153 | // DS9 (9 Channel): One data-frame at 115200 baud every 11ms, alternating frame 1/2 for CH1-7 / CH8-9 |
153 | // DS9 (9 Channel): One data-frame at 115200 baud every 11ms, alternating frame 1/2 for CH1-7 / CH8-9 |
154 | // 1st Frame: |
154 | // 1st Frame: |
155 | // byte1: unknown |
155 | // byte1: unknown |
156 | // byte2: unknown |
156 | // byte2: unknown |
157 | // byte3: and byte4: channel data |
157 | // byte3: and byte4: channel data |
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 |
169 | // byte7: and byte8: 0xffff |
169 | // byte7: and byte8: 0xffff |
170 | // byte9: and byte10: 0xffff |
170 | // byte9: and byte10: 0xffff |
171 | // byte11: and byte12: 0xffff |
171 | // byte11: and byte12: 0xffff |
172 | // byte13: and byte14: 0xffff |
172 | // byte13: and byte14: 0xffff |
173 | // byte15: and byte16: 0xffff |
173 | // byte15: and byte16: 0xffff |
174 | // |
174 | // |
175 | // Each channel data (16 bit= 2byte, first msb, second lsb) is arranged as: |
175 | // Each channel data (16 bit= 2byte, first msb, second lsb) is arranged as: |
176 | // |
176 | // |
177 | // Bits: F 0 C3 C2 C1 C0 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 |
177 | // Bits: F 0 C3 C2 C1 C0 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 |
178 | // |
178 | // |
179 | // 0 means a '0' bit |
179 | // 0 means a '0' bit |
180 | // F: 1 = indicates beginning of 2nd frame for CH8-9 (DS9 only) |
180 | // F: 1 = indicates beginning of 2nd frame for CH8-9 (DS9 only) |
181 | // C3 to C0 is the channel number. 0 to 9 (4 bit, as assigned in the transmitter) |
181 | // C3 to C0 is the channel number. 0 to 9 (4 bit, as assigned in the transmitter) |
182 | // D9 to D0 is the channel data (10 bit) 0xaa..0x200..0x356 for 100% transmitter-travel |
182 | // D9 to D0 is the channel data (10 bit) 0xaa..0x200..0x356 for 100% transmitter-travel |
183 | // |
183 | // |
184 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
184 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
185 | 185 | ||
186 | #define MIN_FRAMEGAP 68 // 7ms |
186 | #define MIN_FRAMEGAP 68 // 7ms |
187 | #define MAX_BYTEGAP 3 // 310us |
187 | #define MAX_BYTEGAP 3 // 310us |
188 | 188 | ||
189 | //############################################################################ |
189 | //############################################################################ |
190 | //Diese Routine startet und inizialisiert den USART1 für seriellen Spektrum satellite reciever |
190 | //Diese Routine startet und inizialisiert den USART1 für seriellen Spektrum satellite reciever |
191 | SIGNAL(USART1_RX_vect) |
191 | SIGNAL(USART1_RX_vect) |
192 | //############################################################################ |
192 | //############################################################################ |
193 | { |
193 | { |
194 | static unsigned char Sync=0, FrameCnt=0, ByteHigh=0, ReSync=1, Frame2=0; |
194 | static unsigned char Sync=0, FrameCnt=0, ByteHigh=0, ReSync=1, Frame2=0; |
195 | unsigned int Channel, index; |
195 | unsigned int Channel, index; |
196 | signed int signal, tmp; |
196 | signed int signal, tmp; |
197 | int bCheckDelay; |
197 | int bCheckDelay; |
198 | uint8_t c; |
198 | uint8_t c; |
199 | c = UDR1; // get data byte |
199 | c = UDR1; // get data byte |
200 | if(ReSync == 1) |
200 | if(ReSync == 1) |
201 | { |
201 | { |
202 | // wait for beginning of new frame |
202 | // wait for beginning of new frame |
203 | ReSync = 0; |
203 | ReSync = 0; |
204 | SpektrumTimer = MIN_FRAMEGAP; |
204 | SpektrumTimer = MIN_FRAMEGAP; |
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 | { |
216 | // nach einer Pause von mind. 7ms erstes Sync-Character gefunden |
216 | // nach einer Pause von mind. 7ms erstes Sync-Character gefunden |
217 | // Zeichen ignorieren, da Bedeutung unbekannt |
217 | // Zeichen ignorieren, da Bedeutung unbekannt |
218 | Sync = 1; |
218 | Sync = 1; |
219 | FrameCnt ++; |
219 | FrameCnt ++; |
220 | SpektrumTimer = MAX_BYTEGAP; |
220 | SpektrumTimer = MAX_BYTEGAP; |
221 | } |
221 | } |
222 | else |
222 | else |
223 | { |
223 | { |
224 | // Zeichen kam vor Ablauf der 7ms Sync-Pause |
224 | // Zeichen kam vor Ablauf der 7ms Sync-Pause |
225 | // warten auf erstes Sync-Zeichen |
225 | // warten auf erstes Sync-Zeichen |
226 | SpektrumTimer = MIN_FRAMEGAP; |
226 | SpektrumTimer = MIN_FRAMEGAP; |
227 | FrameCnt = 0; |
227 | FrameCnt = 0; |
228 | Sync = 0; |
228 | Sync = 0; |
229 | ByteHigh = 0; |
229 | ByteHigh = 0; |
230 | } |
230 | } |
231 | } |
231 | } |
232 | else if((Sync == 1) && !bCheckDelay) |
232 | else if((Sync == 1) && !bCheckDelay) |
233 | { |
233 | { |
234 | // zweites Sync-Character ignorieren, Bedeutung unbekannt |
234 | // zweites Sync-Character ignorieren, Bedeutung unbekannt |
235 | Sync = 2; |
235 | Sync = 2; |
236 | FrameCnt ++; |
236 | FrameCnt ++; |
237 | SpektrumTimer = MAX_BYTEGAP; |
237 | SpektrumTimer = MAX_BYTEGAP; |
238 | } |
238 | } |
239 | else if((Sync == 2) && !bCheckDelay) |
239 | else if((Sync == 2) && !bCheckDelay) |
240 | { |
240 | { |
241 | SpektrumTimer = MAX_BYTEGAP; |
241 | SpektrumTimer = MAX_BYTEGAP; |
242 | // Datenbyte high |
242 | // Datenbyte high |
243 | ByteHigh = c; |
243 | ByteHigh = c; |
244 | if (FrameCnt == 2) |
244 | if (FrameCnt == 2) |
245 | { |
245 | { |
246 | // is 1st Byte of Channel-data |
246 | // is 1st Byte of Channel-data |
247 | // Frame 1 with Channel 1-7 comming next |
247 | // Frame 1 with Channel 1-7 comming next |
248 | Frame2 = 0; |
248 | Frame2 = 0; |
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 | { |
260 | // Datenbyte low |
260 | // Datenbyte low |
261 | // High-Byte for next channel comes next |
261 | // High-Byte for next channel comes next |
262 | SpektrumTimer = MAX_BYTEGAP; |
262 | SpektrumTimer = MAX_BYTEGAP; |
263 | Sync = 2; |
263 | Sync = 2; |
264 | FrameCnt ++; |
264 | FrameCnt ++; |
265 | index = (ByteHigh >> 2) & 0x0f; |
265 | index = (ByteHigh >> 2) & 0x0f; |
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 <= 10) |
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 | } |
293 | else |
293 | else |
294 | { |
294 | { |
295 | // hier stimmt was nicht: neu synchronisieren |
295 | // hier stimmt was nicht: neu synchronisieren |
296 | ReSync = 1; |
296 | ReSync = 1; |
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 | } |
320 | 320 | ||
321 | 321 | ||
322 | 322 |