Subversion Repositories Projects

Rev

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

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