Subversion Repositories Projects

Rev

Rev 780 | Rev 931 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 780 Rev 791
1
// ***************************************************************
1
// ***************************************************************
2
// ** Spektrum Diversity v1.0 - use up to 4 satellite receivers **
2
// ** Spektrum Diversity v1.3 - use up to 4 satellite receivers **
3
// ***************************************************************
3
// ***************************************************************
4
// ** Target: An Atmel ATtiny2313 (RC-Oscillator @ 8 MHz)       **
4
// ** Target: An Atmel ATtiny2313 (RC-Oscillator @ 8 MHz)       **
5
// **         controls a 74HC151 Multiplexer                    **
5
// **         controls a 74HC151 Multiplexer                    **
6
// ***************************************************************
6
// ***************************************************************
7
// ** It monitors the data from 4 satellite receivers and       **
7
// ** It monitors the data from 4 satellite receivers and       **
8
// ** connects a valid one to the output via the multiplexer    **
8
// ** connects a valid one to the output via the multiplexer    **
9
// ***************************************************************
9
// ***************************************************************
10
// ** LED-Modes during startup (in chronological order)         **
10
// ** LED-Modes during startup                                  **
11
// ** ************************                                  **
11
// ** ************************                                  **
12
// ** LED fast blink: Waiting for first datapulse               **
12
// ** LED fast flash: Waiting for first datapulse               **
13
// ** LED slow blink: Waiting 3 seconds while counting sat's    **
-
 
14
// ** LED flashes:    Indicates the number of active sat's      **
-
 
15
// **                                                           **
13
// **                                                           **
16
// ** LED-Modes during operation                                **
14
// ** LED-Modes during operation                                **
17
// ** **************************                                **
15
// ** **************************                                **
18
// ** LED OFF: Everything is fine                               **
16
// ** LED OFF: Everything is fine                               **
19
// ** LED ON:  FAILURE - The first selected sat had lost sync   **
17
// ** LED ON:  FAILURE - The first selected sat had lost sync   **
20
// **                                                           **
18
// **                                                           **
21
// ** LED-Modes after Self-Test                                 **
19
// ** LED-Modes after Self-Test and pressed button              **
22
// ** *************************                                 **
20
// ** ********************************************              **
23
// ** LED flashes at 1 Hz: Everything is fine                   **
21
// ** LED flashes at 1 Hz: Everything is fine                   **
24
// ** LED flashes some times: Times flashed -> damaged Channel# **
22
// ** LED flashes some times: Times flashed -> damaged Channel# **
25
// ** LED off: check voltage(regulator) or firmware             **
23
// ** LED off: check voltage(regulator), button or firmware     **
26
// ***************************************************************
24
// ***************************************************************
27
// ** (c) 2010-0xFFFF Stefan Pendsa                             **
25
// ** (c) 2010-0xFFFF Stefan Pendsa                             **
28
// ** License: don't know - use it and have fun                 **
26
// ** License: don't know - use it and have fun                 **
29
// ***************************************************************
27
// ***************************************************************
30
 
28
 
31
#include <avr/io.h>
29
#include <avr/io.h>
-
 
30
#include <avr/eeprom.h>
32
#include <avr/interrupt.h>
31
#include <avr/interrupt.h>
33
#include <util/delay.h>
32
#include <util/delay.h>
-
 
33
 
34
 
34
 
35
#define LED_OFF PORTD |= 1 << PD0
35
#define LED_OFF PORTD |= 1 << PD0
36
#define LED_ON  PORTD &= ~(1 << PD0)
36
#define LED_ON  PORTD &= ~(1 << PD0)
37
 
37
 
38
volatile unsigned int TimerEvent;
38
volatile unsigned int TimerEvent;
-
 
39
 
-
 
40
unsigned char eedummy EEMEM;                                                            // Dummy-Variable for Address 0 in EEPROM
-
 
41
unsigned char eecheck EEMEM;                                                            // Check-Variable
-
 
42
unsigned char bind;                                                                                     // Bind-Pulse-Counter (in RAM)
-
 
43
unsigned char eebind EEMEM;                                                                     // Bind-Pulse-Counter (in EEPROM)
39
 
44
 
40
 
45
 
41
 
46
 
42
ISR(TIMER1_OVF_vect)                                                                            // Triggered every 8,192 msec
47
ISR(TIMER1_OVF_vect)                                                                            // Triggered every 8,192 msec
43
{
48
{
44
        TimerEvent++;
49
        TimerEvent++;
45
}
50
}
46
 
51
 
47
 
52
 
48
 
53
 
49
void SelectSat(unsigned char sat)
54
void SelectSat(unsigned char sat)
50
{
55
{
51
        PORTD = (PORTD & 0b1100111) | (sat << 3);                               // Select the input for 74HC151
56
        PORTD = (PORTD & 0b1100111) | (sat << 3);                               // Select the input for 74HC151
52
        _delay_us(10);                                                                                  // Wait for stable state
57
        _delay_us(10);                                                                                  // Wait for stable state
53
}
58
}
54
 
59
 
55
 
60
 
56
 
61
 
57
void ResetTimer(void)
62
void ResetTimer(void)
58
{
63
{
59
        TCNT1H = 0;
64
        TCNT1H = 0;
60
        TCNT1L = 0;
65
        TCNT1L = 0;
61
        TimerEvent = 0;
66
        TimerEvent = 0;
62
}
67
}
63
 
68
 
64
 
69
 
65
 
70
 
66
void Binding(void)                                                                                      // Binding sequence for DX7
71
void Binding(void)                                                                                      // Binding sequence
67
{
72
{
68
        unsigned char i = 0;
73
        unsigned char i = 0;
69
 
74
 
70
        DDRB = 0b11110000;                                                                              // Sat-Pins to output
75
        DDRB = 0b11110000;                                                                              // Sat-Pins to output
71
        _delay_ms(80);                                                                                  // Let them time to boot up
76
        _delay_ms(80);                                                                                  // Let them time to boot up
72
       
77
       
73
        for (i=0;i<3;i++)                                                                               // send three negative 100µs pulses to all sat's
78
        for (i=0;i<bind;i++)                                                                    // Send negative 100µs pulses to all sat's
74
        {
79
        {
75
                PORTB = 0b00000000;
80
                PORTB = 0b00000000;
76
                _delay_us(100);
81
                _delay_us(100);
77
                PORTB = 0b11110000;
82
                PORTB = 0b11110000;
78
                _delay_us(100);
83
                _delay_us(100);
79
        }
84
        }
-
 
85
 
-
 
86
        for (i=0;i<bind;i++)                                                                    // Flash the number of used pulses to the LED
-
 
87
        {
80
 
88
                LED_ON;
-
 
89
                _delay_ms(100);
-
 
90
                LED_OFF;
-
 
91
                _delay_ms(250);
-
 
92
        }
81
        _delay_ms(1000);
93
 
-
 
94
        DDRB = 0b00000000;                                                                              // Sat-Pins to input again
-
 
95
       
-
 
96
        bind++;                                                                                                 // Save increased bind-pulse-counter for next binding.
-
 
97
        if (bind > 6) bind = 3;                                                                 // 3 pulses are for DX7+AR6200, 4-6 pulses have been seen...
-
 
98
        eeprom_write_byte(&eebind, bind);                                               // around the world for other combinations. Hopefully it works for them.
82
        DDRB = 0b00000000;                                                                              // Sat-Pins to input after 1sec
99
        _delay_ms(500);
83
}
100
}
84
 
101
 
85
 
102
 
86
 
103
 
87
void Testing(void)                                                                                      // Self Test
104
void Testing(void)                                                                                      // Self Test
88
{
105
{
89
        unsigned char error = 0;
106
        unsigned char error = 0;
90
        unsigned char i = 0;
107
        unsigned char i = 0;
91
 
108
 
92
        DDRB = 0b11110000;                                                                              // Port B Output for satellites, Input for feedback
109
        DDRB = 0b11110000;                                                                              // Port B Output for satellites, Input for feedback
93
 
110
 
94
        PORTB = 0b01010000;                                                                             // Test Pattern
111
        PORTB = 0b01010000;                                                                             // Test Pattern
95
        SelectSat(0);
112
        SelectSat(0);
96
        if (!(PINB & (1<<PB3))) error = 1;
113
        if (!(PINB & (1<<PB3))) error = 1;
97
        SelectSat(1);
114
        SelectSat(1);
98
        if (PINB & (1<<PB3)) error = 2;
115
        if (PINB & (1<<PB3)) error = 2;
99
        SelectSat(2);
116
        SelectSat(2);
100
        if (!(PINB & (1<<PB3))) error = 3;
117
        if (!(PINB & (1<<PB3))) error = 3;
101
        SelectSat(3);
118
        SelectSat(3);
102
        if (PINB & (1<<PB3)) error = 4;
119
        if (PINB & (1<<PB3)) error = 4;
103
 
120
 
104
        PORTB = 0b10100000;                                                                             // Another (inverted) Test Pattern
121
        PORTB = 0b10100000;                                                                             // Another (inverted) Test Pattern
105
        SelectSat(0);
122
        SelectSat(0);
106
        if (PINB & (1<<PB3)) error = 1;
123
        if (PINB & (1<<PB3)) error = 1;
107
        SelectSat(1);
124
        SelectSat(1);
108
        if (!(PINB & (1<<PB3))) error = 2;
125
        if (!(PINB & (1<<PB3))) error = 2;
109
        SelectSat(2);
126
        SelectSat(2);
110
        if (PINB & (1<<PB3)) error = 3;
127
        if (PINB & (1<<PB3)) error = 3;
111
        SelectSat(3);
128
        SelectSat(3);
112
        if (!(PINB & (1<<PB3))) error = 4;
129
        if (!(PINB & (1<<PB3))) error = 4;
113
 
130
 
114
        DDRB = 0b00000000;                                                                              // Port B Input again
131
        DDRB = 0b00000000;                                                                              // Port B Input again
-
 
132
 
-
 
133
        while (PIND & (1<<PD6));                                                                // Wait for Bind-Switch
115
 
134
 
116
        while(1)                                                                                                // Never return
135
        while(1)                                                                                                // Never return
117
        {
136
        {
118
                if (error == 0)                                                                         // When no error occured -> LED flashes at 1 Hz
137
                if (error == 0)                                                                         // When no error occured -> LED flashes at 1 Hz
119
                {
138
                {
120
                        LED_ON;
139
                        LED_ON;
121
                        _delay_ms(100);
140
                        _delay_ms(100);
122
                        LED_OFF;
141
                        LED_OFF;
123
                        _delay_ms(900);
142
                        _delay_ms(900);
124
                }
143
                }
125
                else
144
                else
126
                {
145
                {
127
                        for (i=0;i<error;i++)                                                   // When error occured -> Flash-Out the Errorcode
146
                        for (i=0;i<error;i++)                                                   // When error occured -> Flash-Out the Errorcode
128
                        {
147
                        {
129
                                LED_ON;
148
                                LED_ON;
130
                                _delay_ms(100);
149
                                _delay_ms(100);
131
                                LED_OFF;
150
                                LED_OFF;
132
                                _delay_ms(400);
151
                                _delay_ms(400);
133
                        }
152
                        }
134
                        _delay_ms(1000);
153
                        _delay_ms(1000);
135
                }
154
                }
136
 
155
 
137
        }
156
        }
138
 
157
 
139
}
158
}
140
 
159
 
141
 
160
 
142
 
161
 
143
int main(void)
162
int main(void)
144
{
163
{
145
        unsigned char i = 0;
164
        unsigned char i = 0;
146
        unsigned char j = 0;
-
 
147
        unsigned char active[4];
165
        unsigned char active[4];
148
        unsigned char sat = 4;
166
        unsigned char sat = 99;
149
 
167
 
150
        DDRB = 0b00000000;                                                                              // Port B Input for satellites and feedback
168
        DDRB = 0b00000000;                                                                              // Port B Input for satellites and feedback
151
        DDRD = 0b0011001;                                                                               // Port D Output for MUX and LED, Input for Switch & Test
169
        DDRD = 0b0011001;                                                                               // Port D Output for MUX and LED, Input for Switch & Test
152
        PORTB = 0b11110000;                                                                             // Port B Pullup's for (unused) satellites
170
        PORTB = 0b11110000;                                                                             // Port B Pullup's for (unused) satellites
153
        PORTD = 0b1100001;                                                                              // Port D Pullup's for Switch & Test, LED off
171
        PORTD = 0b1100001;                                                                              // Port D Pullup's for Switch & Test, LED off
154
 
172
 
155
        for (i=0;i<4;i++) active[i] = 0;                                                // Reset active-array
173
        for (i=0;i<4;i++) active[i] = 0;                                                // Reset active-array
-
 
174
 
-
 
175
 
-
 
176
    if (eeprom_read_byte(&eecheck) != 0x42)                                     // Invalid Data in EEPROM -> initialize
-
 
177
        {
-
 
178
                eeprom_write_byte(&eecheck, 0x42);
-
 
179
                bind = 3;
-
 
180
                eeprom_write_byte(&eebind, bind);
-
 
181
        }
-
 
182
        else bind = eeprom_read_byte(&eebind);                                  // Valid Data in EEPROM -> read bind-pulse-counter
-
 
183
 
156
 
184
 
157
        if (!(PIND & (1<<PD5))) Testing();                                              // Initiate Self-Test when Test-Pad is low
185
        if (!(PIND & (1<<PD5))) Testing();                                              // Initiate Self-Test when Test-Pad is low
158
        if (!(PIND & (1<<PD6))) Binding();                                              // Initiate Binding when Bind-Button is pressed
186
        if (!(PIND & (1<<PD6))) Binding();                                              // Initiate Binding when Bind-Button is pressed
159
 
187
 
160
        _delay_ms(100);
188
        _delay_ms(100);
161
 
189
 
162
        TCCR1B = ( 1 << CS10 );                                                                 // Timer1 Prescaler = 1 -> 8,192 msec
190
        TCCR1B = ( 1 << CS10 );                                                                 // Timer1 Prescaler = 1 -> 8,192 msec
163
        TIMSK = ( 1 << TOIE1 );                                                                 // Timer1 Overflow Interrupt Enable
191
        TIMSK = ( 1 << TOIE1 );                                                                 // Timer1 Overflow Interrupt Enable
164
        sei();                                                                                                  // Global Interrupts enable
192
        sei();                                                                                                  // Global Interrupts enable
165
 
193
 
166
        ResetTimer();
194
        ResetTimer();
167
        while((PINB & 0b11110000) == 0b11110000)                                // Wait for first signal (SYNC to frame)
195
        while(sat == 99)                                                                                // Wait for first signal
168
        {
196
        {
169
                if (TimerEvent == 10) LED_ON;                                           // Blink while waiting...
197
                if (TimerEvent == 10) LED_ON;                                           // Blink while waiting...
170
                if (TimerEvent == 20)
198
                if (TimerEvent == 20)
171
                {
199
                {
172
                        LED_OFF;
200
                        LED_OFF;
173
                        TimerEvent = 0;
201
                        TimerEvent = 0;
174
                }
202
                }
175
        }
-
 
176
        LED_OFF;
-
 
177
 
-
 
178
        ResetTimer();
-
 
179
        while(1)                                                                                                // Check active satellites for 5 seconds
-
 
180
        {
-
 
181
                if (TimerEvent == 20) LED_ON;                                           // Blink (slower) while waiting...
-
 
182
                if (TimerEvent == 40)
-
 
183
                {
-
 
184
                        LED_OFF;
-
 
185
                        TimerEvent = 0;
-
 
186
                        j++;
-
 
187
                }
-
 
188
 
203
 
189
                for (i=0;i<4;i++)
204
                for (i=0;i<4;i++)                                                                       // Select first active Satellite
190
                {
205
                {
191
                        if (!(PINB & (1<<(i+4))))
206
                        if (!(PINB & (1<<(i+4))))
192
                        {
207
                        {
193
                                active[i] = 1;
208
                                active[i] = 1;
194
                                if (sat == 4)                                                           // Select first active satellite (only once)
-
 
195
                                {
-
 
196
                                        sat = i;
209
                                sat = i;
197
                                        SelectSat(i);
210
                                SelectSat(sat);
198
                                }
211
                                break;
199
 
-
 
200
                        }
212
                        }
201
                }
213
                }
202
               
-
 
203
                if (j == 16) break;                                                                     // 16 * 40 * 8ms = ~5sec
-
 
204
        }
214
        }
205
 
215
 
206
        LED_OFF;
216
        LED_OFF;
207
        _delay_ms(1000);
-
 
208
 
-
 
209
        for (i=0;i<4;i++)                                                                               // Flash once for every active satellite
-
 
210
        {
-
 
211
                if (active[i] == 1)
-
 
212
                {
-
 
213
                        LED_ON;
-
 
214
                        _delay_ms(100);
-
 
215
                        LED_OFF;
-
 
216
                        _delay_ms(400);
-
 
217
                }
-
 
218
        }
-
 
219
 
-
 
220
 
-
 
221
 
-
 
222
 
217
 
223
        while(1)                                                                                                // Main-Loop
218
        while(1)                                                                                                // Main-Loop
224
        {
219
        {
225
                for (i=0;i<4;i++) active[i] = 0;                                        // Reset active-array
220
                for (i=0;i<4;i++) active[i] = 0;                                        // Reset active-array
226
 
221
 
227
                ResetTimer();
222
                ResetTimer();
228
                while((PINB & 0b11110000) == 0b11110000)                        // Wait for first signal (SYNC to frame)
223
                while((PINB & 0b11110000) == 0b11110000)                        // Wait for first signal (SYNC to frame)
229
                {
224
                {
230
                        if (TimerEvent > 3) break;                                              // (max. 3*8=24ms)
225
                        if (TimerEvent > 3) break;                                              // (max. 3*8=24ms)
231
                }
226
                }
232
               
227
               
233
                ResetTimer();
228
                ResetTimer();
234
                while(TimerEvent < 1)                                                           // Check active satellites (for 1*8=8ms)
229
                while(TimerEvent < 1)                                                           // Check active satellites (for 1*8=8ms)
235
                {
230
                {
236
 
-
 
237
                        for (i=0;i<4;i++)
231
                        for (i=0;i<4;i++)
238
                        {
232
                        {
239
                                if (!(PINB & (1<<(i+4))))
233
                                if (!(PINB & (1<<(i+4))))
240
                                {
234
                                {
241
                                        active[i] = 1;
235
                                        active[i] = 1;
242
                                }
236
                                }
243
                        }
237
                        }
244
                }
238
                }
245
 
239
 
246
                if (active[sat] == 0)                                                           // Detect fail on active satellite
240
                if (active[sat] == 0)                                                           // Detect fail on active satellite
247
                {
241
                {
248
                        LED_ON;                                                                                 // Failure-LED ON
-
 
249
                        for (i=0;i<4;i++)                                                               // Select lowest active satellite
242
                        for (i=0;i<4;i++)                                                               // Select lowest active satellite
250
                        {
243
                        {
251
                                if (active[i] == 1)
244
                                if (active[i] == 1)
252
                                {
245
                                {
253
                                        sat = i;
246
                                        sat = i;
254
                                        SelectSat(sat);
247
                                        SelectSat(sat);
255
                                        break;
248
                                        break;
256
                                }
249
                                }
257
                        }
250
                        }
-
 
251
                        LED_ON;                                                                                 // Failure-LED ON
258
                }
252
                }
259
 
253
 
260
                if (!(PIND & (1<<PD6))) LED_OFF;                                        // Reset Failure-LED when Bind-Button is pressed
254
                if (!(PIND & (1<<PD6))) LED_OFF;                                        // Reset Failure-LED when Bind-Button is pressed
261
        }
255
        }
262
 
256
 
263
}
257
}
264
 
258