Subversion Repositories FlightCtrl

Rev

Rev 1221 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1221 Rev 1222
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"
Line -... Line 6...
-
 
6
#include "main.h"
Line -... Line 7...
-
 
7
 
-
 
8
//--------------------------------------------------------------//
-
 
9
 
-
 
10
//--------------------------------------------------------------//
-
 
11
void SpektrumBinding(void)
-
 
12
{
-
 
13
  unsigned int timerTimeout = SetDelay(10000);  // Timeout 10 sec.
-
 
14
  unsigned char connected = 0;
-
 
15
  unsigned int delaycounter;
-
 
16
 
-
 
17
  UCSR1B &= ~(1 << RXCIE1);  // disable rx-interrupt
-
 
18
  UCSR1B &= ~(1<<RXEN1);     // disable Uart-Rx
-
 
19
  PORTD &= ~(1 << PORTD2);   // disable pull-up
-
 
20
 
-
 
21
  printf("\n\rPlease connect Spektrum receiver for binding NOW...");
-
 
22
 
-
 
23
  while(!CheckDelay(timerTimeout))
-
 
24
  {
-
 
25
    if (PIND & (1 << PORTD2)) { timerTimeout = SetDelay(90); connected = 1; break; }
-
 
26
  }
-
 
27
 
-
 
28
  if (connected)
-
 
29
  {
-
 
30
   
-
 
31
    printf("ok.\n\r");
-
 
32
    DDRD |= (1 << DDD2);     // Rx as output
-
 
33
 
-
 
34
        while(!CheckDelay(timerTimeout));  // delay after startup of RX
-
 
35
        for (delaycounter = 0; delaycounter < 100; delaycounter++) PORTD |= (1 << PORTD2);
-
 
36
        for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2);
-
 
37
       
-
 
38
        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);
-
 
41
        for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2);
-
 
42
     
-
 
43
        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);
-
 
46
        for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2);
-
 
47
 
-
 
48
        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);
-
 
51
 
-
 
52
  }
-
 
53
   else
-
 
54
  { printf("Timeout.\n\r");
-
 
55
 
-
 
56
 
-
 
57
  }
-
 
58
   
-
 
59
  DDRD &= ~(1 << DDD2);      // RX as input
-
 
60
  PORTD &= ~(1 << PORTD2);  
-
 
61
 
-
 
62
  Uart1Init();    // init Uart again
-
 
63
}
-
 
64
 
-
 
65
//############################################################################
-
 
66
// zum Decodieren des Spektrum Satelliten wird USART1 benutzt.
-
 
67
// USART1 initialisation from killagreg
-
 
68
void Uart1Init(void)
-
 
69
//############################################################################
-
 
70
    {
-
 
71
        // -- Start of USART1 initialisation for Spekturm seriell-mode
-
 
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);
-
 
74
        // disable RX-Interrupt
-
 
75
        UCSR1B &= ~(1 << RXCIE1);
-
 
76
        // disable TX-Interrupt
-
 
77
        UCSR1B &= ~(1 << TXCIE1);
-
 
78
        // disable DRE-Interrupt
-
 
79
        UCSR1B &= ~(1 << UDRIE1);
-
 
80
        // set direction of RXD1 and TXD1 pins
-
 
81
        // set RXD1 (PD2) as an input pin
-
 
82
        PORTD |= (1 << PORTD2);
-
 
83
        DDRD &= ~(1 << DDD2);
-
 
84
        // USART0 Baud Rate Register
-
 
85
        // set clock divider
-
 
86
       
-
 
87
        UBRR1H = (uint8_t)(ubrr>>8);
-
 
88
        UBRR1L = (uint8_t)ubrr;
-
 
89
        // enable double speed operation
-
 
90
        UCSR1A |= (1 << U2X1);
-
 
91
        // enable receiver and transmitter
-
 
92
        //UCSR1B = (1<<RXEN1)|(1<<TXEN1);
-
 
93
 
-
 
94
 
-
 
95
       
-
 
96
       
-
 
97
        UCSR1B = (1<<RXEN1);
-
 
98
        // set asynchronous mode
-
 
99
        UCSR1C &= ~(1 << UMSEL11);
-
 
100
        UCSR1C &= ~(1 << UMSEL10);
-
 
101
        // no parity
-
 
102
        UCSR1C &= ~(1 << UPM11);
-
 
103
        UCSR1C &= ~(1 << UPM10);
-
 
104
        // 1 stop bit
-
 
105
        UCSR1C &= ~(1 << USBS1);
-
 
106
        // 8-bit
-
 
107
        UCSR1B &= ~(1 << UCSZ12);
-
 
108
        UCSR1C |=  (1 << UCSZ11);
-
 
109
        UCSR1C |=  (1 << UCSZ10);
-
 
110
        // flush receive buffer explicit
-
 
111
        while(UCSR1A & (1<<RXC1)) UDR1;
-
 
112
        // enable RX-interrupts at the end
-
 
113
        UCSR1B |= (1 << RXCIE1);
-
 
114
        // -- End of USART1 initialisation
6
#include rc.h
115
  return;
7
 
116
 }
8
 
117
       
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
118
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10
// + Copyright (c) Rainer Walther
119
// + Copyright (c) Rainer Walther
Line 51... Line 160...
51
//      byte5:  and byte6:  channel data
160
//      byte5:  and byte6:  channel data
52
//      byte7:  and byte8:  channel data
161
//      byte7:  and byte8:  channel data
53
//      byte9:  and byte10: channel data
162
//      byte9:  and byte10: channel data
54
//      byte11: and byte12: channel data
163
//      byte11: and byte12: channel data
55
//      byte13: and byte14: channel data
164
//      byte13: and byte14: channel data
56
//      byte15: and byte16: channel data
165
//      byte15: and byte16: channel data 
57
//  2nd Frame:
166
//  2nd Frame:
58
//              byte1:  unknown
167
//              byte1:  unknown
59
//      byte2:  unknown
168
//      byte2:  unknown
60
//              byte3:  and byte4:  channel data
169
//              byte3:  and byte4:  channel data
61
//      byte5:  and byte6:  channel data
170
//      byte5:  and byte6:  channel data
Line 74... Line 183...
74
// 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)
75
// 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
76
//
185
//
77
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
186
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Line 78... Line -...
78
 
-
 
-
 
187
 
-
 
188
//############################################################################
79
 
189
//Diese Routine startet und inizialisiert den USART1 für seriellen Spektrum satellite reciever
80
void spectrum_parser(uint8_t c)
-
 
-
 
190
SIGNAL(USART1_RX_vect)
81
 
191
//############################################################################
82
{
192
{
83
        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;
84
        unsigned int Channel, index;
194
        unsigned int Channel, index;
85
        signed int signal, tmp;
195
        signed int signal, tmp;
-
 
196
        int bCheckDelay;
86
        int bCheckDelay;
197
        uint8_t c;
-
 
198
       
-
 
199
        c = UDR1; // get data byte
87
 
200
       
88
        if (ReSync == 1)
201
        if (ReSync == 1)
89
            {
202
            {
90
                // wait for beginning of new frame
203
                // wait for beginning of new frame
91
                ReSync = 0;
204
                ReSync = 0;
92
 
205
               
93
                FrameTimer = SetDelay(7);  // minimum 7ms zwischen den frames
206
                FrameTimer = SetDelay(7);  // minimum 7ms zwischen den frames
94
                FrameCnt = 0;
207
                FrameCnt = 0;
95
                Sync = 0;
208
                Sync = 0;
96
                ByteHigh = 0;
209
                ByteHigh = 0;
97
                }
210
                }
98
  else
211
  else
99
  {
212
  {    
100
        bCheckDelay = CheckDelay(FrameTimer);
213
        bCheckDelay = CheckDelay(FrameTimer);
101
        if ( Sync == 0 )
214
        if ( Sync == 0 )
102
            {
215
            {
103
                if(bCheckDelay)
216
                if(bCheckDelay)
Line 121... Line 234...
121
                }
234
                }
122
        else if((Sync == 2) && !bCheckDelay)
235
        else if((Sync == 2) && !bCheckDelay)
123
            {
236
            {
124
                // Datenbyte high
237
                // Datenbyte high
125
                ByteHigh = c;
238
                ByteHigh = c;
126
 
239
               
127
                if (FrameCnt == 2)
240
                if (FrameCnt == 2)
128
                    {
241
                    {
129
                    // is 1st Byte of Channel-data
242
                    // is 1st Byte of Channel-data
130
                        // Frame 1 with Channel 1-7 comming next
243
                        // Frame 1 with Channel 1-7 comming next
131
                        Frame2 = 0;
244
                        Frame2 = 0;
132
                        if(ByteHigh & 0x80)
245
                        if(ByteHigh & 0x80)
133
                            {
246
                            {
134
                                // DS9: Frame 2 with Channel 8-9 comming next
247
                                // DS9: Frame 2 with Channel 8-9 comming next
135
                                Frame2 = 1;
248
                                Frame2 = 1;
136
                                }
249
                                }
137
                        }
250
                        }      
138
                Sync = 3;
251
                Sync = 3;
139
                FrameCnt ++;
252
                FrameCnt ++;
140
                }
253
                }
141
        else if((Sync == 3) && !bCheckDelay)
254
        else if((Sync == 3) && !bCheckDelay)
142
            {
255
            {
143
                // Datenbyte low
256
                // Datenbyte low
144
 
257
               
145
                // High-Byte for next channel comes next
258
                // High-Byte for next channel comes next
146
                Sync = 2;
259
                Sync = 2;
147
                FrameCnt ++;
260
                FrameCnt ++;
148
 
261
               
149
                index = (ByteHigh >> 2) & 0x0f;
262
                index = (ByteHigh >> 2) & 0x0f;
150
                index ++;
263
                index ++;
151
                Channel = (ByteHigh << 8) | c;
264
                Channel = (ByteHigh << 8) | c;
152
                signal = Channel & 0x3ff;
265
                signal = Channel & 0x3ff;
153
                signal -= 0x200;                // Offset, range 0x000..0x3ff?
266
                signal -= 0x200;                // Offset, range 0x000..0x3ff?
154
                signal = signal/3;              // scaling to fit PPM resolution
267
                signal = signal/3;              // scaling to fit PPM resolution
155
 
268
               
156
                if(index >= 0  &&  index <= 10)
269
                if(index >= 0  &&  index <= 10)
157
                    {
270
                    {
158
                // Stabiles Signal
271
                // Stabiles Signal
159
                if(abs(signal - PPM_in[index]) < 6) { if(SenderOkay < 200) SenderOkay += 10; else SenderOkay = 200;}
272
                if(abs(signal - PPM_in[index]) < 6) { if(SenderOkay < 200) SenderOkay += 10; else SenderOkay = 200;}
160
                tmp = (3 * (PPM_in[index]) + signal) / 4;
273
                tmp = (3 * (PPM_in[index]) + signal) / 4;  
161
                if(tmp > signal+1) tmp--; else
274
                if(tmp > signal+1) tmp--; else
162
                if(tmp < signal-1) tmp++;
275
                if(tmp < signal-1) tmp++;
163
                if(SenderOkay >= 180)  PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3;
276
                if(SenderOkay >= 180)  PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3;
164
                else PPM_diff[index] = 0;
277
                else PPM_diff[index] = 0;
165
                PPM_in[index] = tmp;
278
                PPM_in[index] = tmp;
166
                        }
279
                        }
167
                }
280
                }
168
        else
281
        else
Line 170... Line 283...
170
                // hier stimmt was nicht: neu synchronisieren
283
                // hier stimmt was nicht: neu synchronisieren
171
                ReSync = 1;
284
                ReSync = 1;
172
                FrameCnt = 0;
285
                FrameCnt = 0;
173
                Frame2 = 0;
286
                Frame2 = 0;
174
                }
287
                }
175
 
288
               
176
        // 16 Bytes per frame
289
        // 16 Bytes per frame
177
        if(FrameCnt >= 16)
290
        if(FrameCnt >= 16)
178
            {
291
            {
179
                // Frame complete
292
                // Frame complete
180
                if(Frame2 == 0)
293
                if(Frame2 == 0)
181
                        {
294
                        {
182
                        // Null bedeutet: Neue Daten
295
                        // Null bedeutet: Neue Daten
183
                        // nur beim ersten Frame (CH 0-7) setzen
296
                        // nur beim ersten Frame (CH 0-7) setzen
184
                        NewPpmData = 0;
297
                        NewPpmData = 0;  
185
                        }
298
                        }
186
 
299
               
187
                // new frame next, nach fruehestens 7ms erwartet
300
                // new frame next, nach fruehestens 7ms erwartet
188
                FrameCnt = 0;
301
                FrameCnt = 0;
189
                Frame2 = 0;
302
                Frame2 = 0;
190
                Sync = 0;
303
                Sync = 0;
191
                }
304
                }
192
        // Zeit bis zum nächsten Zeichen messen
305
        // Zeit bis zum nächsten Zeichen messen
193
        FrameTimer = SetDelay(7);
306
        FrameTimer = SetDelay(7);
194
   }
307
   }
195
}
308
}