Rev 1213 | Rev 1320 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1213 | Rev 1232 | ||
---|---|---|---|
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 | 7 | ||
8 | //--------------------------------------------------------------// |
8 | //--------------------------------------------------------------// |
9 | 9 | ||
10 | //--------------------------------------------------------------// |
10 | //--------------------------------------------------------------// |
11 | void SpektrumBinding(void) |
11 | void SpektrumBinding(void) |
12 | { |
12 | { |
13 | unsigned int timerTimeout = SetDelay(10000); // Timeout 10 sec. |
13 | unsigned int timerTimeout = SetDelay(10000); // Timeout 10 sec. |
14 | unsigned char connected = 0; |
14 | unsigned char connected = 0; |
15 | unsigned int delaycounter; |
15 | unsigned int delaycounter; |
16 | 16 | ||
17 | UCSR1B &= ~(1 << RXCIE1); // disable rx-interrupt |
17 | UCSR1B &= ~(1 << RXCIE1); // disable rx-interrupt |
18 | UCSR1B &= ~(1<<RXEN1); // disable Uart-Rx |
18 | UCSR1B &= ~(1<<RXEN1); // disable Uart-Rx |
19 | PORTD &= ~(1 << PORTD2); // disable pull-up |
19 | PORTD &= ~(1 << PORTD2); // disable pull-up |
20 | 20 | ||
21 | printf("\n\rPlease connect Spektrum receiver for binding NOW..."); |
21 | printf("\n\rPlease connect Spektrum receiver for binding NOW..."); |
22 | 22 | ||
23 | while(!CheckDelay(timerTimeout)) |
23 | while(!CheckDelay(timerTimeout)) |
24 | { |
24 | { |
25 | if (PIND & (1 << PORTD2)) { timerTimeout = SetDelay(90); connected = 1; break; } |
25 | if (PIND & (1 << PORTD2)) { timerTimeout = SetDelay(90); connected = 1; break; } |
26 | } |
26 | } |
27 | 27 | ||
28 | if (connected) |
28 | if (connected) |
29 | { |
29 | { |
30 | 30 | ||
31 | printf("ok.\n\r"); |
31 | printf("ok.\n\r"); |
32 | DDRD |= (1 << DDD2); // Rx as output |
32 | DDRD |= (1 << DDD2); // Rx as output |
33 | 33 | ||
34 | while(!CheckDelay(timerTimeout)); // delay after startup of RX |
34 | while(!CheckDelay(timerTimeout)); // delay after startup of RX |
35 | for (delaycounter = 0; delaycounter < 100; delaycounter++) PORTD |= (1 << PORTD2); |
35 | for (delaycounter = 0; delaycounter < 100; delaycounter++) PORTD |= (1 << PORTD2); |
36 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
36 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
37 | 37 | ||
38 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
38 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
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 < 400; delaycounter++) PORTD |= (1 << PORTD2); |
40 | for (delaycounter = 0; delaycounter < 400; 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 | 42 | ||
43 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
43 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
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 < 400; delaycounter++) PORTD |= (1 << PORTD2); |
45 | for (delaycounter = 0; delaycounter < 400; 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 | 47 | ||
48 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
48 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
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 < 400; delaycounter++) PORTD |= (1 << PORTD2); |
50 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
51 | 51 | ||
52 | } |
52 | } |
53 | else |
53 | else |
54 | { printf("Timeout.\n\r"); |
54 | { printf("Timeout.\n\r"); |
55 | 55 | ||
56 | 56 | ||
57 | } |
57 | } |
58 | 58 | ||
59 | DDRD &= ~(1 << DDD2); // RX as input |
59 | DDRD &= ~(1 << DDD2); // RX as input |
60 | PORTD &= ~(1 << PORTD2); |
60 | PORTD &= ~(1 << PORTD2); |
61 | 61 | ||
62 | Uart1Init(); // init Uart again |
62 | Uart1Init(); // init Uart again |
63 | } |
63 | } |
64 | 64 | ||
65 | //############################################################################ |
65 | //############################################################################ |
66 | // zum Decodieren des Spektrum Satelliten wird USART1 benutzt. |
66 | // zum Decodieren des Spektrum Satelliten wird USART1 benutzt. |
67 | // USART1 initialisation from killagreg |
67 | // USART1 initialisation from killagreg |
68 | void Uart1Init(void) |
68 | void Uart1Init(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 | 94 | ||
95 | 95 | ||
96 | 96 | ||
97 | UCSR1B = (1<<RXEN1); |
97 | UCSR1B = (1<<RXEN1); |
98 | // set asynchronous mode |
98 | // set asynchronous mode |
99 | UCSR1C &= ~(1 << UMSEL11); |
99 | UCSR1C &= ~(1 << UMSEL11); |
100 | UCSR1C &= ~(1 << UMSEL10); |
100 | UCSR1C &= ~(1 << UMSEL10); |
101 | // no parity |
101 | // no parity |
102 | UCSR1C &= ~(1 << UPM11); |
102 | UCSR1C &= ~(1 << UPM11); |
103 | UCSR1C &= ~(1 << UPM10); |
103 | UCSR1C &= ~(1 << UPM10); |
104 | // 1 stop bit |
104 | // 1 stop bit |
105 | UCSR1C &= ~(1 << USBS1); |
105 | UCSR1C &= ~(1 << USBS1); |
106 | // 8-bit |
106 | // 8-bit |
107 | UCSR1B &= ~(1 << UCSZ12); |
107 | UCSR1B &= ~(1 << UCSZ12); |
108 | UCSR1C |= (1 << UCSZ11); |
108 | UCSR1C |= (1 << UCSZ11); |
109 | UCSR1C |= (1 << UCSZ10); |
109 | UCSR1C |= (1 << UCSZ10); |
110 | // flush receive buffer explicit |
110 | // flush receive buffer explicit |
111 | while(UCSR1A & (1<<RXC1)) UDR1; |
111 | while(UCSR1A & (1<<RXC1)) UDR1; |
112 | // enable RX-interrupts at the end |
112 | // enable RX-interrupts at the end |
113 | UCSR1B |= (1 << RXCIE1); |
113 | UCSR1B |= (1 << RXCIE1); |
114 | // -- End of USART1 initialisation |
114 | // -- End of USART1 initialisation |
115 | return; |
115 | return; |
116 | } |
116 | } |
117 | 117 | ||
118 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
118 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
119 | // + Copyright (c) Rainer Walther |
119 | // + Copyright (c) Rainer Walther |
120 | // + RC-routines from original MK rc.c (c) H&I |
120 | // + RC-routines from original MK rc.c (c) H&I |
121 | // + Useful infos from Walter: http://www.rcgroups.com/forums/showthread.php?t=714299&page=2 |
121 | // + Useful infos from Walter: http://www.rcgroups.com/forums/showthread.php?t=714299&page=2 |
122 | // + only for non-profit use |
122 | // + only for non-profit use |
123 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
123 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
124 | // |
124 | // |
125 | // 20080808 rw Modified for Spektrum AR6100 (PPM) |
125 | // 20080808 rw Modified for Spektrum AR6100 (PPM) |
126 | // 20080823 rw Add Spektrum satellite receiver on USART1 (644P only) |
126 | // 20080823 rw Add Spektrum satellite receiver on USART1 (644P only) |
127 | // 20081213 rw Add support for Spektrum DS9 Air-Tx-Module (9 channels) |
127 | // 20081213 rw Add support for Spektrum DS9 Air-Tx-Module (9 channels) |
128 | // Replace AR6100-coding with original composit-signal routines |
128 | // Replace AR6100-coding with original composit-signal routines |
129 | // |
129 | // |
130 | // --- |
130 | // --- |
131 | // Entweder Summensignal ODER Spektrum-Receiver anschließen. Nicht beides gleichzeitig betreiben! |
131 | // Entweder Summensignal ODER Spektrum-Receiver anschließen. Nicht beides gleichzeitig betreiben! |
132 | // Binding is not implemented. Bind with external Receiver. |
132 | // Binding is not implemented. Bind with external Receiver. |
133 | // Servo output J3, J4, J5 not serviced |
133 | // Servo output J3, J4, J5 not serviced |
134 | // |
134 | // |
135 | // Anschuß Spektrum Receiver |
135 | // Anschuß Spektrum Receiver |
136 | // Orange: 3V von der FC (keinesfalls an 5V anschließen!) |
136 | // Orange: 3V von der FC (keinesfalls an 5V anschließen!) |
137 | // Schwarz: GND |
137 | // Schwarz: GND |
138 | // Grau: RXD1 (Pin 3) auf 10-Pol FC-Stecker |
138 | // Grau: RXD1 (Pin 3) auf 10-Pol FC-Stecker |
139 | // |
139 | // |
140 | // --- |
140 | // --- |
141 | // Satellite-Reciever connected on USART1: |
141 | // Satellite-Reciever connected on USART1: |
142 | // |
142 | // |
143 | // DX7/DX6i: One data-frame at 115200 baud every 22ms. |
143 | // DX7/DX6i: One data-frame at 115200 baud every 22ms. |
144 | // DX7se: One data-frame at 115200 baud every 11ms. |
144 | // DX7se: One data-frame at 115200 baud every 11ms. |
145 | // byte1: unknown |
145 | // byte1: unknown |
146 | // byte2: unknown |
146 | // byte2: unknown |
147 | // byte3: and byte4: channel data (FLT-Mode) |
147 | // byte3: and byte4: channel data (FLT-Mode) |
148 | // byte5: and byte6: channel data (Roll) |
148 | // byte5: and byte6: channel data (Roll) |
149 | // byte7: and byte8: channel data (Nick) |
149 | // byte7: and byte8: channel data (Nick) |
150 | // byte9: and byte10: channel data (Gier) |
150 | // byte9: and byte10: channel data (Gier) |
151 | // byte11: and byte12: channel data (Gear Switch) |
151 | // byte11: and byte12: channel data (Gear Switch) |
152 | // byte13: and byte14: channel data (Gas) |
152 | // byte13: and byte14: channel data (Gas) |
153 | // byte15: and byte16: channel data (AUX2) |
153 | // byte15: and byte16: channel data (AUX2) |
154 | // |
154 | // |
155 | // DS9 (9 Channel): One data-frame at 115200 baud every 11ms, alternating frame 1/2 for CH1-7 / CH8-9 |
155 | // DS9 (9 Channel): One data-frame at 115200 baud every 11ms, alternating frame 1/2 for CH1-7 / CH8-9 |
156 | // 1st Frame: |
156 | // 1st Frame: |
157 | // byte1: unknown |
157 | // byte1: unknown |
158 | // byte2: unknown |
158 | // byte2: unknown |
159 | // byte3: and byte4: channel data |
159 | // byte3: and byte4: channel data |
160 | // byte5: and byte6: channel data |
160 | // byte5: and byte6: channel data |
161 | // byte7: and byte8: channel data |
161 | // byte7: and byte8: channel data |
162 | // byte9: and byte10: channel data |
162 | // byte9: and byte10: channel data |
163 | // byte11: and byte12: channel data |
163 | // byte11: and byte12: channel data |
164 | // byte13: and byte14: channel data |
164 | // byte13: and byte14: channel data |
165 | // byte15: and byte16: channel data |
165 | // byte15: and byte16: channel data |
166 | // 2nd Frame: |
166 | // 2nd Frame: |
167 | // byte1: unknown |
167 | // byte1: unknown |
168 | // byte2: unknown |
168 | // byte2: unknown |
169 | // byte3: and byte4: channel data |
169 | // byte3: and byte4: channel data |
170 | // byte5: and byte6: channel data |
170 | // byte5: and byte6: channel data |
171 | // byte7: and byte8: 0xffff |
171 | // byte7: and byte8: 0xffff |
172 | // byte9: and byte10: 0xffff |
172 | // byte9: and byte10: 0xffff |
173 | // byte11: and byte12: 0xffff |
173 | // byte11: and byte12: 0xffff |
174 | // byte13: and byte14: 0xffff |
174 | // byte13: and byte14: 0xffff |
175 | // byte15: and byte16: 0xffff |
175 | // byte15: and byte16: 0xffff |
176 | // |
176 | // |
177 | // Each channel data (16 bit= 2byte, first msb, second lsb) is arranged as: |
177 | // Each channel data (16 bit= 2byte, first msb, second lsb) is arranged as: |
178 | // |
178 | // |
179 | // Bits: F 0 C3 C2 C1 C0 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 |
179 | // Bits: F 0 C3 C2 C1 C0 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 |
180 | // |
180 | // |
181 | // 0 means a '0' bit |
181 | // 0 means a '0' bit |
182 | // F: 1 = indicates beginning of 2nd frame for CH8-9 (DS9 only) |
182 | // F: 1 = indicates beginning of 2nd frame for CH8-9 (DS9 only) |
183 | // C3 to C0 is the channel number. 0 to 9 (4 bit, as assigned in the transmitter) |
183 | // C3 to C0 is the channel number. 0 to 9 (4 bit, as assigned in the transmitter) |
184 | // D9 to D0 is the channel data (10 bit) 0xaa..0x200..0x356 for 100% transmitter-travel |
184 | // D9 to D0 is the channel data (10 bit) 0xaa..0x200..0x356 for 100% transmitter-travel |
185 | // |
185 | // |
186 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
186 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
187 | 187 | ||
188 | //############################################################################ |
188 | //############################################################################ |
189 | //Diese Routine startet und inizialisiert den USART1 für seriellen Spektrum satellite reciever |
189 | //Diese Routine startet und inizialisiert den USART1 für seriellen Spektrum satellite reciever |
190 | SIGNAL(USART1_RX_vect) |
190 | SIGNAL(USART1_RX_vect) |
191 | //############################################################################ |
191 | //############################################################################ |
192 | { |
192 | { |
193 | static unsigned int Sync=0, FrameCnt=0, ByteHigh=0, ReSync=1, Frame2=0, FrameTimer; |
193 | static unsigned int Sync=0, FrameCnt=0, ByteHigh=0, ReSync=1, Frame2=0, FrameTimer; |
194 | unsigned int Channel, index; |
194 | unsigned int Channel, index; |
195 | signed int signal, tmp; |
195 | signed int signal, tmp; |
196 | int bCheckDelay; |
196 | int bCheckDelay; |
197 | uint8_t c; |
197 | uint8_t c; |
198 | 198 | ||
199 | c = UDR1; // get data byte |
199 | c = UDR1; // get data byte |
200 | 200 | ||
201 | if (ReSync == 1) |
201 | if (ReSync == 1) |
202 | { |
202 | { |
203 | // wait for beginning of new frame |
203 | // wait for beginning of new frame |
204 | ReSync = 0; |
204 | ReSync = 0; |
205 | 205 | ||
206 | FrameTimer = SetDelay(7); // minimum 7ms zwischen den frames |
206 | FrameTimer = SetDelay(7); // minimum 7ms zwischen den frames |
207 | FrameCnt = 0; |
207 | FrameCnt = 0; |
208 | Sync = 0; |
208 | Sync = 0; |
209 | ByteHigh = 0; |
209 | ByteHigh = 0; |
210 | } |
210 | } |
211 | else |
211 | else |
212 | { |
212 | { |
213 | bCheckDelay = CheckDelay(FrameTimer); |
213 | bCheckDelay = CheckDelay(FrameTimer); |
214 | if ( Sync == 0 ) |
214 | if ( Sync == 0 ) |
215 | { |
215 | { |
216 | if(bCheckDelay) |
216 | if(bCheckDelay) |
217 | { |
217 | { |
218 | // nach einer Pause von mind. 7ms erstes Sync-Character gefunden |
218 | // nach einer Pause von mind. 7ms erstes Sync-Character gefunden |
219 | // Zeichen ignorieren, da Bedeutung unbekannt |
219 | // Zeichen ignorieren, da Bedeutung unbekannt |
220 | Sync = 1; |
220 | Sync = 1; |
221 | FrameCnt ++; |
221 | FrameCnt ++; |
222 | } |
222 | } |
223 | else |
223 | else |
224 | { |
224 | { |
225 | // Zeichen kam vor Ablauf der 7ms Sync-Pause |
225 | // Zeichen kam vor Ablauf der 7ms Sync-Pause |
226 | // warten auf erstes Sync-Zeichen |
226 | // warten auf erstes Sync-Zeichen |
227 | } |
227 | } |
228 | } |
228 | } |
229 | else if((Sync == 1) && !bCheckDelay) |
229 | else if((Sync == 1) && !bCheckDelay) |
230 | { |
230 | { |
231 | // zweites Sync-Character ignorieren, Bedeutung unbekannt |
231 | // zweites Sync-Character ignorieren, Bedeutung unbekannt |
232 | Sync = 2; |
232 | Sync = 2; |
233 | FrameCnt ++; |
233 | FrameCnt ++; |
234 | } |
234 | } |
235 | else if((Sync == 2) && !bCheckDelay) |
235 | else if((Sync == 2) && !bCheckDelay) |
236 | { |
236 | { |
237 | // Datenbyte high |
237 | // Datenbyte high |
238 | ByteHigh = c; |
238 | ByteHigh = c; |
239 | 239 | ||
240 | if (FrameCnt == 2) |
240 | if (FrameCnt == 2) |
241 | { |
241 | { |
242 | // is 1st Byte of Channel-data |
242 | // is 1st Byte of Channel-data |
243 | // Frame 1 with Channel 1-7 comming next |
243 | // Frame 1 with Channel 1-7 comming next |
244 | Frame2 = 0; |
244 | Frame2 = 0; |
245 | if(ByteHigh & 0x80) |
245 | if(ByteHigh & 0x80) |
246 | { |
246 | { |
247 | // DS9: Frame 2 with Channel 8-9 comming next |
247 | // DS9: Frame 2 with Channel 8-9 comming next |
248 | Frame2 = 1; |
248 | Frame2 = 1; |
249 | } |
249 | } |
250 | } |
250 | } |
251 | Sync = 3; |
251 | Sync = 3; |
252 | FrameCnt ++; |
252 | FrameCnt ++; |
253 | } |
253 | } |
254 | else if((Sync == 3) && !bCheckDelay) |
254 | else if((Sync == 3) && !bCheckDelay) |
255 | { |
255 | { |
256 | // Datenbyte low |
256 | // Datenbyte low |
257 | 257 | ||
258 | // High-Byte for next channel comes next |
258 | // High-Byte for next channel comes next |
259 | Sync = 2; |
259 | Sync = 2; |
260 | FrameCnt ++; |
260 | FrameCnt ++; |
261 | 261 | ||
262 | index = (ByteHigh >> 2) & 0x0f; |
262 | index = (ByteHigh >> 2) & 0x0f; |
263 | index ++; |
263 | index ++; |
264 | Channel = (ByteHigh << 8) | c; |
264 | Channel = (ByteHigh << 8) | c; |
265 | signal = Channel & 0x3ff; |
265 | signal = Channel & 0x3ff; |
266 | signal -= 0x200; // Offset, range 0x000..0x3ff? |
266 | signal -= 0x200; // Offset, range 0x000..0x3ff? |
267 | signal = signal/3; // scaling to fit PPM resolution |
267 | signal = signal/3; // scaling to fit PPM resolution |
268 | 268 | ||
269 | if(index >= 0 && index <= 10) |
269 | if(index >= 0 && index <= 10) |
270 | { |
270 | { |
271 | // Stabiles Signal |
271 | // Stabiles Signal |
272 | if(abs(signal - PPM_in[index]) < 6) { if(SenderOkay < 200) SenderOkay += 10; else SenderOkay = 200;} |
272 | if(abs(signal - PPM_in[index]) < 6) |
- | 273 | { |
|
- | 274 | if(SenderOkay < 200) SenderOkay += 10; |
|
- | 275 | else |
|
- | 276 | { |
|
- | 277 | SenderOkay = 200; |
|
- | 278 | TIMSK1 &= ~_BV(ICIE1); // disable PPM-Input |
|
- | 279 | } |
|
- | 280 | } |
|
273 | tmp = (3 * (PPM_in[index]) + signal) / 4; |
281 | tmp = (3 * (PPM_in[index]) + signal) / 4; |
274 | if(tmp > signal+1) tmp--; else |
282 | if(tmp > signal+1) tmp--; else |
275 | if(tmp < signal-1) tmp++; |
283 | if(tmp < signal-1) tmp++; |
276 | if(SenderOkay >= 180) PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3; |
284 | if(SenderOkay >= 180) PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3; |
277 | else PPM_diff[index] = 0; |
285 | else PPM_diff[index] = 0; |
278 | PPM_in[index] = tmp; |
286 | PPM_in[index] = tmp; |
279 | } |
287 | } |
280 | } |
288 | } |
281 | else |
289 | else |
282 | { |
290 | { |
283 | // hier stimmt was nicht: neu synchronisieren |
291 | // hier stimmt was nicht: neu synchronisieren |
284 | ReSync = 1; |
292 | ReSync = 1; |
285 | FrameCnt = 0; |
293 | FrameCnt = 0; |
286 | Frame2 = 0; |
294 | Frame2 = 0; |
287 | } |
295 | } |
288 | 296 | ||
289 | // 16 Bytes per frame |
297 | // 16 Bytes per frame |
290 | if(FrameCnt >= 16) |
298 | if(FrameCnt >= 16) |
291 | { |
299 | { |
292 | // Frame complete |
300 | // Frame complete |
293 | if(Frame2 == 0) |
301 | if(Frame2 == 0) |
294 | { |
302 | { |
295 | // Null bedeutet: Neue Daten |
303 | // Null bedeutet: Neue Daten |
296 | // nur beim ersten Frame (CH 0-7) setzen |
304 | // nur beim ersten Frame (CH 0-7) setzen |
297 | NewPpmData = 0; |
305 | NewPpmData = 0; |
298 | } |
306 | } |
299 | 307 | ||
300 | // new frame next, nach fruehestens 7ms erwartet |
308 | // new frame next, nach fruehestens 7ms erwartet |
301 | FrameCnt = 0; |
309 | FrameCnt = 0; |
302 | Frame2 = 0; |
310 | Frame2 = 0; |
303 | Sync = 0; |
311 | Sync = 0; |
304 | } |
312 | } |
305 | // Zeit bis zum nächsten Zeichen messen |
313 | // Zeit bis zum nächsten Zeichen messen |
306 | FrameTimer = SetDelay(7); |
314 | FrameTimer = SetDelay(7); |
307 | } |
315 | } |
308 | } |
316 | } |
309 | 317 | ||
310 | 318 | ||
311 | 319 |