Subversion Repositories Projects

Rev

Rev 64 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 64 Rev 65
1
//############################################################################
1
//############################################################################
2
// - PWM CTRL 
2
// - PWM CTRL 
3
// - Main
3
// - Main
4
// - ATMEGA8 mit 8MHz
4
// - ATMEGA8 mit 8MHz
5
// - Nur für den privaten Gebrauch
5
// - Nur für den privaten Gebrauch
6
// - Keine Garantie auf Fehlerfreiheit
6
// - Keine Garantie auf Fehlerfreiheit
7
// - Kommerzielle Nutzung nur mit meiner Zustimmung
7
// - Kommerzielle Nutzung nur mit meiner Zustimmung
8
// - walter Meyer @ www.freakware.de
8
// - walter Meyer @ www.freakware.de
9
// - 11.12.2007
9
// - 11.12.2007
10
// - Make sure Fuses are programmed for internal 8 MHz RC Oscilator
10
// - Make sure Fuses are programmed for internal 8 MHz RC Oscilator
11
//############################################################################*/
11
//############################################################################*/
12
 
12
 
13
#include "main.h"
13
#include "main.h"
14
#include "uart.h"
14
#include "uart.h"
15
#include "twislave.h"
15
#include "twislave.h"
16
 
16
 
17
volatile unsigned int PPM_SIGNAL = 1520;
17
volatile unsigned int PPM_SIGNAL = 1520;
18
volatile unsigned char PPM_NEW = 0;
18
volatile unsigned char PPM_NEW = 0;
19
volatile unsigned char TMR1OvF = 0;
19
volatile unsigned char TMR1OvF = 0;
20
volatile unsigned int  TMR1MS;
20
volatile unsigned int  TMR1MS;
21
volatile unsigned char CH0;
21
volatile unsigned char CH0;
22
volatile unsigned char CH1;
22
volatile unsigned char CH1;
23
volatile unsigned char CH2;
23
volatile unsigned char CH2;
24
volatile unsigned char CH3;
24
volatile unsigned char CH3;
25
volatile unsigned char CH4;
25
volatile unsigned char CH4;
26
volatile unsigned char CH5;
26
volatile unsigned char CH5;
27
 
27
 
28
uint16_t EEMEM EEPMIN=0x0000;
28
uint16_t EEMEM EEPMIN=0x0000;
29
uint16_t EEMEM EEPMAX=0xffff;
29
uint16_t EEMEM EEPMAX=0xffff;
30
uint16_t EEMEM EEPSIG=0xffff;
30
uint16_t EEMEM EEPSIG=0xffff;
31
 
31
 
32
 
32
 
33
//led kennlinie, (noch nicht implementiert, nur vorbereitet)
33
//led kennlinie, (noch nicht implementiert, nur vorbereitet)
34
unsigned char kl[256]={
34
unsigned char kl[256]={
35
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
35
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
36
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
36
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
37
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
37
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
38
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
38
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
39
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
39
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
40
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
40
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
41
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
41
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
42
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
42
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
43
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
43
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
44
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
44
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
45
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
45
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
46
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
46
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
47
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
47
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
48
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
48
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
49
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
49
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
50
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
50
        0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
51
};
51
};
52
 
52
 
53
 
53
 
54
 
54
 
55
/*##############################################################################*/
55
/*##############################################################################*/
56
SIGNAL(SIG_OVERFLOW1)
56
SIGNAL(SIG_OVERFLOW1)
57
{
57
{
58
        TMR1OvF++;
58
        TMR1OvF++;
59
}
59
}
60
 
60
 
61
 
61
 
62
SIGNAL(SIG_INPUT_CAPTURE1)
62
SIGNAL(SIG_INPUT_CAPTURE1)
63
{
63
{
64
        static unsigned int pos_ICR;
64
        static unsigned int pos_ICR;
65
        static unsigned int ppm;
65
        static unsigned int ppm;
66
       
66
       
67
        if ((TCCR1B & (1<<ICES1)) != 0)                                         //rising edge
67
        if ((TCCR1B & (1<<ICES1)) != 0)                                         //rising edge
68
        {
68
        {
69
                TCCR1B &= ~(1<<ICES1);                                                          //set falling egde
69
                TCCR1B &= ~(1<<ICES1);                                                          //set falling egde
70
                TMR1OvF = 0;
70
                TMR1OvF = 0;
71
                pos_ICR = ICR1;
71
                pos_ICR = ICR1;
72
        }
72
        }
73
        else                                                                                    //falling edge
73
        else                                                                                    //falling edge
74
        {
74
        {
75
                TCCR1B |= (1<<ICES1);                                                           //set rising egde
75
                TCCR1B |= (1<<ICES1);                                                           //set rising egde
76
                ppm = (ICR1 - pos_ICR + (int) TMR1OvF * 65536);
76
                ppm = (ICR1 - pos_ICR + (int) TMR1OvF * 65536);
77
                if ((ppm > 600) && (ppm < 2400))
77
                if ((ppm > 600) && (ppm < 2400))
78
                {
78
                {
79
                        if (ppm > 2100) ppm = 2100;
79
                        if (ppm > 2100) ppm = 2100;
80
                        if (ppm < 900) ppm = 900;
80
                        if (ppm < 900) ppm = 900;
81
                        ppm = (PPM_SIGNAL * 3 + ppm) / 4;
81
                        ppm = (PPM_SIGNAL * 3 + ppm) / 4;
82
                        PPM_SIGNAL = ppm;
82
                        PPM_SIGNAL = ppm;
83
                        if (PPM_NEW < 50) PPM_NEW++;
83
                        if (PPM_NEW < 50) PPM_NEW++;
84
                }
84
                }
85
               
85
               
86
        }
86
        }
87
 
87
 
88
}
88
}
89
 
89
 
90
/*##############################################################################*/
90
/*##############################################################################*/
91
SIGNAL(SIG_OVERFLOW0)
91
SIGNAL(SIG_OVERFLOW0)
92
{
92
{
93
        // this function is called every 32us,
93
        // this function is called every 32us,
94
        // it is very important that it's execution time is as short as possible
94
        // it is very important that it's execution time is as short as possible
95
        // currently it's about 20us
95
        // currently it's about 20us
96
 
96
 
97
        static unsigned char counter = 254;
97
        static unsigned char counter = 254;
98
        static unsigned char ms1 = 0;
98
        static unsigned char ms1 = 0;
99
        static unsigned char ch0_tmp = 0;
99
        static unsigned char ch0_tmp = 0;
100
        static unsigned char ch1_tmp = 0;
100
        static unsigned char ch1_tmp = 0;
101
        static unsigned char ch2_tmp = 0;
101
        static unsigned char ch2_tmp = 0;
102
        static unsigned char ch3_tmp = 0;
102
        static unsigned char ch3_tmp = 0;
103
        static unsigned char ch4_tmp = 0;
103
        static unsigned char ch4_tmp = 0;
104
        static unsigned char ch5_tmp = 0;
104
        static unsigned char ch5_tmp = 0;
105
        unsigned char PORTB_BAK;
105
        unsigned char PORTB_BAK;
106
        unsigned char PORTD_BAK;       
106
        unsigned char PORTD_BAK;       
107
 
107
 
108
        PORTB_BAK = PORTB;
108
        PORTB_BAK = PORTB;
109
        PORTD_BAK = PORTD;
109
        PORTD_BAK = PORTD;
110
 
110
 
111
        if (counter++ == 254)
111
        if (counter++ == 254)
112
        {
112
        {
113
                PORTB_BAK LEDON (CH0_B | CH1_B | CH2_B);                //new cycle, output on
113
                PORTB_BAK LEDON (CH0_B | CH1_B | CH2_B);                //new cycle, output on
114
                PORTD_BAK LEDON (CH3_D | CH4_D | CH5_D);                //
114
                PORTD_BAK LEDON (CH3_D | CH4_D | CH5_D);                //
-
 
115
 
115
                ch0_tmp = CH0;
116
                ch0_tmp = CH0;
116
                ch1_tmp = CH1;
117
                ch1_tmp = CH1;
117
                ch2_tmp = CH2;
118
                ch2_tmp = CH2;
118
                ch3_tmp = CH3;
119
                ch3_tmp = CH3;
119
                ch4_tmp = CH4;
120
                ch4_tmp = CH4;
120
                ch5_tmp = CH5;         
121
                ch5_tmp = CH5;         
-
 
122
 
121
                counter = 0;
123
                counter = 0;
122
        }
124
        }
123
 
125
 
124
        if (ch0_tmp == counter) PORTB_BAK LEDOFF CH0_B;         //channel value reached, output off
126
        if (ch0_tmp == counter) PORTB_BAK LEDOFF CH0_B;         //channel value reached, output off
125
        if (ch1_tmp == counter) PORTB_BAK LEDOFF CH1_B;         //
127
        if (ch1_tmp == counter) PORTB_BAK LEDOFF CH1_B;         //
126
        if (ch2_tmp == counter) PORTB_BAK LEDOFF CH2_B;         //
128
        if (ch2_tmp == counter) PORTB_BAK LEDOFF CH2_B;         //
127
        if (ch3_tmp == counter) PORTD_BAK LEDOFF CH3_D;         //
129
        if (ch3_tmp == counter) PORTD_BAK LEDOFF CH3_D;         //
128
        if (ch4_tmp == counter) PORTD_BAK LEDOFF CH4_D;         //
130
        if (ch4_tmp == counter) PORTD_BAK LEDOFF CH4_D;         //
129
        if (ch5_tmp == counter) PORTD_BAK LEDOFF CH5_D;         //
131
        if (ch5_tmp == counter) PORTD_BAK LEDOFF CH5_D;         //
130
       
132
       
131
        PORTB = PORTB_BAK;
133
        PORTB = PORTB_BAK;
132
        PORTD = PORTD_BAK;
134
        PORTD = PORTD_BAK;
133
       
135
       
134
        if (ms1++ == 32)
136
        if (ms1++ == 32)
135
        {
137
        {
136
                ms1=0;
138
                ms1=0;
137
                TMR1MS++;
139
                TMR1MS++;
138
        }
140
        }
139
       
141
       
140
 
142
 
141
 
143
 
142
 
144
 
143
}
145
}
144
 
146
 
145
 
147
 
146
/*##############################################################################*/
148
/*##############################################################################*/
147
unsigned int SetDelay (unsigned int t)
149
unsigned int SetDelay (unsigned int t)
148
{
150
{
149
        unsigned char hi_byte;
151
        unsigned char hi_byte;
150
        unsigned char lo_byte;  
152
        unsigned char lo_byte;  
151
 
153
 
152
        hi_byte = (TMR1MS >> 8);
154
        hi_byte = (TMR1MS >> 8);
153
        lo_byte = (TMR1MS & 0xff);
155
        lo_byte = (TMR1MS & 0xff);
154
        if (hi_byte != (TMR1MS >> 8)) hi_byte = (TMR1MS >> 8);
156
        if (hi_byte != (TMR1MS >> 8)) hi_byte = (TMR1MS >> 8);
155
 
157
 
156
        return(((hi_byte << 8) | lo_byte)  + t + 1);                                            
158
        return(((hi_byte << 8) | lo_byte)  + t + 1);                                            
157
}
159
}
158
 
160
 
159
 
161
 
160
/*##############################################################################*/
162
/*##############################################################################*/
161
char CheckDelay(unsigned int t)
163
char CheckDelay(unsigned int t)
162
{
164
{
163
        unsigned char hi_byte;
165
        unsigned char hi_byte;
164
        unsigned char lo_byte;  
166
        unsigned char lo_byte;  
165
 
167
 
166
        hi_byte = (TMR1MS >> 8);
168
        hi_byte = (TMR1MS >> 8);
167
        lo_byte = (TMR1MS & 0xff);
169
        lo_byte = (TMR1MS & 0xff);
168
        if (hi_byte != (TMR1MS >> 8)) hi_byte = (TMR1MS >> 8);
170
        if (hi_byte != (TMR1MS >> 8)) hi_byte = (TMR1MS >> 8);
169
 
171
 
170
        return(((t - ((hi_byte << 8) | lo_byte)) & 0x8000) >> 9);
172
        return(((t - ((hi_byte << 8) | lo_byte)) & 0x8000) >> 9);
171
}
173
}
172
 
174
 
173
 
175
 
174
 
176
 
175
/*##############################################################################*/
177
/*##############################################################################*/
176
void StartPWM(void)
178
void StartPWM(void)
177
{
179
{
178
        //Timer 0 Config
180
        //Timer 0 Config
179
        TCCR0 = (0<<CS02)|(0<<CS01)|(1<<CS00);          // (@8MHz) =  1/8us Clk = 256/8 = 32us overflow
181
        TCCR0 = (0<<CS02)|(0<<CS01)|(1<<CS00);          // (@8MHz) =  1/8us Clk = 256/8 = 32us overflow
180
        TIMSK setbit (1<<TOIE0);                                                // enable Int
182
        TIMSK setbit (1<<TOIE0);                                                // enable Int
181
 
183
 
182
}
184
}
183
 
185
 
184
 
186
 
185
/*##############################################################################*/
187
/*##############################################################################*/
186
void StartPPM(void)
188
void StartPPM(void)
187
{
189
{
188
       
190
       
189
        //Timer1 Config
191
        //Timer1 Config
190
        TCCR1A =        (0<<COM1A1)|(0<<COM1A0)|(0<<COM1B1)|(0<<COM1B0)|
192
        TCCR1A =        (0<<COM1A1)|(0<<COM1A0)|(0<<COM1B1)|(0<<COM1B0)|
191
                                (0<<FOC1A) |(0<<FOC1B) |(0<<WGM10) |(0<<WGM11);                
193
                                (0<<FOC1A) |(0<<FOC1B) |(0<<WGM10) |(0<<WGM11);                
192
    TCCR1B =    (1<<ICNC1)|(1<<ICES1)|(0<<WGM13)|
194
    TCCR1B =    (1<<ICNC1)|(1<<ICES1)|(0<<WGM13)|
193
                                (0<<WGM12)|(0<<CS12)|(1<<CS11)|(0<<CS10);                               //ICP_POS_FLANKE
195
                                (0<<WGM12)|(0<<CS12)|(1<<CS11)|(0<<CS10);                               //ICP_POS_FLANKE
194
 
196
 
195
        // interrupts
197
        // interrupts
196
        TIMSK |=        (1<<TICIE1)|(1<<TOIE1);                                                                 //ICP_INT_ENABLE and TIMER1_INT_ENABLE
198
        TIMSK |=        (1<<TICIE1)|(1<<TOIE1);                                                                 //ICP_INT_ENABLE and TIMER1_INT_ENABLE
197
 
199
 
198
}
200
}
199
 
201
 
200
 
202
 
201
/*##############################################################################*/
203
/*##############################################################################*/
202
unsigned int GetPPM(void)
204
unsigned int GetPPM(void)
203
{
205
{
204
        //this routines seems to be nesseccary, as reading a 16 bit value
206
        //this routines seems to be nesseccary, as reading a 16 bit value
205
        //on a 8 bit machine is not atomic, so if an interrupt apears between reading
207
        //on a 8 bit machine is not atomic, so if an interrupt apears between reading
206
        //low and high byte of the 16 bit value a wrong result is possible
208
        //low and high byte of the 16 bit value a wrong result is possible
207
       
209
       
208
        unsigned char temp_hi;
210
        unsigned char temp_hi;
209
        unsigned char temp_lo;  
211
        unsigned char temp_lo;  
210
 
212
 
211
        temp_hi = (PPM_SIGNAL >> 8);
213
        temp_hi = (PPM_SIGNAL >> 8);
212
        temp_lo = (PPM_SIGNAL & 0xff);
214
        temp_lo = (PPM_SIGNAL & 0xff);
213
        if (temp_hi != (PPM_SIGNAL >> 8)) temp_hi = (PPM_SIGNAL >> 8);
215
        if (temp_hi != (PPM_SIGNAL >> 8)) temp_hi = (PPM_SIGNAL >> 8);
214
        return( (temp_hi << 8) | temp_lo);                                            
216
        return( (temp_hi << 8) | temp_lo);                                            
215
 
217
 
216
}
218
}
217
 
219
 
218
 
220
 
219
/*##############################################################################*/
221
/*##############################################################################*/
220
// MAIN
222
// MAIN
221
/*##############################################################################*/
223
/*##############################################################################*/
222
int main (void)
224
int main (void)
223
{
225
{
224
 
226
 
225
        #define STEP            256
227
        #define STEP            256
226
        #define MUL                     1
228
        #define MUL                     1
227
        #define REDUCE          2
229
        #define REDUCE          2
228
       
230
       
229
        #define EEPSIGNATURE    0x55aa
231
        #define EEPSIGNATURE    0x55aa
230
 
232
 
231
        #define inimin          1200
233
        #define inimin          1200
232
        #define inimax          1800    
234
        #define inimax          1800    
233
 
235
 
234
        unsigned int    ppm;
236
        unsigned int    ppm;
235
        signed int              color; 
237
        signed int              color; 
236
        unsigned int    setupdly;
238
        volatile unsigned int   setupdly;
237
        unsigned int    ppmtodly;      
239
        volatile unsigned int   ppmtodly;      
238
        unsigned int    flashdly;
240
        volatile unsigned int   flashdly;
-
 
241
       
-
 
242
        volatile unsigned int   dly1;
-
 
243
 
-
 
244
        unsigned char   flags;
-
 
245
 
239
        unsigned char   setup;
246
        unsigned char   setup;
240
        unsigned int    lmax;
247
        unsigned int    lmax;
241
        unsigned int    lmin;
248
        unsigned int    lmin;
242
        unsigned int    max;
249
        unsigned int    max;
243
        unsigned int    min;
250
        unsigned int    min;
-
 
251
        unsigned char   temp;
244
        signed long             temp1; 
252
        signed long             temp1; 
245
        signed long             temp2;         
253
        signed long             temp2;         
-
 
254
 
-
 
255
        flags = 0;
246
 
256
 
247
    DDRB  = (CH0_B|CH1_B|CH2_B);
257
    DDRB  = (CH0_B|CH1_B|CH2_B);
248
    PORTB = 0x00;
258
    PORTB = 0x00;
249
 
259
 
250
    DDRC  = (ledred);
260
    DDRC  = (ledred);
251
    PORTC = 0x00;
261
    PORTC = 0x00;
252
 
262
 
253
    DDRD  = (ledgreen|CH3_D|CH4_D|CH5_D);
263
    DDRD  = (ledgreen|CH3_D|CH4_D|CH5_D);
254
    PORTD = 0x00;
264
    PORTD = 0x00;
255
 
265
 
256
        CH0 = 0;
266
        CH0 = 0xFF;             //PORTB RGBLED red
257
        CH1 = 0;
267
        CH1 = 0xFF;             //PORTB RGBLED green
258
        CH2 = 0;
268
        CH2 = 0xFF;             //PORTB RGBLED blue
259
        CH3 = 0;
269
        CH3 = 0;       
260
        CH4 = 0;
270
        CH4 = 0;
261
        CH5 = 0;       
271
        CH5 = 0;       
262
 
272
 
263
        lmax = 0x0000;
273
        lmax = 0x0000;
264
        lmin = 0xffff;
274
        lmin = 0xffff;
265
 
275
 
266
//      StartUART();
276
        StartUART();
267
        StartPPM();
277
        StartPPM();
268
        //StartI2C();
278
        //StartI2C();
269
        StartPWM();
279
        StartPWM();
270
        sei();
280
        sei();
271
 
281
 
272
        min = inimin;                                                                                           //default min 
282
        min = inimin;                                                                                           //default min 
273
        max = inimax;                                                                                           //default max
283
        max = inimax;                                                                                           //default max
274
 
284
 
275
        if (eeprom_read_word(&EEPSIG) != EEPSIGNATURE)                          //check eep if signature is there
285
        if (eeprom_read_word(&EEPSIG) != EEPSIGNATURE)                          //check eep if signature is there
276
        {
286
        {
277
                eeprom_write_word(&EEPMIN, min);                                                //no, write initial min
287
                eeprom_write_word(&EEPMIN, min);                                                //no, write initial min
278
                eeprom_write_word(&EEPMAX, max);                                                //and max values
288
                eeprom_write_word(&EEPMAX, max);                                                //and max values
279
                eeprom_write_word(&EEPSIG, EEPSIGNATURE);                               //along with eep signature
289
                eeprom_write_word(&EEPSIG, EEPSIGNATURE);                               //along with eep signature
280
        }
290
        }
281
        else
291
        else
282
        {
292
        {
283
 
293
 
284
                min = eeprom_read_word(&EEPMIN);                                                //signature ok
294
                min = eeprom_read_word(&EEPMIN);                                                //signature ok
285
                max = eeprom_read_word(&EEPMAX);                                                //read min and max
295
                max = eeprom_read_word(&EEPMAX);                                                //read min and max
286
        }
296
        }
287
 
297
 
288
 
298
 
289
        setup = 0;                                                                                                      //reset setup toggle counter
299
        setup = 0;                                                                                                      //reset setup toggle counter
-
 
300
        temp = (1<<0);
290
        setupdly = SetDelay(5000);
301
        setupdly = SetDelay(5000);
291
        ppmtodly = SetDelay(5000);     
302
        ppmtodly = SetDelay(2000);     
292
        flashdly = SetDelay(1000);                                                             
303
        flashdly = SetDelay(200);                                                              
293
   
304
   
294
    while (1)
305
    while (1)
295
        {
306
        {
296
 
307
 
297
                if (PPM_NEW > 20)                                                                               //ppm Signal ok 
308
                if (PPM_NEW > 20)                                                                               //ppm Signal ok 
298
                {
309
                {
299
                        PORTC clrbit ledred;
310
                        PORTC clrbit ledred;
300
                        ppm = GetPPM();                        
311
                        ppm = GetPPM();                        
301
                        ppmtodly = SetDelay(500);                                                       //reset timeout                 
312
                        ppmtodly = SetDelay(500);                                                       //reset timeout                 
302
                        if (lmax < ppm) lmax=ppm;                                                       //update impulse max 
313
                        if (lmax < ppm) lmax=ppm;                                                       //update impulse max 
303
                        if (lmin > ppm) lmin=ppm;                                                       //and min boundarys
314
                        if (lmin > ppm) lmin=ppm;                                                       //and min boundarys
304
                }
315
                }
305
                else
316
                else
306
                {
317
                {
307
                        PORTC setbit ledred;                                                            //ppm signal not ok
318
                        PORTC setbit ledred;                                                            //ppm signal not ok
308
                        ppm = min;                                                                                      //set ppm to minimum
319
                        ppm = min;                                                                                      //set ppm to minimum
309
                }
320
                }
310
               
321
               
311
 
322
 
312
                if (CheckDelay(ppmtodly))                                                               //timeout 
323
                if (CheckDelay(ppmtodly))                                                               //timeout 
313
                {
324
                {
314
                        ppmtodly = SetDelay(5000);             
325
                        ppmtodly = SetDelay(5000);             
315
                        PPM_NEW = 0;                                                                            //set ppm signal not ok
326
                        PPM_NEW = 0;                                                                            //set ppm signal not ok
316
                }      
327
                }      
317
 
328
 
318
 
329
 
319
                if (setup < 6)
330
                if (setup < 6)
320
                {
331
                {
321
                        if ((ppm > 1600) && ((setup&1)==0)) setup++;            //
332
                        if ((ppm > 1600) && ((setup&1)==0)) setup++;            //
322
                        if ((ppm < 1400) && ((setup&1)==1)) setup++;            //
333
                        if ((ppm < 1400) && ((setup&1)==1)) setup++;            //
323
 
334
 
324
                        if (CheckDelay(flashdly))                                                      
335
                        if (CheckDelay(flashdly))                                                      
325
                        {      
336
                        {      
326
                                CH0 = CH0 ^ 0xff;
337
                                CH0 = (temp & (1<<0)) * 255;
-
 
338
                                CH1 = (temp & (1<<1)) * 255;
-
 
339
                                CH2 = (temp & (1<<2)) * 255;
-
 
340
                                CH3 = (temp & (1<<3)) * 255;                           
-
 
341
                                CH4 = (temp & (1<<4)) * 255;
-
 
342
                                CH5 = (temp & (1<<5)) * 255;                           
-
 
343
                                temp <<= 1;
-
 
344
                                if (temp == (1<<6)) temp = (1<<0);
-
 
345
 
-
 
346
                                /*
-
 
347
                                CH0 = CH0 ^ 0xff;                                                               //invert brightness
327
                                CH1 = CH1 ^ 0xff;
348
                                CH1 = CH1 ^ 0xff;
328
                                CH2 = CH2 ^ 0xff;
349
                                CH2 = CH2 ^ 0xff;
-
 
350
                                CH3 = CH3 ^ 0xff;
-
 
351
                                CH4 = CH4 ^ 0xff;
-
 
352
                                CH5 = CH5 ^ 0xff;
-
 
353
                                */
-
 
354
 
329
                                flashdly = SetDelay(250);                                                              
355
                                flashdly = SetDelay(100);                                                              
330
                        }
356
                        }
331
                }
357
                }
332
 
358
 
333
                if (setup == 6)                                                                                 //if stick is toggled 6 times 
359
                if (setup == 6)                                                                                 //if stick is toggled 6 times 
334
                {                                                                                                               //within setup timeout
360
                {                                                                                                               //within setup timeout
335
                        PORTD setbit ledgreen;                                                          //store ppm min and max 
361
                        PORTD setbit ledgreen;                                                          //store ppm min and max 
336
                        setupdly = SetDelay(3000);                                                      //for 2000ms
362
                        setupdly = SetDelay(3000);                                                      //for 2000ms
337
                        eeprom_write_word(&EEPMIN, lmin);                                       //in eeprom
363
                        eeprom_write_word(&EEPMIN, lmin);                                       //in eeprom
338
                        eeprom_write_word(&EEPMAX, lmax);      
364
                        eeprom_write_word(&EEPMAX, lmax);      
339
                        min = lmin;                                                                                    
365
                        min = lmin;                                                                                    
340
                        max = lmax;
366
                        max = lmax;
341
                        setup = 7;                                                                                      //enter PWM toggle mode
367
                        setup = 7;                                                                                      //enter PWM toggle mode
342
 
368
 
343
                }
369
                }
344
 
370
 
345
 
371
 
346
                if (setup == 7)
372
                if (setup == 7)
347
                {
373
                {
348
                        if (CheckDelay(flashdly))                                                       //each 25ms toggle PWM's 
374
                        if (CheckDelay(flashdly))                                                       //each 25ms toggle PWM's 
349
                        {      
375
                        {      
350
                                CH0 = CH0 ^ 0xff;
376
                                CH0 = CH0 ^ 0xff;                                                               //invert brightness
351
                                CH1 = CH1 ^ 0xff;
377
                                CH1 = CH1 ^ 0xff;
352
                                CH2 = CH2 ^ 0xff;
378
                                CH2 = CH2 ^ 0xff;
-
 
379
                                CH3 = CH3 ^ 0xff;
-
 
380
                                CH4 = CH4 ^ 0xff;
-
 
381
                                CH5 = CH5 ^ 0xff;
-
 
382
 
353
                                flashdly = SetDelay(25);                                                               
383
                                flashdly = SetDelay(25);                                                               
354
                        }
384
                        }
355
               
385
               
356
                }
386
                }
357
 
387
 
358
                if (CheckDelay(setupdly))                                                               //setup timeout reached
388
                if ((CheckDelay(setupdly)) && setup < 8)                                //setup timeout reached
359
                {
389
                {
360
                        setup = 8;                                                                                      //lockdown setup
390
                        setup = 8;                                                                                      //lockdown setup
361
                        PORTD clrbit ledgreen;
391
                        PORTD clrbit ledgreen;
-
 
392
                        CH0 = 0;                                                                                        //start CH3
-
 
393
                        CH1 = 85;                                                                                       //CH4
-
 
394
                        CH2 = 170;                                                                                      //and CH4 with about 120° phase shift
-
 
395
 
-
 
396
                        flags = 0;
-
 
397
                        dly1 = SetDelay(100);                                                                                  
362
                }
398
                }
363
 
399
 
364
               
400
               
365
                if (setup == 8)
401
                if (setup == 8)
366
                {
402
                {
367
               
403
 
-
 
404
 
-
 
405
/************************************************************************************/                 
-
 
406
/* control CH3, CH4 and CH5     (RGB LED) */
368
                       
407
/************************************************************************************/                 
369
 
408
 
370
                        temp1 = ((long)STEP * 6 * 20000) / ((max-REDUCE) - (min+REDUCE));
409
                        temp1 = ((long)STEP * 7 * 40000) / ((max-REDUCE) - (min+REDUCE));
371
                        temp2 = (((int)ppm - ((int)min+REDUCE)) * temp1);
410
                        temp2 = (((int)ppm - ((int)min+REDUCE)) * temp1);
372
                        color = temp2 / 20000;
411
                        color = temp2 / 40000;
373
                       
412
                       
374
                        if (color < 0) color = 0;
413
                        if (color < 0) color = 0;
375
                        if (color > ((STEP*6)+1)) color = ((STEP*6)+1);
414
                        if (color > ((STEP*7)+1)) color = ((STEP*7)+1);
376
 
415
 
377
               
416
               
378
                        //printf("p %u  ",ppm);
417
                        //printf("p %u  ",ppm);
379
                        //printf("pm %u  ",(min+REDUCE));
418
                        //printf("pm %u  ",(min+REDUCE));
380
                        //printf("t1 %li  ",temp1);
419
                        //printf("t1 %li  ",temp1);
381
                        //printf("t2 %li  ",temp2);
420
                        //printf("t2 %li  ",temp2);
382
                        //printf("c %i\n  ",color);                                     
421
                        //printf("c %i\n  ",color);                                     
383
               
422
               
384
               
423
               
385
                        // Farbablauf: rot > Violett > blau > tuerkis > gruen > gelb >
424
                        // Farbablauf: off > red > purple > blue > cyan > green > yellow > white
386
                        if ((color >= (STEP * 0)) && (color < (STEP * 1)))
425
                        if ((color >= (STEP * 0)) && (color < (STEP * 1)))
387
                        {
426
                        {
388
                                CH0 = MUL * ((color - (STEP * 0)));                             //fade in red > red (red only)
427
                                CH3 = MUL * ((color - (STEP * 0)));                             //fade in red > red (red only)
389
                                CH1 = 0;
428
                                CH4 = 0;
390
                                CH2 = 0;
429
                                CH5 = 0;
391
                        }
430
                        }
392
                        if ((color >= (STEP * 1)) && (color < (STEP * 2)))
431
                        if ((color >= (STEP * 1)) && (color < (STEP * 2)))
393
                        {
432
                        {
394
                                CH0 = ((STEP-1) * MUL);
433
                                CH3 = ((STEP-1) * MUL);
395
                                CH1 = 0;
434
                                CH4 = 0;
396
                                CH2 = MUL * ((color - (STEP * 1)));                             //fade in blue > purple (red + blue)
435
                                CH5 = MUL * ((color - (STEP * 1)));                             //fade in blue > purple (red + blue)
397
                        }
436
                        }
398
                        if ((color >= (STEP * 2)) && (color < (STEP * 3)))
437
                        if ((color >= (STEP * 2)) && (color < (STEP * 3)))
399
                        {
438
                        {
400
                                CH0 = MUL * ((STEP - 1) - (color - (STEP * 2)));        //fade out red > blue (blue only)
439
                                CH3 = MUL * ((STEP - 1) - (color - (STEP * 2)));        //fade out red > blue (blue only)
401
                                CH1 = 0;
440
                                CH4 = 0;
402
                                CH2 = ((STEP-1) * MUL);
441
                                CH5 = ((STEP-1) * MUL);
403
                        }
442
                        }
404
                        if ((color >= (STEP * 3)) && (color < (STEP * 4)))
443
                        if ((color >= (STEP * 3)) && (color < (STEP * 4)))
405
                        {
444
                        {
406
                                CH0 = 0;
445
                                CH3 = 0;
407
                                CH1 = MUL * ((color - (STEP * 3)));                             //fade in green > cyan (blue + green)
446
                                CH4 = MUL * ((color - (STEP * 3)));                             //fade in green > cyan (blue + green)
408
                                CH2 = ((STEP-1) * MUL);
447
                                CH5 = ((STEP-1) * MUL);
409
                        }
448
                        }
410
                        if ((color >= (STEP * 4)) && (color < (STEP * 5)))
449
                        if ((color >= (STEP * 4)) && (color < (STEP * 5)))
411
                        {
450
                        {
412
                                CH0 = 0;
451
                                CH3 = 0;
413
                                CH1 = ((STEP-1) * MUL);
452
                                CH4 = ((STEP-1) * MUL);
414
                                CH2 = MUL * ((STEP - 1) - (color - (STEP * 4)));        //fade out blue > green (green only)
453
                                CH5 = MUL * ((STEP - 1) - (color - (STEP * 4)));        //fade out blue > green (green only)
415
                        }
454
                        }
416
                        if ((color >= (STEP * 5)) && (color < (STEP * 6)))
455
                        if ((color >= (STEP * 5)) && (color < (STEP * 6)))
417
                        {
456
                        {
418
                                CH0 = MUL * ((color - (STEP * 5)));                             //fade in red > yellow (green + red)
457
                                CH3 = MUL * ((color - (STEP * 5)));                             //fade in red > yellow (green + red)
419
                                CH1 = ((STEP-1) * MUL);
458
                                CH4 = ((STEP-1) * MUL);
420
                                CH2 = 0;
459
                                CH5 = 0;
421
                        }
460
                        }
422
                        if (color >= (STEP * 6))  
461
                        if ((color >= (STEP * 6)) && (color < (STEP * 7)))
423
                        {
462
                        {
424
                                CH0 = ((STEP-1) * MUL);                                                 //fade in red > yellow (green + red)
463
                                CH3 = ((STEP-1) * MUL);
425
                                CH1 = ((STEP-1) * MUL);
464
                                CH4 = ((STEP-1) * MUL);
426
                                CH2 = 0;
465
                                CH5 = MUL * ((color - (STEP * 1)));                             //fade in blue > purple (red + blue)
427
                        }
466
                        }
-
 
467
 
-
 
468
                        if (color >= (STEP * 7))  
-
 
469
                        {
-
 
470
                                CH3 = ((STEP-1) * MUL);
-
 
471
                                CH4 = ((STEP-1) * MUL);
-
 
472
                                CH5 = ((STEP-1) * MUL);
428
                }
473
                        }
-
 
474
 
-
 
475
/************************************************************************************/                 
-
 
476
/* control CH0, CH1 and CH2     */
-
 
477
/************************************************************************************/                 
-
 
478
 
-
 
479
                        if (CheckDelay(dly1))                                           //timeout reached ?
-
 
480
                        {      
-
 
481
 
-
 
482
                                if ((flags & (1<<0))==0)                                //check if up or down
-
 
483
                                {
-
 
484
                                        if (CH0++ == 254)                                       //run CH0 up to 255
-
 
485
                                        {
-
 
486
                                                flags setbit (1<<0);                    //255 reached next time go down
-
 
487
 
-
 
488
                                        }
-
 
489
                                }
-
 
490
                                else                                                                    //run down
-
 
491
                                {
-
 
492
                                        if (CH0-- == 1)                                 //run CH0 down to 0
-
 
493
                                        {
-
 
494
                                                flags clrbit (1<<0);                    //0 reached next time go up
-
 
495
                                        }
-
 
496
                               
-
 
497
                                }
-
 
498
 
-
 
499
 
-
 
500
                                if ((flags & (1<<1))==0)                                //same for CH1
-
 
501
                                {
-
 
502
                                        if (CH1++ == 254)
-
 
503
                                        {
-
 
504
                                                flags setbit (1<<1);
-
 
505
                                        }
-
 
506
                                }
-
 
507
                                else
-
 
508
                                {
-
 
509
                                        if (CH1-- == 1)
-
 
510
                                        {
-
 
511
                                                flags clrbit (1<<1);
-
 
512
                                        }
-
 
513
                               
-
 
514
                                }
-
 
515
 
-
 
516
 
-
 
517
                                if ((flags & (1<<2))==0)                                //and same for CH2
-
 
518
                                {
-
 
519
                                        if (CH2++ == 254)
-
 
520
                                        {
-
 
521
                                                flags setbit (1<<2);
-
 
522
                                        }
-
 
523
                                }
-
 
524
                                else
-
 
525
                                {
-
 
526
                                        if (CH2-- == 1)
-
 
527
                                        {
-
 
528
                                                flags clrbit (1<<2);
-
 
529
                                        }
-
 
530
                               
-
 
531
                                }
-
 
532
 
-
 
533
 
-
 
534
                                dly1 = SetDelay(1);                                             //set next timeout in 25ms                                                                                      
-
 
535
 
-
 
536
//                              printf("ch3:%i\n",CH0);
-
 
537
                        }
-
 
538
 
429
 
539
                }
430
 
540
 
431
        }
541
        }
432
 
542
 
433
}
543
}
434
 
544
 
435
 
545