Rev 1232 | Rev 1322 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1232 | Rev 1320 | ||
---|---|---|---|
Line 2... | Line 2... | ||
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 | ||
5 | #include "Spectrum.h" |
5 | #include "Spectrum.h" |
- | 6 | #include "main.h" |
|
Line 6... | Line 7... | ||
6 | #include "main.h" |
7 | unsigned char SpektrumTimer = 0; |
Line 7... | Line 8... | ||
7 | 8 | ||
8 | //--------------------------------------------------------------// |
9 | //--------------------------------------------------------------// |
Line 88... | Line 89... | ||
88 | UBRR1L = (uint8_t)ubrr; |
89 | UBRR1L = (uint8_t)ubrr; |
89 | // enable double speed operation |
90 | // enable double speed operation |
90 | UCSR1A |= (1 << U2X1); |
91 | UCSR1A |= (1 << U2X1); |
91 | // enable receiver and transmitter |
92 | // enable receiver and transmitter |
92 | //UCSR1B = (1<<RXEN1)|(1<<TXEN1); |
93 | //UCSR1B = (1<<RXEN1)|(1<<TXEN1); |
93 | - | ||
94 | - | ||
95 | - | ||
Line 96... | Line 94... | ||
96 | 94 | ||
97 | UCSR1B = (1<<RXEN1); |
95 | UCSR1B = (1<<RXEN1); |
98 | // set asynchronous mode |
96 | // set asynchronous mode |
99 | UCSR1C &= ~(1 << UMSEL11); |
97 | UCSR1C &= ~(1 << UMSEL11); |
Line 183... | Line 181... | ||
183 | // 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) |
184 | // 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 |
185 | // |
183 | // |
186 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
184 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
Line -... | Line 185... | ||
- | 185 | ||
- | 186 | #define MAX_FRAMEGAP 56 // 7ms |
|
- | 187 | #define MAX_BYTEGAP 3 // 375us |
|
187 | 188 | ||
188 | //############################################################################ |
189 | //############################################################################ |
189 | //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 |
190 | SIGNAL(USART1_RX_vect) |
191 | SIGNAL(USART1_RX_vect) |
191 | //############################################################################ |
192 | //############################################################################ |
192 | { |
193 | { |
193 | static unsigned int Sync=0, FrameCnt=0, ByteHigh=0, ReSync=1, Frame2=0, FrameTimer; |
194 | static unsigned char Sync=0, FrameCnt=0, ByteHigh=0, ReSync=1, Frame2=0; |
194 | unsigned int Channel, index; |
195 | unsigned int Channel, index; |
195 | signed int signal, tmp; |
196 | signed int signal, tmp; |
196 | int bCheckDelay; |
197 | int bCheckDelay; |
197 | uint8_t c; |
- | |
198 | 198 | uint8_t c; |
|
199 | c = UDR1; // get data byte |
- | |
200 | 199 | c = UDR1; // get data byte |
|
201 | if (ReSync == 1) |
200 | if(ReSync == 1) |
202 | { |
201 | { |
203 | // wait for beginning of new frame |
202 | // wait for beginning of new frame |
204 | ReSync = 0; |
- | |
205 | 203 | ReSync = 0; |
|
206 | FrameTimer = SetDelay(7); // minimum 7ms zwischen den frames |
204 | SpektrumTimer = MAX_FRAMEGAP; |
207 | FrameCnt = 0; |
205 | FrameCnt = 0; |
208 | Sync = 0; |
206 | Sync = 0; |
209 | ByteHigh = 0; |
207 | ByteHigh = 0; |
210 | } |
208 | } |
211 | else |
209 | else |
212 | { |
210 | { |
213 | bCheckDelay = CheckDelay(FrameTimer); |
211 | if(!SpektrumTimer) bCheckDelay = 1; else bCheckDelay = 0;//CheckDelay(FrameTimer); |
214 | if ( Sync == 0 ) |
212 | if ( Sync == 0 ) |
215 | { |
213 | { |
216 | if(bCheckDelay) |
214 | if(bCheckDelay) |
217 | { |
215 | { |
218 | // nach einer Pause von mind. 7ms erstes Sync-Character gefunden |
216 | // nach einer Pause von mind. 7ms erstes Sync-Character gefunden |
219 | // Zeichen ignorieren, da Bedeutung unbekannt |
217 | // Zeichen ignorieren, da Bedeutung unbekannt |
220 | Sync = 1; |
218 | Sync = 1; |
- | 219 | FrameCnt ++; |
|
221 | FrameCnt ++; |
220 | SpektrumTimer = MAX_BYTEGAP; |
222 | } |
221 | } |
223 | else |
222 | else |
224 | { |
223 | { |
225 | // Zeichen kam vor Ablauf der 7ms Sync-Pause |
224 | // Zeichen kam vor Ablauf der 7ms Sync-Pause |
- | 225 | // warten auf erstes Sync-Zeichen |
|
- | 226 | SpektrumTimer = MAX_FRAMEGAP; |
|
- | 227 | FrameCnt = 0; |
|
- | 228 | Sync = 0; |
|
226 | // warten auf erstes Sync-Zeichen |
229 | ByteHigh = 0; |
227 | } |
230 | } |
228 | } |
231 | } |
229 | else if((Sync == 1) && !bCheckDelay) |
232 | else if((Sync == 1) && !bCheckDelay) |
230 | { |
233 | { |
231 | // zweites Sync-Character ignorieren, Bedeutung unbekannt |
234 | // zweites Sync-Character ignorieren, Bedeutung unbekannt |
232 | Sync = 2; |
235 | Sync = 2; |
- | 236 | FrameCnt ++; |
|
233 | FrameCnt ++; |
237 | SpektrumTimer = MAX_BYTEGAP; |
234 | } |
238 | } |
235 | else if((Sync == 2) && !bCheckDelay) |
239 | else if((Sync == 2) && !bCheckDelay) |
- | 240 | { |
|
236 | { |
241 | SpektrumTimer = MAX_BYTEGAP; |
237 | // Datenbyte high |
242 | // Datenbyte high |
238 | ByteHigh = c; |
- | |
239 | 243 | ByteHigh = c; |
|
240 | if (FrameCnt == 2) |
244 | if (FrameCnt == 2) |
241 | { |
245 | { |
242 | // is 1st Byte of Channel-data |
246 | // is 1st Byte of Channel-data |
243 | // Frame 1 with Channel 1-7 comming next |
247 | // Frame 1 with Channel 1-7 comming next |
Line 252... | Line 256... | ||
252 | FrameCnt ++; |
256 | FrameCnt ++; |
253 | } |
257 | } |
254 | else if((Sync == 3) && !bCheckDelay) |
258 | else if((Sync == 3) && !bCheckDelay) |
255 | { |
259 | { |
256 | // Datenbyte low |
260 | // Datenbyte low |
257 | - | ||
258 | // High-Byte for next channel comes next |
261 | // High-Byte for next channel comes next |
- | 262 | SpektrumTimer = MAX_BYTEGAP; |
|
259 | Sync = 2; |
263 | Sync = 2; |
260 | FrameCnt ++; |
264 | FrameCnt ++; |
261 | - | ||
262 | index = (ByteHigh >> 2) & 0x0f; |
265 | index = (ByteHigh >> 2) & 0x0f; |
263 | index ++; |
266 | index++; |
264 | Channel = (ByteHigh << 8) | c; |
267 | Channel = ((unsigned int)ByteHigh << 8) | c; |
265 | signal = Channel & 0x3ff; |
268 | signal = Channel & 0x3ff; |
266 | signal -= 0x200; // Offset, range 0x000..0x3ff? |
269 | signal -= 0x200; // Offset, range 0x000..0x3ff? |
267 | signal = signal/3; // scaling to fit PPM resolution |
270 | signal = signal/3; // scaling to fit PPM resolution |
268 | 271 | ||
269 | if(index >= 0 && index <= 10) |
272 | if(index >= 0 && index <= 10) |
270 | { |
273 | { |
271 | // Stabiles Signal |
274 | // Stabiles Signal |
272 | if(abs(signal - PPM_in[index]) < 6) |
275 | if(abs(signal - PPM_in[index]) < 6) |
273 | { |
276 | { |
Line 283... | Line 286... | ||
283 | if(tmp < signal-1) tmp++; |
286 | if(tmp < signal-1) tmp++; |
284 | 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; |
285 | else PPM_diff[index] = 0; |
288 | else PPM_diff[index] = 0; |
286 | PPM_in[index] = tmp; |
289 | PPM_in[index] = tmp; |
287 | } |
290 | } |
- | 291 | else if(index > 17) ReSync = 1; // hier stimmt was nicht: neu synchronisieren |
|
288 | } |
292 | } |
289 | else |
293 | else |
290 | { |
294 | { |
291 | // hier stimmt was nicht: neu synchronisieren |
295 | // hier stimmt was nicht: neu synchronisieren |
292 | ReSync = 1; |
296 | ReSync = 1; |
293 | FrameCnt = 0; |
297 | FrameCnt = 0; |
294 | Frame2 = 0; |
298 | Frame2 = 0; |
- | 299 | // new frame next, nach fruehestens 7ms erwartet |
|
- | 300 | SpektrumTimer = MAX_FRAMEGAP; |
|
295 | } |
301 | } |
Line 296... | Line 302... | ||
296 | 302 | ||
297 | // 16 Bytes per frame |
303 | // 16 Bytes eingetroffen -> Komplett |
298 | if(FrameCnt >= 16) |
304 | if(FrameCnt >= 16) |
299 | { |
305 | { |
300 | // Frame complete |
306 | // Frame complete |
301 | if(Frame2 == 0) |
307 | if(Frame2 == 0) |
302 | { |
308 | { |
303 | // Null bedeutet: Neue Daten |
309 | // Null bedeutet: Neue Daten |
304 | // nur beim ersten Frame (CH 0-7) setzen |
310 | // nur beim ersten Frame (CH 0-7) setzen |
305 | NewPpmData = 0; |
311 | if(!ReSync) NewPpmData = 0; |
306 | } |
- | |
307 | - | ||
308 | // new frame next, nach fruehestens 7ms erwartet |
312 | } |
309 | FrameCnt = 0; |
313 | FrameCnt = 0; |
310 | Frame2 = 0; |
314 | Frame2 = 0; |
- | 315 | Sync = 0; |
|
311 | Sync = 0; |
316 | SpektrumTimer = MAX_FRAMEGAP; |
312 | } |
- | |
313 | // Zeit bis zum nächsten Zeichen messen |
- | |
314 | FrameTimer = SetDelay(7); |
317 | } |
315 | } |
318 | } |