Rev 1180 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1180 | Rev 1201 | ||
---|---|---|---|
Line 1... | Line 1... | ||
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 | #######################################################################################*/ |
Line 4... | Line 4... | ||
4 | 4 | ||
5 | #include "Spectrum.h" |
5 | #include "spectrum.h" |
- | 6 | #include rc.h |
|
Line 6... | Line -... | ||
6 | #include "main.h" |
- | |
7 | - | ||
8 | //############################################################################ |
- | |
9 | // zum Decodieren des Spektrum Satelliten wird USART1 benutzt. |
- | |
10 | // USART1 initialisation from killagreg |
- | |
11 | void Uart1Init(void) |
- | |
12 | //############################################################################ |
- | |
13 | { |
- | |
14 | // -- Start of USART1 initialisation for Spekturm seriell-mode |
- | |
15 | // USART1 Control and Status Register A, B, C and baud rate register |
- | |
16 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * 115200) - 1); |
- | |
17 | // disable RX-Interrupt |
- | |
18 | UCSR1B &= ~(1 << RXCIE1); |
- | |
19 | // disable TX-Interrupt |
- | |
20 | UCSR1B &= ~(1 << TXCIE1); |
- | |
21 | // disable DRE-Interrupt |
- | |
22 | UCSR1B &= ~(1 << UDRIE1); |
- | |
23 | // set direction of RXD1 and TXD1 pins |
- | |
24 | // set RXD1 (PD2) as an input pin |
- | |
25 | PORTD |= (1 << PORTD2); |
- | |
26 | DDRD &= ~(1 << DDD2); |
- | |
27 | // USART0 Baud Rate Register |
- | |
28 | // set clock divider |
- | |
29 | UBRR1H = (uint8_t)(ubrr>>8); |
- | |
30 | UBRR1L = (uint8_t)ubrr; |
- | |
31 | // enable double speed operation |
- | |
32 | UCSR1A |= (1 << U2X1); |
- | |
33 | // enable receiver and transmitter |
- | |
34 | //UCSR1B = (1<<RXEN1)|(1<<TXEN1); |
- | |
35 | UCSR1B = (1<<RXEN1); |
- | |
36 | // set asynchronous mode |
- | |
37 | UCSR1C &= ~(1 << UMSEL11); |
- | |
38 | UCSR1C &= ~(1 << UMSEL10); |
- | |
39 | // no parity |
- | |
40 | UCSR1C &= ~(1 << UPM11); |
- | |
41 | UCSR1C &= ~(1 << UPM10); |
- | |
42 | // 1 stop bit |
- | |
43 | UCSR1C &= ~(1 << USBS1); |
- | |
44 | // 8-bit |
- | |
45 | UCSR1B &= ~(1 << UCSZ12); |
- | |
46 | UCSR1C |= (1 << UCSZ11); |
- | |
47 | UCSR1C |= (1 << UCSZ10); |
- | |
48 | // flush receive buffer explicit |
- | |
49 | while(UCSR1A & (1<<RXC1)) UDR1; |
- | |
50 | // enable RX-interrupts at the end |
- | |
51 | UCSR1B |= (1 << RXCIE1); |
- | |
52 | // -- End of USART1 initialisation |
- | |
53 | return; |
- | |
54 | } |
7 | |
55 | 8 | ||
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
57 | // + Copyright (c) Rainer Walther |
10 | // + Copyright (c) Rainer Walther |
58 | // + RC-routines from original MK rc.c (c) H&I |
11 | // + RC-routines from original MK rc.c (c) H&I |
Line 98... | Line 51... | ||
98 | // byte5: and byte6: channel data |
51 | // byte5: and byte6: channel data |
99 | // byte7: and byte8: channel data |
52 | // byte7: and byte8: channel data |
100 | // byte9: and byte10: channel data |
53 | // byte9: and byte10: channel data |
101 | // byte11: and byte12: channel data |
54 | // byte11: and byte12: channel data |
102 | // byte13: and byte14: channel data |
55 | // byte13: and byte14: channel data |
103 | // byte15: and byte16: channel data |
56 | // byte15: and byte16: channel data |
104 | // 2nd Frame: |
57 | // 2nd Frame: |
105 | // byte1: unknown |
58 | // byte1: unknown |
106 | // byte2: unknown |
59 | // byte2: unknown |
107 | // byte3: and byte4: channel data |
60 | // byte3: and byte4: channel data |
108 | // byte5: and byte6: channel data |
61 | // byte5: and byte6: channel data |
Line 121... | Line 74... | ||
121 | // C3 to C0 is the channel number. 0 to 9 (4 bit, as assigned in the transmitter) |
74 | // C3 to C0 is the channel number. 0 to 9 (4 bit, as assigned in the transmitter) |
122 | // D9 to D0 is the channel data (10 bit) 0xaa..0x200..0x356 for 100% transmitter-travel |
75 | // D9 to D0 is the channel data (10 bit) 0xaa..0x200..0x356 for 100% transmitter-travel |
123 | // |
76 | // |
124 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
77 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
Line 125... | Line -... | ||
125 | - | ||
126 | //############################################################################ |
- | |
- | 78 | ||
127 | //Diese Routine startet und inizialisiert den USART1 für seriellen Spektrum satellite reciever |
79 | |
128 | SIGNAL(USART1_RX_vect) |
- | |
- | 80 | void spectrum_parser(uint8_t c) |
|
129 | //############################################################################ |
81 | |
130 | { |
82 | { |
131 | static unsigned int Sync=0, FrameCnt=0, ByteHigh=0, ReSync=1, Frame2=0, FrameTimer; |
83 | static unsigned int Sync=0, FrameCnt=0, ByteHigh=0, ReSync=1, Frame2=0, FrameTimer; |
132 | unsigned int Channel, index; |
84 | unsigned int Channel, index; |
133 | signed int signal, tmp; |
85 | signed int signal, tmp; |
134 | int bCheckDelay; |
- | |
135 | uint8_t c; |
86 | int bCheckDelay; |
136 | - | ||
137 | c = UDR1; // get data byte |
- | |
138 | 87 | ||
139 | if (ReSync == 1) |
88 | if (ReSync == 1) |
140 | { |
89 | { |
141 | // wait for beginning of new frame |
90 | // wait for beginning of new frame |
142 | ReSync = 0; |
91 | ReSync = 0; |
143 | 92 | ||
144 | FrameTimer = SetDelay(7); // minimum 7ms zwischen den frames |
93 | FrameTimer = SetDelay(7); // minimum 7ms zwischen den frames |
145 | FrameCnt = 0; |
94 | FrameCnt = 0; |
146 | Sync = 0; |
95 | Sync = 0; |
147 | ByteHigh = 0; |
96 | ByteHigh = 0; |
148 | } |
97 | } |
149 | else |
98 | else |
150 | { |
99 | { |
151 | bCheckDelay = CheckDelay(FrameTimer); |
100 | bCheckDelay = CheckDelay(FrameTimer); |
152 | if ( Sync == 0 ) |
101 | if ( Sync == 0 ) |
153 | { |
102 | { |
154 | if(bCheckDelay) |
103 | if(bCheckDelay) |
Line 172... | Line 121... | ||
172 | } |
121 | } |
173 | else if((Sync == 2) && !bCheckDelay) |
122 | else if((Sync == 2) && !bCheckDelay) |
174 | { |
123 | { |
175 | // Datenbyte high |
124 | // Datenbyte high |
176 | ByteHigh = c; |
125 | ByteHigh = c; |
177 | 126 | ||
178 | if (FrameCnt == 2) |
127 | if (FrameCnt == 2) |
179 | { |
128 | { |
180 | // is 1st Byte of Channel-data |
129 | // is 1st Byte of Channel-data |
181 | // Frame 1 with Channel 1-7 comming next |
130 | // Frame 1 with Channel 1-7 comming next |
182 | Frame2 = 0; |
131 | Frame2 = 0; |
183 | if(ByteHigh & 0x80) |
132 | if(ByteHigh & 0x80) |
184 | { |
133 | { |
185 | // DS9: Frame 2 with Channel 8-9 comming next |
134 | // DS9: Frame 2 with Channel 8-9 comming next |
186 | Frame2 = 1; |
135 | Frame2 = 1; |
187 | } |
136 | } |
188 | } |
137 | } |
189 | Sync = 3; |
138 | Sync = 3; |
190 | FrameCnt ++; |
139 | FrameCnt ++; |
191 | } |
140 | } |
192 | else if((Sync == 3) && !bCheckDelay) |
141 | else if((Sync == 3) && !bCheckDelay) |
193 | { |
142 | { |
194 | // Datenbyte low |
143 | // Datenbyte low |
195 | 144 | ||
196 | // High-Byte for next channel comes next |
145 | // High-Byte for next channel comes next |
197 | Sync = 2; |
146 | Sync = 2; |
198 | FrameCnt ++; |
147 | FrameCnt ++; |
199 | 148 | ||
200 | index = (ByteHigh >> 2) & 0x0f; |
149 | index = (ByteHigh >> 2) & 0x0f; |
201 | index ++; |
150 | index ++; |
202 | Channel = (ByteHigh << 8) | c; |
151 | Channel = (ByteHigh << 8) | c; |
203 | signal = Channel & 0x3ff; |
152 | signal = Channel & 0x3ff; |
204 | signal -= 0x200; // Offset, range 0x000..0x3ff? |
153 | signal -= 0x200; // Offset, range 0x000..0x3ff? |
205 | signal = signal/3; // scaling to fit PPM resolution |
154 | signal = signal/3; // scaling to fit PPM resolution |
206 | 155 | ||
207 | if(index >= 0 && index <= 10) |
156 | if(index >= 0 && index <= 10) |
208 | { |
157 | { |
209 | // Stabiles Signal |
158 | // Stabiles Signal |
210 | if(abs(signal - PPM_in[index]) < 6) { if(SenderOkay < 200) SenderOkay += 10; else SenderOkay = 200;} |
159 | if(abs(signal - PPM_in[index]) < 6) { if(SenderOkay < 200) SenderOkay += 10; else SenderOkay = 200;} |
211 | tmp = (3 * (PPM_in[index]) + signal) / 4; |
160 | tmp = (3 * (PPM_in[index]) + signal) / 4; |
212 | if(tmp > signal+1) tmp--; else |
161 | if(tmp > signal+1) tmp--; else |
213 | if(tmp < signal-1) tmp++; |
162 | if(tmp < signal-1) tmp++; |
214 | if(SenderOkay >= 180) PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3; |
163 | if(SenderOkay >= 180) PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3; |
215 | else PPM_diff[index] = 0; |
164 | else PPM_diff[index] = 0; |
216 | PPM_in[index] = tmp; |
165 | PPM_in[index] = tmp; |
217 | } |
166 | } |
218 | } |
167 | } |
219 | else |
168 | else |
Line 221... | Line 170... | ||
221 | // hier stimmt was nicht: neu synchronisieren |
170 | // hier stimmt was nicht: neu synchronisieren |
222 | ReSync = 1; |
171 | ReSync = 1; |
223 | FrameCnt = 0; |
172 | FrameCnt = 0; |
224 | Frame2 = 0; |
173 | Frame2 = 0; |
225 | } |
174 | } |
226 | 175 | ||
227 | // 16 Bytes per frame |
176 | // 16 Bytes per frame |
228 | if(FrameCnt >= 16) |
177 | if(FrameCnt >= 16) |
229 | { |
178 | { |
230 | // Frame complete |
179 | // Frame complete |
231 | if(Frame2 == 0) |
180 | if(Frame2 == 0) |
232 | { |
181 | { |
233 | // Null bedeutet: Neue Daten |
182 | // Null bedeutet: Neue Daten |
234 | // nur beim ersten Frame (CH 0-7) setzen |
183 | // nur beim ersten Frame (CH 0-7) setzen |
235 | NewPpmData = 0; |
184 | NewPpmData = 0; |
236 | } |
185 | } |
237 | 186 | ||
238 | // new frame next, nach fruehestens 7ms erwartet |
187 | // new frame next, nach fruehestens 7ms erwartet |
239 | FrameCnt = 0; |
188 | FrameCnt = 0; |
240 | Frame2 = 0; |
189 | Frame2 = 0; |
241 | Sync = 0; |
190 | Sync = 0; |
242 | } |
191 | } |
243 | // Zeit bis zum nächsten Zeichen messen |
192 | // Zeit bis zum nächsten Zeichen messen |
244 | FrameTimer = SetDelay(7); |
193 | FrameTimer = SetDelay(7); |
245 | } |
194 | } |
246 | } |
195 | } |