Subversion Repositories Projects

Rev

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

Rev 55 Rev 56
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 = 1500;
17
volatile unsigned int PPM_SIGNAL = 1500;
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
 
-
 
28
uint16_t EEMEM EEPMIN=0x0000;
-
 
29
uint16_t EEMEM EEPMAX=0xffff;
-
 
30
uint16_t EEMEM EEPSIG=0xffff;
-
 
31
 
-
 
32
 
-
 
33
//led kennlinie, (noch nicht implementiert, nur vorbereitet)
-
 
34
unsigned char kl[256]={
-
 
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,
-
 
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,
-
 
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,
-
 
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,
-
 
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,
-
 
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,
-
 
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,
-
 
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
-
 
51
};
-
 
52
 
27
 
53
 
28
 
54
 
29
/*##############################################################################*/
55
/*##############################################################################*/
30
SIGNAL(SIG_OVERFLOW1)
56
SIGNAL(SIG_OVERFLOW1)
31
{
57
{
32
        TMR1OvF++;
58
        TMR1OvF++;
33
}
59
}
34
 
60
 
35
 
61
 
36
SIGNAL(SIG_INPUT_CAPTURE1)
62
SIGNAL(SIG_INPUT_CAPTURE1)
37
{
63
{
38
        static unsigned int pos_ICR;
64
        static unsigned int pos_ICR;
39
        static unsigned int ppm;
65
        static unsigned int ppm;
40
       
66
       
41
        if ((TCCR1B & (1<<ICES1)) != 0)                                         //rising edge
67
        if ((TCCR1B & (1<<ICES1)) != 0)                                         //rising edge
42
        {
68
        {
43
                TCCR1B &= ~(1<<ICES1);                                                          //set falling egde
69
                TCCR1B &= ~(1<<ICES1);                                                          //set falling egde
44
                TMR1OvF = 0;
70
                TMR1OvF = 0;
45
                pos_ICR = ICR1;
71
                pos_ICR = ICR1;
46
        }
72
        }
47
        else                                                                                    //falling edge
73
        else                                                                                    //falling edge
48
        {
74
        {
49
                TCCR1B |= (1<<ICES1);                                                           //set rising egde
75
                TCCR1B |= (1<<ICES1);                                                           //set rising egde
50
                ppm = (ICR1 - pos_ICR + (int) TMR1OvF * 65536);
76
                ppm = (ICR1 - pos_ICR + (int) TMR1OvF * 65536);
51
                if ((ppm > 600) && (ppm < 2400))
77
                if ((ppm > 600) && (ppm < 2400))
52
                {
78
                {
53
                        if (ppm > 2100) ppm = 2100;
79
                        if (ppm > 2100) ppm = 2100;
54
                        if (ppm < 900) ppm = 900;
80
                        if (ppm < 900) ppm = 900;
55
                        ppm = (PPM_SIGNAL * 3 + ppm) / 4;
81
                        ppm = (PPM_SIGNAL * 3 + ppm) / 4;
56
                        PPM_SIGNAL = ppm;
82
                        PPM_SIGNAL = ppm;
57
                        if (PPM_NEW < 50) PPM_NEW++;
83
                        if (PPM_NEW < 50) PPM_NEW++;
58
                }
84
                }
59
               
85
               
60
        }
86
        }
61
 
87
 
62
}
88
}
63
 
89
 
64
/*##############################################################################*/
90
/*##############################################################################*/
65
SIGNAL(SIG_OVERFLOW0)
91
SIGNAL(SIG_OVERFLOW0)
66
{
92
{
67
        // this function is called every 32us,
93
        // this function is called every 32us,
68
        // 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
69
        // currently it's about 20us
95
        // currently it's about 20us
70
 
96
 
71
        static unsigned char counter = 254;
97
        static unsigned char counter = 254;
72
        static unsigned char ms1 = 0;
98
        static unsigned char ms1 = 0;
73
        static unsigned char ch0_tmp = 0;
99
        static unsigned char ch0_tmp = 0;
74
        static unsigned char ch1_tmp = 0;
100
        static unsigned char ch1_tmp = 0;
75
        static unsigned char ch2_tmp = 0;
101
        static unsigned char ch2_tmp = 0;
76
        static unsigned char ch3_tmp = 0;
102
        static unsigned char ch3_tmp = 0;
77
        static unsigned char ch4_tmp = 0;
103
        static unsigned char ch4_tmp = 0;
78
        static unsigned char ch5_tmp = 0;
104
        static unsigned char ch5_tmp = 0;
79
        unsigned char PORTB_BAK;
105
        unsigned char PORTB_BAK;
80
        unsigned char PORTD_BAK;       
106
        unsigned char PORTD_BAK;       
81
 
107
 
82
        PORTB_BAK = PORTB;
108
        PORTB_BAK = PORTB;
83
        PORTD_BAK = PORTD;
109
        PORTD_BAK = PORTD;
84
 
110
 
85
        if (counter++ == 254)
111
        if (counter++ == 254)
86
        {
112
        {
87
                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
88
                PORTD_BAK LEDON (CH3_D | CH4_D | CH5_D);                //
114
                PORTD_BAK LEDON (CH3_D | CH4_D | CH5_D);                //
89
                ch0_tmp = CH0;
115
                ch0_tmp = CH0;
90
                ch1_tmp = CH1;
116
                ch1_tmp = CH1;
91
                ch2_tmp = CH2;
117
                ch2_tmp = CH2;
92
                ch3_tmp = CH3;
118
                ch3_tmp = CH3;
93
                ch4_tmp = CH4;
119
                ch4_tmp = CH4;
94
                ch5_tmp = CH5;         
120
                ch5_tmp = CH5;         
95
                counter = 0;
121
                counter = 0;
96
        }
122
        }
97
 
123
 
98
        if (ch0_tmp == counter) PORTB_BAK LEDOFF CH0_B;         //channel value reached, output off
124
        if (ch0_tmp == counter) PORTB_BAK LEDOFF CH0_B;         //channel value reached, output off
99
        if (ch1_tmp == counter) PORTB_BAK LEDOFF CH1_B;         //
125
        if (ch1_tmp == counter) PORTB_BAK LEDOFF CH1_B;         //
100
        if (ch2_tmp == counter) PORTB_BAK LEDOFF CH2_B;         //
126
        if (ch2_tmp == counter) PORTB_BAK LEDOFF CH2_B;         //
101
        if (ch3_tmp == counter) PORTD_BAK LEDOFF CH3_D;         //
127
        if (ch3_tmp == counter) PORTD_BAK LEDOFF CH3_D;         //
102
        if (ch4_tmp == counter) PORTD_BAK LEDOFF CH4_D;         //
128
        if (ch4_tmp == counter) PORTD_BAK LEDOFF CH4_D;         //
103
        if (ch5_tmp == counter) PORTD_BAK LEDOFF CH5_D;         //
129
        if (ch5_tmp == counter) PORTD_BAK LEDOFF CH5_D;         //
104
       
130
       
105
        PORTB = PORTB_BAK;
131
        PORTB = PORTB_BAK;
106
        PORTD = PORTD_BAK;
132
        PORTD = PORTD_BAK;
107
       
133
       
108
        if (ms1++ == 32)
134
        if (ms1++ == 32)
109
        {
135
        {
110
                ms1=0;
136
                ms1=0;
111
                TMR1MS++;
137
                TMR1MS++;
112
        }
138
        }
113
       
139
       
114
 
140
 
115
 
141
 
116
 
142
 
117
}
143
}
118
 
144
 
119
 
145
 
120
/*##############################################################################*/
146
/*##############################################################################*/
121
unsigned int SetDelay (unsigned int t)
147
unsigned int SetDelay (unsigned int t)
122
{
148
{
123
        unsigned char hi_byte;
149
        unsigned char hi_byte;
124
        unsigned char lo_byte;  
150
        unsigned char lo_byte;  
125
 
151
 
126
        hi_byte = (TMR1MS >> 8);
152
        hi_byte = (TMR1MS >> 8);
127
        lo_byte = (TMR1MS & 0xff);
153
        lo_byte = (TMR1MS & 0xff);
128
        if (hi_byte != (TMR1MS >> 8)) hi_byte = (TMR1MS >> 8);
154
        if (hi_byte != (TMR1MS >> 8)) hi_byte = (TMR1MS >> 8);
129
 
155
 
130
        return(((hi_byte << 8) | lo_byte)  + t + 1);                                            
156
        return(((hi_byte << 8) | lo_byte)  + t + 1);                                            
131
}
157
}
132
 
158
 
133
 
159
 
134
/*##############################################################################*/
160
/*##############################################################################*/
135
char CheckDelay(unsigned int t)
161
char CheckDelay(unsigned int t)
136
{
162
{
137
        unsigned char hi_byte;
163
        unsigned char hi_byte;
138
        unsigned char lo_byte;  
164
        unsigned char lo_byte;  
139
 
165
 
140
        hi_byte = (TMR1MS >> 8);
166
        hi_byte = (TMR1MS >> 8);
141
        lo_byte = (TMR1MS & 0xff);
167
        lo_byte = (TMR1MS & 0xff);
142
        if (hi_byte != (TMR1MS >> 8)) hi_byte = (TMR1MS >> 8);
168
        if (hi_byte != (TMR1MS >> 8)) hi_byte = (TMR1MS >> 8);
143
 
169
 
144
        return(((t - ((hi_byte << 8) | lo_byte)) & 0x8000) >> 9);
170
        return(((t - ((hi_byte << 8) | lo_byte)) & 0x8000) >> 9);
145
}
171
}
146
 
172
 
147
 
173
 
148
 
174
 
149
/*##############################################################################*/
175
/*##############################################################################*/
150
void StartPWM(void)
176
void StartPWM(void)
151
{
177
{
152
        //Timer 0 Config
178
        //Timer 0 Config
153
        TCCR0 = (0<<CS02)|(0<<CS01)|(1<<CS00);          // (@8MHz) =  1/8us Clk = 256/8 = 32us overflow
179
        TCCR0 = (0<<CS02)|(0<<CS01)|(1<<CS00);          // (@8MHz) =  1/8us Clk = 256/8 = 32us overflow
154
        TIMSK setbit (1<<TOIE0);                                                // enable Int
180
        TIMSK setbit (1<<TOIE0);                                                // enable Int
155
 
181
 
156
}
182
}
157
 
183
 
158
 
184
 
159
/*##############################################################################*/
185
/*##############################################################################*/
160
void StartPPM(void)
186
void StartPPM(void)
161
{
187
{
162
       
188
       
163
        //Timer1 Config
189
        //Timer1 Config
164
        TCCR1A =        (0<<COM1A1)|(0<<COM1A0)|(0<<COM1B1)|(0<<COM1B0)|
190
        TCCR1A =        (0<<COM1A1)|(0<<COM1A0)|(0<<COM1B1)|(0<<COM1B0)|
165
                                (0<<FOC1A) |(0<<FOC1B) |(0<<WGM10) |(0<<WGM11);                
191
                                (0<<FOC1A) |(0<<FOC1B) |(0<<WGM10) |(0<<WGM11);                
166
    TCCR1B =    (1<<ICNC1)|(1<<ICES1)|(0<<WGM13)|
192
    TCCR1B =    (1<<ICNC1)|(1<<ICES1)|(0<<WGM13)|
167
                                (0<<WGM12)|(0<<CS12)|(1<<CS11)|(0<<CS10);                               //ICP_POS_FLANKE
193
                                (0<<WGM12)|(0<<CS12)|(1<<CS11)|(0<<CS10);                               //ICP_POS_FLANKE
168
 
194
 
169
        // interrupts
195
        // interrupts
170
        TIMSK |=        (1<<TICIE1)|(1<<TOIE1);                                                                 //ICP_INT_ENABLE and TIMER1_INT_ENABLE
196
        TIMSK |=        (1<<TICIE1)|(1<<TOIE1);                                                                 //ICP_INT_ENABLE and TIMER1_INT_ENABLE
171
 
197
 
172
}
198
}
173
 
199
 
174
 
200
 
175
/*##############################################################################*/
201
/*##############################################################################*/
176
unsigned int GetPPM(void)
202
unsigned int GetPPM(void)
177
{
203
{
178
        //this routines seems to be nesseccary, as reading a 16 bit value
204
        //this routines seems to be nesseccary, as reading a 16 bit value
179
        //on a 8 bit machine is not atomic, so if an interrupt apears between reading
205
        //on a 8 bit machine is not atomic, so if an interrupt apears between reading
180
        //low and high byte of the 16 bit value a wrong result is possible
206
        //low and high byte of the 16 bit value a wrong result is possible
181
       
207
       
182
        unsigned char temp_hi;
208
        unsigned char temp_hi;
183
        unsigned char temp_lo;  
209
        unsigned char temp_lo;  
184
 
210
 
185
        temp_hi = (PPM_SIGNAL >> 8);
211
        temp_hi = (PPM_SIGNAL >> 8);
186
        temp_lo = (PPM_SIGNAL & 0xff);
212
        temp_lo = (PPM_SIGNAL & 0xff);
187
        if (temp_hi != (PPM_SIGNAL >> 8)) temp_hi = (PPM_SIGNAL >> 8);
213
        if (temp_hi != (PPM_SIGNAL >> 8)) temp_hi = (PPM_SIGNAL >> 8);
188
        return( (temp_hi << 8) | temp_lo);                                            
214
        return( (temp_hi << 8) | temp_lo);                                            
189
 
215
 
190
}
216
}
191
 
217
 
192
 
218
 
193
/*##############################################################################*/
219
/*##############################################################################*/
194
// MAIN
220
// MAIN
195
/*##############################################################################*/
221
/*##############################################################################*/
196
int main (void)
222
int main (void)
197
{
223
{
198
 
224
 
199
        #define STEP            256
225
        #define STEP            256
200
        #define MUL                     1
226
        #define MUL                     1
201
        #define REDUCE          5
227
        #define REDUCE          2
202
       
-
 
203
        #define EEPADRSIG       0x00
-
 
204
        #define EEPADRMIN       0x20
-
 
205
        #define EEPADRMAX       0x22
228
       
206
        #define EEPSIGATURE     0x55aa
229
        #define EEPSIGNATURE    0x55aa
207
 
230
 
208
        unsigned int    ppm;
231
        unsigned int    ppm;
209
        signed int              color; 
232
        signed int              color; 
210
        unsigned int    setupdly;
233
        unsigned int    setupdly;
211
        unsigned int    ppmtodly;      
234
        unsigned int    ppmtodly;      
212
        unsigned int    flashdly;
235
        unsigned int    flashdly;
213
        unsigned char   setup;
236
        unsigned char   setup;
214
        unsigned int    lmax;
237
        unsigned int    lmax;
215
        unsigned int    lmin;
238
        unsigned int    lmin;
216
        unsigned int    max;
239
        unsigned int    max;
217
        unsigned int    min;
240
        unsigned int    min;
218
        unsigned long   temp1; 
241
        signed long     temp1; 
219
        unsigned long   temp2;         
242
        signed long     temp2;         
220
 
-
 
221
 
-
 
222
 
243
 
223
    DDRB  = (CH0_B|CH1_B|CH2_B);
244
    DDRB  = (CH0_B|CH1_B|CH2_B);
224
    PORTB = 0x00;
245
    PORTB = 0x00;
225
 
246
 
226
    DDRC  = (ledred);
247
    DDRC  = (ledred);
227
    PORTC = 0x00;
248
    PORTC = 0x00;
228
 
249
 
229
    DDRD  = (ledgreen|CH3_D|CH4_D|CH5_D);
250
    DDRD  = (ledgreen|CH3_D|CH4_D|CH5_D);
230
    PORTD = 0x00;
251
    PORTD = 0x00;
231
 
252
 
232
        CH0 = 0;
253
        CH0 = 0;
233
        CH1 = 0;
254
        CH1 = 0;
234
        CH2 = 0;
255
        CH2 = 0;
235
        CH3 = 0;
256
        CH3 = 0;
236
        CH4 = 0;
257
        CH4 = 0;
237
        CH5 = 0;       
258
        CH5 = 0;       
238
 
259
 
239
        lmax = 0x0000;
260
        lmax = 0x0000;
240
        lmin = 0xffff;
261
        lmin = 0xffff;
241
 
262
 
242
        StartUART();
263
        StartUART();
243
        StartPPM();
264
        StartPPM();
244
        //StartI2C();
265
        //StartI2C();
245
        StartPWM();
266
        StartPWM();
246
        sei();
267
        sei();
247
 
-
 
248
 
-
 
249
 
268
 
250
        if (eeprom_read_word(EEPADRSIG) != EEPSIGATURE)                 //check eep if signature is there
269
        if (eeprom_read_word(&EEPSIG) != EEPSIGNATURE)                  //check eep if signature is there
251
        {
270
        {
252
                min = 1100;                                                                                             //default min 
271
                min = 1100;                                                                                             //default min 
253
                max = 1900;                                                                                             //default max
272
                max = 1900;                                                                                             //default max
254
                eeprom_write_word(EEPADRMIN, min);                                              //no, write initial min
273
                eeprom_write_word(&EEPMIN, min);                                                //no, write initial min
255
                eeprom_write_word(EEPADRMAX, max);                                              //and max values
274
                eeprom_write_word(&EEPMAX, max);                                                //and max values
256
                eeprom_write_word(EEPADRSIG, EEPSIGATURE);                              //along with eep signature
275
                eeprom_write_word(&EEPSIG, EEPSIGNATURE);                               //along with eep signature
257
        }
276
        }
258
        else
277
        else
259
        {
278
        {
260
                min = eeprom_read_word(EEPADRMIN);                                              //signature ok
279
                min = eeprom_read_word(&EEPMIN);                                                //signature ok
261
                max = eeprom_read_word(EEPADRMAX);                                              //read min and max
280
                max = eeprom_read_word(&EEPMAX);                                                //read min and max
262
        }
281
        }
263
 
282
 
264
        setup = 0;                                                                                                      //reset setup toggle counter
283
        setup = 0;                                                                                                      //reset setup toggle counter
265
        setupdly = SetDelay(3000);
284
        setupdly = SetDelay(5000);
266
        ppmtodly = SetDelay(5000);     
285
        ppmtodly = SetDelay(5000);     
267
        flashdly = SetDelay(100);                                                              
286
        flashdly = SetDelay(1000);                                                             
268
   
287
   
269
    while (1)
288
    while (1)
270
        {
289
        {
271
 
290
 
272
                if (PPM_NEW > 20)                                                                               //ppm Signal ok 
291
                if (PPM_NEW > 20)                                                                               //ppm Signal ok 
273
                {
292
                {
274
                        PORTC clrbit ledred;
293
                        PORTC clrbit ledred;
275
                        ppm = GetPPM();                        
294
                        ppm = GetPPM();                        
276
                        ppmtodly = SetDelay(500);                                                       //reset timeout                 
295
                        ppmtodly = SetDelay(500);                                                       //reset timeout                 
277
                        if (lmax < ppm) lmax=ppm;                                                       //update impulse max 
296
                        if (lmax < ppm) lmax=ppm;                                                       //update impulse max 
278
                        if (lmin > ppm) lmin=ppm;                                                       //and min boundarys
297
                        if (lmin > ppm) lmin=ppm;                                                       //and min boundarys
279
                }
298
                }
280
                else
299
                else
281
                {
300
                {
282
                        PORTC setbit ledred;                                                            //ppm signal not ok
301
                        PORTC setbit ledred;                                                            //ppm signal not ok
283
                        ppm = min;                                                                                      //set ppm to minimum
302
                        ppm = min;                                                                                      //set ppm to minimum
284
                }
303
                }
285
               
304
               
286
 
305
 
287
                if (CheckDelay(ppmtodly))                                                               //timeout 
306
                if (CheckDelay(ppmtodly))                                                               //timeout 
288
                {
307
                {
289
                        ppmtodly = SetDelay(5000);             
308
                        ppmtodly = SetDelay(5000);             
290
                        PPM_NEW = 0;                                                                            //set ppm signal not ok
309
                        PPM_NEW = 0;                                                                            //set ppm signal not ok
291
                }      
310
                }      
292
 
311
 
293
 
312
 
294
                if (setup < 6)
313
                if (setup < 6)
295
                {
314
                {
296
                        if ((ppm > 1600) && ((setup&1)==0)) setup++;            //
315
                        if ((ppm > 1600) && ((setup&1)==0)) setup++;            //
297
                        if ((ppm < 1400) && ((setup&1)==1)) setup++;            //
316
                        if ((ppm < 1400) && ((setup&1)==1)) setup++;            //
298
 
317
 
299
                        if (CheckDelay(flashdly))                                                               //setup timeout reached
318
                        if (CheckDelay(flashdly))                                                      
300
                        {      
319
                        {      
301
                                CH0 = CH0 ^ 0xff;
320
                                CH0 = CH0 ^ 0xff;
302
                                CH1 = CH1 ^ 0xff;
321
                                CH1 = CH1 ^ 0xff;
303
                                CH2 = CH2 ^ 0xff;
322
                                CH2 = CH2 ^ 0xff;
304
                                flashdly = SetDelay(250);                                                              
323
                                flashdly = SetDelay(250);                                                              
305
                        }
324
                        }
306
 
-
 
307
                }
325
                }
308
 
326
 
309
                if (setup == 6)                                                                                 //if stick is toggled 6 times 
327
                if (setup == 6)                                                                                 //if stick is toggled 6 times 
310
                {                                                                                                               //within setup timeout
328
                {                                                                                                               //within setup timeout
311
                        PORTD setbit ledgreen;                                                          //store ppm min and max 
329
                        PORTD setbit ledgreen;                                                          //store ppm min and max 
-
 
330
                        setupdly = SetDelay(3000);                                                      //for 2000ms
312
                        eeprom_write_word(EEPADRMIN, lmin);                                     //in eeprom
331
                        eeprom_write_word(&EEPMIN, lmin);                                       //in eeprom
313
                        eeprom_write_word(EEPADRMAX, lmax);    
332
                        eeprom_write_word(&EEPMAX, lmax);      
314
                        min = lmin;                                                                                    
333
                        min = lmin;                                                                                    
315
                        max = lmax;
334
                        max = lmax;
316
                        setupdly = SetDelay(2000);                                     
335
                        setup = 7;                                                                                      //enter PWM toggle mode
-
 
336
 
-
 
337
                }
-
 
338
 
-
 
339
 
-
 
340
                if (setup == 7)
317
 
341
                {
318
                        if (CheckDelay(flashdly))                                                               //setup timeout reached
342
                        if (CheckDelay(flashdly))                                                       //each 25ms toggle PWM's 
319
                        {      
343
                        {      
320
                                CH0 = CH0 ^ 0xff;
344
                                CH0 = CH0 ^ 0xff;
321
                                CH1 = CH1 ^ 0xff;
345
                                CH1 = CH1 ^ 0xff;
322
                                CH2 = CH2 ^ 0xff;
346
                                CH2 = CH2 ^ 0xff;
323
                                flashdly = SetDelay(25);                                                               
347
                                flashdly = SetDelay(25);                                                               
324
                        }
348
                        }
325
 
349
               
326
                }
350
                }
327
 
351
 
328
                if (CheckDelay(setupdly))                                                               //setup timeout reached
352
                if (CheckDelay(setupdly))                                                               //setup timeout reached
329
                {
353
                {
330
                        setup = 7;                                                                                      //lockdown setup
354
                        setup = 8;                                                                                      //lockdown setup
331
                        PORTD clrbit ledgreen;
355
                        PORTD clrbit ledgreen;
332
                }
356
                }
333
 
357
 
334
                //printf("ppm: %d / min: %d / max: %d\n",ppm,lmin,lmax);                        
358
                printf("ppm: %d / min: %d / max: %d\n",ppm,min,max);                   
335
               
359
               
336
               
360
               
337
               
361
               
338
                if (setup = 7)
362
                if (setup == 8)
339
                {
363
                {
340
               
364
               
341
                        temp1 = (STEP * (long)40000 * (long)6) / ((max-REDUCE) - (min+REDUCE));
365
                        temp1 = (STEP * (signed long)30000 * (signed long)6) / ((max-REDUCE) - (min+REDUCE));
342
                        temp2 = ((ppm - (min+REDUCE)) * temp1);
366
                        temp2 = ((ppm - (min+REDUCE)) * temp1);
343
                        color = temp2 / 40000;
367
                        color = temp2 / 30000;
344
               
368
               
345
                        if (color < 0) color = 0;
369
                        if (color < 0) color = 0;
346
                        if (color > (STEP * 6)) color = (STEP * 6);            
370
                        if (color > (STEP * 6)) color = (STEP * 6);            
-
 
371
               
-
 
372
                        printf("color: %d\n",color);
347
               
373
               
348
                        // Farbablauf: rot > Violett > blau > tuerkis > gruen > gelb >
374
                        // Farbablauf: rot > Violett > blau > tuerkis > gruen > gelb >
349
                        if ((color >= (STEP * 0)) && (color < (STEP * 1)))
375
                        if ((color >= (STEP * 0)) && (color < (STEP * 1)))
350
                        {
376
                        {
351
                                CH0 = MUL * ((color - (STEP * 0)));                             //fade in red > red (red only)
377
                                CH0 = MUL * ((color - (STEP * 0)));                             //fade in red > red (red only)
352
                                CH1 = 0;
378
                                CH1 = 0;
353
                                CH2 = 0;
379
                                CH2 = 0;
354
                        }
380
                        }
355
                        if ((color >= (STEP * 1)) && (color < (STEP * 2)))
381
                        if ((color >= (STEP * 1)) && (color < (STEP * 2)))
356
                        {
382
                        {
357
                                CH0 = ((STEP-1) * MUL);
383
                                CH0 = ((STEP-1) * MUL);
358
                                CH1 = 0;
384
                                CH1 = 0;
359
                                CH2 = MUL * ((color - (STEP * 1)));                             //fade in blue > purple (red + blue)
385
                                CH2 = MUL * ((color - (STEP * 1)));                             //fade in blue > purple (red + blue)
360
                        }
386
                        }
361
                        if ((color >= (STEP * 2)) && (color < (STEP * 3)))
387
                        if ((color >= (STEP * 2)) && (color < (STEP * 3)))
362
                        {
388
                        {
363
                                CH0 = MUL * ((STEP - 1) - (color - (STEP * 2)));        //fade out red > blue (blue only)
389
                                CH0 = MUL * ((STEP - 1) - (color - (STEP * 2)));        //fade out red > blue (blue only)
364
                                CH1 = 0;
390
                                CH1 = 0;
365
                                CH2 = ((STEP-1) * MUL);
391
                                CH2 = ((STEP-1) * MUL);
366
                        }
392
                        }
367
                        if ((color >= (STEP * 3)) && (color < (STEP * 4)))
393
                        if ((color >= (STEP * 3)) && (color < (STEP * 4)))
368
                        {
394
                        {
369
                                CH0 = 0;
395
                                CH0 = 0;
370
                                CH1 = MUL * ((color - (STEP * 3)));                             //fade in green > cyan (blue + green)
396
                                CH1 = MUL * ((color - (STEP * 3)));                             //fade in green > cyan (blue + green)
371
                                CH2 = ((STEP-1) * MUL);
397
                                CH2 = ((STEP-1) * MUL);
372
                        }
398
                        }
373
                        if ((color >= (STEP * 4)) && (color < (STEP * 5)))
399
                        if ((color >= (STEP * 4)) && (color < (STEP * 5)))
374
                        {
400
                        {
375
                                CH0 = 0;
401
                                CH0 = 0;
376
                                CH1 = ((STEP-1) * MUL);
402
                                CH1 = ((STEP-1) * MUL);
377
                                CH2 = MUL * ((STEP - 1) - (color - (STEP * 4)));        //fade out blue > green (green only)
403
                                CH2 = MUL * ((STEP - 1) - (color - (STEP * 4)));        //fade out blue > green (green only)
378
                        }
404
                        }
379
                        if ((color >= (STEP * 5)) && (color < (STEP * 6)))
405
                        if ((color >= (STEP * 5)) && (color < (STEP * 6)))
380
                        {
406
                        {
381
                                CH0 = MUL * ((color - (STEP * 5)));                             //fade in red > yellow (green + red)
407
                                CH0 = MUL * ((color - (STEP * 5)));                             //fade in red > yellow (green + red)
382
                                CH1 = ((STEP-1) * MUL);
408
                                CH1 = ((STEP-1) * MUL);
383
                                CH2 = 0;
409
                                CH2 = 0;
384
                        }
410
                        }
385
                        if (color >= (STEP * 6))  
411
                        if (color >= (STEP * 6))  
386
                        {
412
                        {
387
                                CH0 = ((STEP-1) * MUL);                                                 //fade in red > yellow (green + red)
413
                                CH0 = ((STEP-1) * MUL);                                                 //fade in red > yellow (green + red)
388
                                CH1 = ((STEP-1) * MUL);
414
                                CH1 = ((STEP-1) * MUL);
389
                                CH2 = 0;
415
                                CH2 = 0;
390
                        }
416
                        }
391
                }
417
                }
392
 
418
 
393
 
419
 
394
        }
420
        }
395
 
421
 
396
}
422
}
397
 
423
 
398
 
424