Subversion Repositories Projects

Rev

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

Rev 775 Rev 776
1
// ************************************************************
1
// ************************************************************
2
// ** Spektrum Diversity - use up to 8 satellite receivers   **
2
// ** Spektrum Diversity - 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 8 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
// ** (c) 2010-0xFFFF Stefan Pendsa                          **
21
// ** (c) 2010-0xFFFF Stefan Pendsa                          **
22
// ** License: don't know - use it and have fun              **
22
// ** License: don't know - use it and have fun              **
23
// ************************************************************
23
// ************************************************************
24
 
24
 
25
#include <avr/io.h>
25
#include <avr/io.h>
26
#include <avr/interrupt.h>
26
#include <avr/interrupt.h>
27
#include <util/delay.h>
27
#include <util/delay.h>
28
 
28
 
29
#define LED_OFF PORTD |= 1 << PORTD3
29
#define LED_OFF PORTD |= 1 << PORTD0
30
#define LED_ON  PORTD &= ~(1 << PORTD3)
-
 
31
 
30
#define LED_ON  PORTD &= ~(1 << PORTD0)
32
 
31
 
33
volatile unsigned int TimerEvent;
32
volatile unsigned int TimerEvent;
34
 
33
 
35
 
34
 
36
void SelectSat(unsigned char sat)
35
void SelectSat(unsigned char sat)
37
{
36
{
38
        /*
-
 
39
        Re-Assignment due to easier routing on layout: sat -> mux-channel# -> mux-input (seen from µC)
-
 
40
        0 -> 2 -> 2
-
 
41
        1 -> 1 -> 4
-
 
42
        2 -> 0 -> 0
-
 
43
        3 -> 3 -> 6
-
 
44
        4 -> 4 -> 1
-
 
45
        5 -> 5 -> 5
-
 
46
        6 -> 6 -> 3
-
 
47
        7 -> 7 -> 7
-
 
48
        */
-
 
49
 
-
 
50
 
-
 
51
        if (sat == 0) sat = 2;
-
 
52
        else if (sat == 1) sat = 4;
-
 
53
        else if (sat == 2) sat = 0;
-
 
54
        else if (sat == 3) sat = 6;
-
 
55
        else if (sat == 4) sat = 1;
-
 
56
        else if (sat == 5) sat = 5;
-
 
57
        else if (sat == 6) sat = 3;
-
 
58
        else if (sat == 7) sat = 7;
-
 
59
 
-
 
60
 
-
 
61
        PORTD = (PORTD & 0b00001000) | sat;
37
        PORTD = (PORTD & 0b00000001) | (sat<<3);                                // Select the Input for 74HC151
62
}
38
}
63
 
39
 
64
void ResetTimer(void)
40
void ResetTimer(void)
65
{
41
{
66
        TCNT1H = 0;
42
        TCNT1H = 0;
67
        TCNT1L = 0;
43
        TCNT1L = 0;
68
        TimerEvent = 0;
44
        TimerEvent = 0;
69
}
45
}
70
 
46
 
-
 
47
 
-
 
48
void Binding(void)                                                                                      // Binding sequence for DX7
-
 
49
{
-
 
50
        unsigned char i = 0;
-
 
51
 
-
 
52
        DDRB = 0b11110000;                                                                              // Sat-Pins to output
-
 
53
        _delay_ms(80);                                                                                  // Let them boot up
-
 
54
       
-
 
55
        for (i=0;i<3;i++)                                                                               // send three negative 100µs pulses to all sat's
-
 
56
        {
-
 
57
                PORTB = 0b00000000;
-
 
58
                _delay_us(100);
-
 
59
                PORTB = 0b11110000;
-
 
60
                _delay_us(100);
-
 
61
        }
-
 
62
 
-
 
63
        _delay_ms(1000);
-
 
64
        DDRB = 0b00000000;                                                                              // Sat-Pins to input after 1sec
-
 
65
}
-
 
66
 
-
 
67
 
-
 
68
void Testing(void)                                                                                      // Self Test
-
 
69
{
-
 
70
        unsigned char i = 0;
-
 
71
        unsigned char error = 0;
-
 
72
 
-
 
73
        DDRB = 0b11110000;                                                                              // PORT B OUTPUT FOR SATELLITES, INPUT FOR FEEDBACK
-
 
74
        PORTB = 0b00000000;
-
 
75
 
-
 
76
        for(i=0;i<4;i++)                                                                                // Test muxxing of High+Low on every Sat-Input
-
 
77
        {
-
 
78
                SelectSat(i);
-
 
79
                PORTB |= 1 << (i+4);
-
 
80
                if (!(PINB & (1<<PB3))) error++;
-
 
81
                PORTB &= ~(1 << (i+4));
-
 
82
                if (PINB & (1<<PB3)) error++;
-
 
83
        }
-
 
84
 
-
 
85
        while(1)                                                                                                // Never return
-
 
86
        {
-
 
87
                if (error == 0)                                                                         // When no error occured, flash around
-
 
88
                {
-
 
89
                        LED_ON;
-
 
90
                        _delay_ms(100);
-
 
91
                        LED_OFF;
-
 
92
                        _delay_ms(100);
-
 
93
                }
-
 
94
        }
-
 
95
}
71
 
96
 
72
 
97
 
73
ISR(TIMER1_OVF_vect)                                                                            // Triggered every 4,096 msec
98
ISR(TIMER1_OVF_vect)                                                                            // Triggered every 8,192 msec
74
{
99
{
75
        TimerEvent++;
100
        TimerEvent++;
76
}
101
}
77
 
102
 
78
 
103
 
79
int main(void)
104
int main(void)
80
{
105
{
81
        unsigned char i = 0;
106
        unsigned char i = 0;
82
        unsigned int j = 0;
107
        unsigned char j = 0;
83
        unsigned char active[8];
108
        unsigned char active[4];
84
        unsigned char sat = 9;
109
        unsigned char sat = 4;
-
 
110
 
85
        DDRB = 0b00000000;                                                                              // PORT A INPUT FOR SATELLITES
111
        DDRB = 0b00000000;                                                                              // PORT B INPUT FOR SATELLITES AND FEEDBACK
86
        DDRD = 0b00001111;                                                                              // PORT B OUTPUT FOR MUX AND LED
112
        DDRD = 0b0011001;                                                                               // PORT D OUTPUT FOR MUX AND LED, INPUT FOR SWITCH & TEST
87
        PORTB = 0b11111111;                                                                             // PORT A PULLUP's FOR (unused) SATELLITES
113
        PORTB = 0b11110000;                                                                             // PORT B PULLUP's FOR (unused) SATELLITES
-
 
114
        PORTD = 0b1100001;                                                                              // PORT D PULLUP's FOR SWITCH & TEST
-
 
115
 
-
 
116
        for (i=0;i<4;i++) active[i] = 0;                                                // Reset active-array
-
 
117
 
-
 
118
        if (!(PIND & (1<<PD6))) Binding();                                              // Initiate Binding when Bind-Button is pressed
88
 
119
        if (!(PIND & (1<<PD5))) Testing();                                              // Initiate Self-Test when Test-Pad is low
89
 
120
 
90
        TCCR1B = ( 1 << CS10 );                                                                 // Timer1 Prescaler 1 = 4,096 msec
121
        TCCR1B = ( 1 << CS10 );                                                                 // Timer1 Prescaler = 1 -> 8,192 msec
91
        TIMSK = ( 1 << TOIE1 );                                                                 // Timer1 Overflow Interrupt Enable
122
        TIMSK = ( 1 << TOIE1 );                                                                 // Timer1 Overflow Interrupt Enable
92
        sei();                                                                                                  // Global Interrupts enable
123
        sei();                                                                                                  // Global Interrupts enable
93
 
-
 
94
        for (i=0;i<8;i++) active[i] = 0;                                                // Reset active-array
-
 
95
 
124
 
96
        ResetTimer();
125
        ResetTimer();
97
        while(PINB == 255)                                                                              // Wait for first signal (SYNC to frame)
126
        while((PINB & 0b11110000) == 0b11110000)                                // Wait for first signal (SYNC to frame)
98
        {
127
        {
99
                if (TimerEvent == 10) LED_ON;                                           // Blink while waiting...
128
                if (TimerEvent == 10) LED_ON;                                           // Blink while waiting...
100
                if (TimerEvent == 20)
129
                if (TimerEvent == 20)
101
                {
130
                {
102
                        LED_OFF;
131
                        LED_OFF;
103
                        TimerEvent = 0;
132
                        TimerEvent = 0;
104
                }
133
                }
105
        }
134
        }
106
 
135
 
107
        LED_OFF;
136
        LED_OFF;
108
 
137
 
109
        ResetTimer();
138
        ResetTimer();
110
        while(1)                                                                                                // Check active satellites for 3 seconds
139
        while(1)                                                                                                // Check active satellites for 3 seconds
111
        {
140
        {
112
                if (TimerEvent == 20) LED_ON;                                           // Blink (slower) while waiting...
141
                if (TimerEvent == 20) LED_ON;                                           // Blink (slower) while waiting...
113
                if (TimerEvent == 40)
142
                if (TimerEvent == 40)
114
                {
143
                {
115
                        LED_OFF;
144
                        LED_OFF;
116
                        TimerEvent = 0;
145
                        TimerEvent = 0;
117
                        j++;
146
                        j++;
118
                }
147
                }
119
 
148
 
120
                for (i=0;i<8;i++)
149
                for (i=0;i<4;i++)
121
                {
150
                {
122
                        if (!(PINB & (1<<i)))
151
                        if (!(PINB & (1<<(i+4))))
123
                        {
152
                        {
124
                                active[i] = 1;
153
                                active[i] = 1;
125
                                if (sat == 9)                                                           // Select first active satellite (only once)
154
                                if (sat == 4)                                                           // Select first active satellite (only once)
126
                                {
155
                                {
127
                                        sat = i;
156
                                        sat = i;
128
                                        SelectSat(i);
157
                                        SelectSat(i);
129
                                }
158
                                }
130
 
159
 
131
                        }
160
                        }
132
                }
161
                }
133
               
162
               
134
                if (j == 9) break;                                                                      // 9 * 40 * 8ms = ~3sec
163
                if (j == 9) break;                                                                      // 9 * 40 * 8ms = ~3sec
135
        }
164
        }
136
 
165
 
137
        LED_OFF;
166
        LED_OFF;
138
        _delay_ms(1000);
167
        _delay_ms(1000);
139
 
168
 
140
        for (i=0;i<8;i++)                                                                               // Flash once for every active satellite
169
        for (i=0;i<4;i++)                                                                               // Flash once for every active satellite
141
        {
170
        {
142
                if (active[i] == 1)
171
                if (active[i] == 1)
143
                {
172
                {
144
                        LED_ON;
173
                        LED_ON;
145
                        _delay_ms(200);
174
                        _delay_ms(100);
146
                        LED_OFF;
175
                        LED_OFF;
147
                        _delay_ms(200);
176
                        _delay_ms(200);
148
                }
177
                }
149
        }
178
        }
150
 
179
 
151
 
180
 
152
 
181
 
153
 
182
 
154
        while(1)                                                                                                // Main-Loop
183
        while(1)                                                                                                // Main-Loop
155
        {
184
        {
156
                ResetTimer();
185
                ResetTimer();
157
 
186
 
158
                while(PINB == 255)                                                                      // Wait for first signal (SYNC to frame)
187
                while((PINB & 0b11110000) == 0b11110000)                        // Wait for first signal (SYNC to frame)
159
                {
188
                {
160
                        if (TimerEvent > 3) break;                                              // (max. 3*8=24ms)
189
                        if (TimerEvent > 3) break;                                              // (max. 3*8=24ms)
161
                }
190
                }
162
               
191
               
163
                for (i=0;i<8;i++) active[i] = 0;                                        // Reset active-array
192
                for (i=0;i<4;i++) active[i] = 0;                                        // Reset active-array
164
                ResetTimer();
193
                ResetTimer();
165
 
194
 
166
                while(TimerEvent < 1)                                                           // Check active satellites (for 1*8=8ms)
195
                while(TimerEvent < 1)                                                           // Check active satellites (for 1*8=8ms)
167
                {
196
                {
168
 
197
 
169
                        for (i=0;i<8;i++)
198
                        for (i=0;i<4;i++)
170
                        {
199
                        {
171
                                if (!(PINB & (1<<i)))
200
                                if (!(PINB & (1<<(i+4))))
172
                                {
201
                                {
173
                                        active[i] = 1;
202
                                        active[i] = 1;
174
                                }
203
                                }
175
                        }
204
                        }
176
                }
205
                }
177
 
206
 
178
 
207
 
179
                if (active[sat] == 0)                                                           // Detect fail on active satellite
208
                if (active[sat] == 0)                                                           // Detect fail on active satellite
180
                {
209
                {
181
                        LED_ON;                                                                                 // Failure-LED ON (never goes off again)
210
                        LED_ON;                                                                                 // Failure-LED ON (never goes off again)
182
                        for (i=0;i<8;i++)                                                               // Select lowest active satellite
211
                        for (i=0;i<4;i++)                                                               // Select lowest active satellite
183
                        {
212
                        {
184
                                if (active[i] == 1)
213
                                if (active[i] == 1)
185
                                {
214
                                {
186
                                        sat = i;
215
                                        sat = i;
187
                                        SelectSat(sat);
216
                                        SelectSat(sat);
188
                                        break;
217
                                        break;
189
                                }
218
                                }
190
                        }
219
                        }
191
                }
220
                }
-
 
221
 
-
 
222
 
192
        }
223
        }
-
 
224
 
193
}
225
}
-
 
226
 
194
 
227