Subversion Repositories Projects

Rev

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

Rev 56 Rev 57
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 = 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
                ch0_tmp = CH0;
115
                ch0_tmp = CH0;
116
                ch1_tmp = CH1;
116
                ch1_tmp = CH1;
117
                ch2_tmp = CH2;
117
                ch2_tmp = CH2;
118
                ch3_tmp = CH3;
118
                ch3_tmp = CH3;
119
                ch4_tmp = CH4;
119
                ch4_tmp = CH4;
120
                ch5_tmp = CH5;         
120
                ch5_tmp = CH5;         
121
                counter = 0;
121
                counter = 0;
122
        }
122
        }
123
 
123
 
124
        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
125
        if (ch1_tmp == counter) PORTB_BAK LEDOFF CH1_B;         //
125
        if (ch1_tmp == counter) PORTB_BAK LEDOFF CH1_B;         //
126
        if (ch2_tmp == counter) PORTB_BAK LEDOFF CH2_B;         //
126
        if (ch2_tmp == counter) PORTB_BAK LEDOFF CH2_B;         //
127
        if (ch3_tmp == counter) PORTD_BAK LEDOFF CH3_D;         //
127
        if (ch3_tmp == counter) PORTD_BAK LEDOFF CH3_D;         //
128
        if (ch4_tmp == counter) PORTD_BAK LEDOFF CH4_D;         //
128
        if (ch4_tmp == counter) PORTD_BAK LEDOFF CH4_D;         //
129
        if (ch5_tmp == counter) PORTD_BAK LEDOFF CH5_D;         //
129
        if (ch5_tmp == counter) PORTD_BAK LEDOFF CH5_D;         //
130
       
130
       
131
        PORTB = PORTB_BAK;
131
        PORTB = PORTB_BAK;
132
        PORTD = PORTD_BAK;
132
        PORTD = PORTD_BAK;
133
       
133
       
134
        if (ms1++ == 32)
134
        if (ms1++ == 32)
135
        {
135
        {
136
                ms1=0;
136
                ms1=0;
137
                TMR1MS++;
137
                TMR1MS++;
138
        }
138
        }
139
       
139
       
140
 
140
 
141
 
141
 
142
 
142
 
143
}
143
}
144
 
144
 
145
 
145
 
146
/*##############################################################################*/
146
/*##############################################################################*/
147
unsigned int SetDelay (unsigned int t)
147
unsigned int SetDelay (unsigned int t)
148
{
148
{
149
        unsigned char hi_byte;
149
        unsigned char hi_byte;
150
        unsigned char lo_byte;  
150
        unsigned char lo_byte;  
151
 
151
 
152
        hi_byte = (TMR1MS >> 8);
152
        hi_byte = (TMR1MS >> 8);
153
        lo_byte = (TMR1MS & 0xff);
153
        lo_byte = (TMR1MS & 0xff);
154
        if (hi_byte != (TMR1MS >> 8)) hi_byte = (TMR1MS >> 8);
154
        if (hi_byte != (TMR1MS >> 8)) hi_byte = (TMR1MS >> 8);
155
 
155
 
156
        return(((hi_byte << 8) | lo_byte)  + t + 1);                                            
156
        return(((hi_byte << 8) | lo_byte)  + t + 1);                                            
157
}
157
}
158
 
158
 
159
 
159
 
160
/*##############################################################################*/
160
/*##############################################################################*/
161
char CheckDelay(unsigned int t)
161
char CheckDelay(unsigned int t)
162
{
162
{
163
        unsigned char hi_byte;
163
        unsigned char hi_byte;
164
        unsigned char lo_byte;  
164
        unsigned char lo_byte;  
165
 
165
 
166
        hi_byte = (TMR1MS >> 8);
166
        hi_byte = (TMR1MS >> 8);
167
        lo_byte = (TMR1MS & 0xff);
167
        lo_byte = (TMR1MS & 0xff);
168
        if (hi_byte != (TMR1MS >> 8)) hi_byte = (TMR1MS >> 8);
168
        if (hi_byte != (TMR1MS >> 8)) hi_byte = (TMR1MS >> 8);
169
 
169
 
170
        return(((t - ((hi_byte << 8) | lo_byte)) & 0x8000) >> 9);
170
        return(((t - ((hi_byte << 8) | lo_byte)) & 0x8000) >> 9);
171
}
171
}
172
 
172
 
173
 
173
 
174
 
174
 
175
/*##############################################################################*/
175
/*##############################################################################*/
176
void StartPWM(void)
176
void StartPWM(void)
177
{
177
{
178
        //Timer 0 Config
178
        //Timer 0 Config
179
        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
180
        TIMSK setbit (1<<TOIE0);                                                // enable Int
180
        TIMSK setbit (1<<TOIE0);                                                // enable Int
181
 
181
 
182
}
182
}
183
 
183
 
184
 
184
 
185
/*##############################################################################*/
185
/*##############################################################################*/
186
void StartPPM(void)
186
void StartPPM(void)
187
{
187
{
188
       
188
       
189
        //Timer1 Config
189
        //Timer1 Config
190
        TCCR1A =        (0<<COM1A1)|(0<<COM1A0)|(0<<COM1B1)|(0<<COM1B0)|
190
        TCCR1A =        (0<<COM1A1)|(0<<COM1A0)|(0<<COM1B1)|(0<<COM1B0)|
191
                                (0<<FOC1A) |(0<<FOC1B) |(0<<WGM10) |(0<<WGM11);                
191
                                (0<<FOC1A) |(0<<FOC1B) |(0<<WGM10) |(0<<WGM11);                
192
    TCCR1B =    (1<<ICNC1)|(1<<ICES1)|(0<<WGM13)|
192
    TCCR1B =    (1<<ICNC1)|(1<<ICES1)|(0<<WGM13)|
193
                                (0<<WGM12)|(0<<CS12)|(1<<CS11)|(0<<CS10);                               //ICP_POS_FLANKE
193
                                (0<<WGM12)|(0<<CS12)|(1<<CS11)|(0<<CS10);                               //ICP_POS_FLANKE
194
 
194
 
195
        // interrupts
195
        // interrupts
196
        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
197
 
197
 
198
}
198
}
199
 
199
 
200
 
200
 
201
/*##############################################################################*/
201
/*##############################################################################*/
202
unsigned int GetPPM(void)
202
unsigned int GetPPM(void)
203
{
203
{
204
        //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
205
        //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
206
        //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
207
       
207
       
208
        unsigned char temp_hi;
208
        unsigned char temp_hi;
209
        unsigned char temp_lo;  
209
        unsigned char temp_lo;  
210
 
210
 
211
        temp_hi = (PPM_SIGNAL >> 8);
211
        temp_hi = (PPM_SIGNAL >> 8);
212
        temp_lo = (PPM_SIGNAL & 0xff);
212
        temp_lo = (PPM_SIGNAL & 0xff);
213
        if (temp_hi != (PPM_SIGNAL >> 8)) temp_hi = (PPM_SIGNAL >> 8);
213
        if (temp_hi != (PPM_SIGNAL >> 8)) temp_hi = (PPM_SIGNAL >> 8);
214
        return( (temp_hi << 8) | temp_lo);                                            
214
        return( (temp_hi << 8) | temp_lo);                                            
215
 
215
 
216
}
216
}
217
 
217
 
218
 
218
 
219
/*##############################################################################*/
219
/*##############################################################################*/
220
// MAIN
220
// MAIN
221
/*##############################################################################*/
221
/*##############################################################################*/
222
int main (void)
222
int main (void)
223
{
223
{
224
 
224
 
225
        #define STEP            256
225
        #define STEP            256
226
        #define MUL                     1
226
        #define MUL                     1
227
        #define REDUCE          2
227
        #define REDUCE          2
228
       
228
       
229
        #define EEPSIGNATURE    0x55aa
229
        #define EEPSIGNATURE    0x55aa
-
 
230
 
-
 
231
        #define inimin          1200
-
 
232
        #define inimax          1800    
230
 
233
 
231
        unsigned int    ppm;
234
        unsigned int    ppm;
232
        signed int              color; 
235
        signed int              color; 
233
        unsigned int    setupdly;
236
        unsigned int    setupdly;
234
        unsigned int    ppmtodly;      
237
        unsigned int    ppmtodly;      
235
        unsigned int    flashdly;
238
        unsigned int    flashdly;
236
        unsigned char   setup;
239
        unsigned char   setup;
237
        unsigned int    lmax;
240
        unsigned int    lmax;
238
        unsigned int    lmin;
241
        unsigned int    lmin;
239
        unsigned int    max;
242
        unsigned int    max;
240
        unsigned int    min;
243
        unsigned int    min;
241
        signed long     temp1; 
244
        signed long             temp1; 
242
        signed long     temp2;         
245
        signed long             temp2;         
243
 
246
 
244
    DDRB  = (CH0_B|CH1_B|CH2_B);
247
    DDRB  = (CH0_B|CH1_B|CH2_B);
245
    PORTB = 0x00;
248
    PORTB = 0x00;
246
 
249
 
247
    DDRC  = (ledred);
250
    DDRC  = (ledred);
248
    PORTC = 0x00;
251
    PORTC = 0x00;
249
 
252
 
250
    DDRD  = (ledgreen|CH3_D|CH4_D|CH5_D);
253
    DDRD  = (ledgreen|CH3_D|CH4_D|CH5_D);
251
    PORTD = 0x00;
254
    PORTD = 0x00;
252
 
255
 
253
        CH0 = 0;
256
        CH0 = 0;
254
        CH1 = 0;
257
        CH1 = 0;
255
        CH2 = 0;
258
        CH2 = 0;
256
        CH3 = 0;
259
        CH3 = 0;
257
        CH4 = 0;
260
        CH4 = 0;
258
        CH5 = 0;       
261
        CH5 = 0;       
259
 
262
 
260
        lmax = 0x0000;
263
        lmax = 0x0000;
261
        lmin = 0xffff;
264
        lmin = 0xffff;
262
 
265
 
263
        StartUART();
266
//      StartUART();
264
        StartPPM();
267
        StartPPM();
265
        //StartI2C();
268
        //StartI2C();
266
        StartPWM();
269
        StartPWM();
267
        sei();
270
        sei();
-
 
271
 
-
 
272
        min = inimin;                                                                                           //default min 
-
 
273
        max = inimax;                                                                                           //default max
268
 
274
 
269
        if (eeprom_read_word(&EEPSIG) != EEPSIGNATURE)                  //check eep if signature is there
275
        if (eeprom_read_word(&EEPSIG) != EEPSIGNATURE)                          //check eep if signature is there
270
        {
-
 
271
                min = 1100;                                                                                             //default min 
-
 
272
                max = 1900;                                                                                             //default max
276
        {
273
                eeprom_write_word(&EEPMIN, min);                                                //no, write initial min
277
                eeprom_write_word(&EEPMIN, min);                                                //no, write initial min
274
                eeprom_write_word(&EEPMAX, max);                                                //and max values
278
                eeprom_write_word(&EEPMAX, max);                                                //and max values
275
                eeprom_write_word(&EEPSIG, EEPSIGNATURE);                               //along with eep signature
279
                eeprom_write_word(&EEPSIG, EEPSIGNATURE);                               //along with eep signature
276
        }
280
        }
277
        else
281
        else
278
        {
282
        {
-
 
283
 
279
                min = eeprom_read_word(&EEPMIN);                                                //signature ok
284
                min = eeprom_read_word(&EEPMIN);                                                //signature ok
280
                max = eeprom_read_word(&EEPMAX);                                                //read min and max
285
                max = eeprom_read_word(&EEPMAX);                                                //read min and max
281
        }
286
        }
-
 
287
 
282
 
288
 
283
        setup = 0;                                                                                                      //reset setup toggle counter
289
        setup = 0;                                                                                                      //reset setup toggle counter
284
        setupdly = SetDelay(5000);
290
        setupdly = SetDelay(5000);
285
        ppmtodly = SetDelay(5000);     
291
        ppmtodly = SetDelay(5000);     
286
        flashdly = SetDelay(1000);                                                             
292
        flashdly = SetDelay(1000);                                                             
287
   
293
   
288
    while (1)
294
    while (1)
289
        {
295
        {
290
 
296
 
291
                if (PPM_NEW > 20)                                                                               //ppm Signal ok 
297
                if (PPM_NEW > 20)                                                                               //ppm Signal ok 
292
                {
298
                {
293
                        PORTC clrbit ledred;
299
                        PORTC clrbit ledred;
294
                        ppm = GetPPM();                        
300
                        ppm = GetPPM();                        
295
                        ppmtodly = SetDelay(500);                                                       //reset timeout                 
301
                        ppmtodly = SetDelay(500);                                                       //reset timeout                 
296
                        if (lmax < ppm) lmax=ppm;                                                       //update impulse max 
302
                        if (lmax < ppm) lmax=ppm;                                                       //update impulse max 
297
                        if (lmin > ppm) lmin=ppm;                                                       //and min boundarys
303
                        if (lmin > ppm) lmin=ppm;                                                       //and min boundarys
298
                }
304
                }
299
                else
305
                else
300
                {
306
                {
301
                        PORTC setbit ledred;                                                            //ppm signal not ok
307
                        PORTC setbit ledred;                                                            //ppm signal not ok
302
                        ppm = min;                                                                                      //set ppm to minimum
308
                        ppm = min;                                                                                      //set ppm to minimum
303
                }
309
                }
304
               
310
               
305
 
311
 
306
                if (CheckDelay(ppmtodly))                                                               //timeout 
312
                if (CheckDelay(ppmtodly))                                                               //timeout 
307
                {
313
                {
308
                        ppmtodly = SetDelay(5000);             
314
                        ppmtodly = SetDelay(5000);             
309
                        PPM_NEW = 0;                                                                            //set ppm signal not ok
315
                        PPM_NEW = 0;                                                                            //set ppm signal not ok
310
                }      
316
                }      
311
 
317
 
312
 
318
 
313
                if (setup < 6)
319
                if (setup < 6)
314
                {
320
                {
315
                        if ((ppm > 1600) && ((setup&1)==0)) setup++;            //
321
                        if ((ppm > 1600) && ((setup&1)==0)) setup++;            //
316
                        if ((ppm < 1400) && ((setup&1)==1)) setup++;            //
322
                        if ((ppm < 1400) && ((setup&1)==1)) setup++;            //
317
 
323
 
318
                        if (CheckDelay(flashdly))                                                      
324
                        if (CheckDelay(flashdly))                                                      
319
                        {      
325
                        {      
320
                                CH0 = CH0 ^ 0xff;
326
                                CH0 = CH0 ^ 0xff;
321
                                CH1 = CH1 ^ 0xff;
327
                                CH1 = CH1 ^ 0xff;
322
                                CH2 = CH2 ^ 0xff;
328
                                CH2 = CH2 ^ 0xff;
323
                                flashdly = SetDelay(250);                                                              
329
                                flashdly = SetDelay(250);                                                              
324
                        }
330
                        }
325
                }
331
                }
326
 
332
 
327
                if (setup == 6)                                                                                 //if stick is toggled 6 times 
333
                if (setup == 6)                                                                                 //if stick is toggled 6 times 
328
                {                                                                                                               //within setup timeout
334
                {                                                                                                               //within setup timeout
329
                        PORTD setbit ledgreen;                                                          //store ppm min and max 
335
                        PORTD setbit ledgreen;                                                          //store ppm min and max 
330
                        setupdly = SetDelay(3000);                                                      //for 2000ms
336
                        setupdly = SetDelay(3000);                                                      //for 2000ms
331
                        eeprom_write_word(&EEPMIN, lmin);                                       //in eeprom
337
                        eeprom_write_word(&EEPMIN, lmin);                                       //in eeprom
332
                        eeprom_write_word(&EEPMAX, lmax);      
338
                        eeprom_write_word(&EEPMAX, lmax);      
333
                        min = lmin;                                                                                    
339
                        min = lmin;                                                                                    
334
                        max = lmax;
340
                        max = lmax;
335
                        setup = 7;                                                                                      //enter PWM toggle mode
341
                        setup = 7;                                                                                      //enter PWM toggle mode
336
 
342
 
337
                }
343
                }
338
 
344
 
339
 
345
 
340
                if (setup == 7)
346
                if (setup == 7)
341
                {
347
                {
342
                        if (CheckDelay(flashdly))                                                       //each 25ms toggle PWM's 
348
                        if (CheckDelay(flashdly))                                                       //each 25ms toggle PWM's 
343
                        {      
349
                        {      
344
                                CH0 = CH0 ^ 0xff;
350
                                CH0 = CH0 ^ 0xff;
345
                                CH1 = CH1 ^ 0xff;
351
                                CH1 = CH1 ^ 0xff;
346
                                CH2 = CH2 ^ 0xff;
352
                                CH2 = CH2 ^ 0xff;
347
                                flashdly = SetDelay(25);                                                               
353
                                flashdly = SetDelay(25);                                                               
348
                        }
354
                        }
349
               
355
               
350
                }
356
                }
351
 
357
 
352
                if (CheckDelay(setupdly))                                                               //setup timeout reached
358
                if (CheckDelay(setupdly))                                                               //setup timeout reached
353
                {
359
                {
354
                        setup = 8;                                                                                      //lockdown setup
360
                        setup = 8;                                                                                      //lockdown setup
355
                        PORTD clrbit ledgreen;
361
                        PORTD clrbit ledgreen;
356
                }
362
                }
357
 
-
 
358
                printf("ppm: %d / min: %d / max: %d\n",ppm,min,max);                   
-
 
359
               
-
 
360
               
363
 
361
               
364
               
362
                if (setup == 8)
365
                if (setup == 8)
363
                {
366
                {
-
 
367
               
-
 
368
                       
364
               
369
 
365
                        temp1 = (STEP * (signed long)30000 * (signed long)6) / ((max-REDUCE) - (min+REDUCE));
370
                        temp1 = ((long)STEP * 6 * 20000) / ((max-REDUCE) - (min+REDUCE));
366
                        temp2 = ((ppm - (min+REDUCE)) * temp1);
371
                        temp2 = (((int)ppm - ((int)min+REDUCE)) * temp1);
367
                        color = temp2 / 30000;
372
                        color = temp2 / 20000;
368
               
373
                       
369
                        if (color < 0) color = 0;
374
                        if (color < 0) color = 0;
-
 
375
                        if (color > ((STEP*6)+1)) color = ((STEP*6)+1);
-
 
376
 
-
 
377
               
-
 
378
                        //printf("p %u  ",ppm);
-
 
379
                        //printf("pm %u  ",(min+REDUCE));
-
 
380
                        //printf("t1 %li  ",temp1);
-
 
381
                        //printf("t2 %li  ",temp2);
370
                        if (color > (STEP * 6)) color = (STEP * 6);            
-
 
371
               
382
                        //printf("c %i\n  ",color);                                     
372
                        printf("color: %d\n",color);
383
               
373
               
384
               
374
                        // Farbablauf: rot > Violett > blau > tuerkis > gruen > gelb >
385
                        // Farbablauf: rot > Violett > blau > tuerkis > gruen > gelb >
375
                        if ((color >= (STEP * 0)) && (color < (STEP * 1)))
386
                        if ((color >= (STEP * 0)) && (color < (STEP * 1)))
376
                        {
387
                        {
377
                                CH0 = MUL * ((color - (STEP * 0)));                             //fade in red > red (red only)
388
                                CH0 = MUL * ((color - (STEP * 0)));                             //fade in red > red (red only)
378
                                CH1 = 0;
389
                                CH1 = 0;
379
                                CH2 = 0;
390
                                CH2 = 0;
380
                        }
391
                        }
381
                        if ((color >= (STEP * 1)) && (color < (STEP * 2)))
392
                        if ((color >= (STEP * 1)) && (color < (STEP * 2)))
382
                        {
393
                        {
383
                                CH0 = ((STEP-1) * MUL);
394
                                CH0 = ((STEP-1) * MUL);
384
                                CH1 = 0;
395
                                CH1 = 0;
385
                                CH2 = MUL * ((color - (STEP * 1)));                             //fade in blue > purple (red + blue)
396
                                CH2 = MUL * ((color - (STEP * 1)));                             //fade in blue > purple (red + blue)
386
                        }
397
                        }
387
                        if ((color >= (STEP * 2)) && (color < (STEP * 3)))
398
                        if ((color >= (STEP * 2)) && (color < (STEP * 3)))
388
                        {
399
                        {
389
                                CH0 = MUL * ((STEP - 1) - (color - (STEP * 2)));        //fade out red > blue (blue only)
400
                                CH0 = MUL * ((STEP - 1) - (color - (STEP * 2)));        //fade out red > blue (blue only)
390
                                CH1 = 0;
401
                                CH1 = 0;
391
                                CH2 = ((STEP-1) * MUL);
402
                                CH2 = ((STEP-1) * MUL);
392
                        }
403
                        }
393
                        if ((color >= (STEP * 3)) && (color < (STEP * 4)))
404
                        if ((color >= (STEP * 3)) && (color < (STEP * 4)))
394
                        {
405
                        {
395
                                CH0 = 0;
406
                                CH0 = 0;
396
                                CH1 = MUL * ((color - (STEP * 3)));                             //fade in green > cyan (blue + green)
407
                                CH1 = MUL * ((color - (STEP * 3)));                             //fade in green > cyan (blue + green)
397
                                CH2 = ((STEP-1) * MUL);
408
                                CH2 = ((STEP-1) * MUL);
398
                        }
409
                        }
399
                        if ((color >= (STEP * 4)) && (color < (STEP * 5)))
410
                        if ((color >= (STEP * 4)) && (color < (STEP * 5)))
400
                        {
411
                        {
401
                                CH0 = 0;
412
                                CH0 = 0;
402
                                CH1 = ((STEP-1) * MUL);
413
                                CH1 = ((STEP-1) * MUL);
403
                                CH2 = MUL * ((STEP - 1) - (color - (STEP * 4)));        //fade out blue > green (green only)
414
                                CH2 = MUL * ((STEP - 1) - (color - (STEP * 4)));        //fade out blue > green (green only)
404
                        }
415
                        }
405
                        if ((color >= (STEP * 5)) && (color < (STEP * 6)))
416
                        if ((color >= (STEP * 5)) && (color < (STEP * 6)))
406
                        {
417
                        {
407
                                CH0 = MUL * ((color - (STEP * 5)));                             //fade in red > yellow (green + red)
418
                                CH0 = MUL * ((color - (STEP * 5)));                             //fade in red > yellow (green + red)
408
                                CH1 = ((STEP-1) * MUL);
419
                                CH1 = ((STEP-1) * MUL);
409
                                CH2 = 0;
420
                                CH2 = 0;
410
                        }
421
                        }
411
                        if (color >= (STEP * 6))  
422
                        if (color >= (STEP * 6))  
412
                        {
423
                        {
413
                                CH0 = ((STEP-1) * MUL);                                                 //fade in red > yellow (green + red)
424
                                CH0 = ((STEP-1) * MUL);                                                 //fade in red > yellow (green + red)
414
                                CH1 = ((STEP-1) * MUL);
425
                                CH1 = ((STEP-1) * MUL);
415
                                CH2 = 0;
426
                                CH2 = 0;
416
                        }
427
                        }
417
                }
428
                }
418
 
429
 
419
 
430
 
420
        }
431
        }
421
 
432
 
422
}
433
}
423
 
434
 
424
 
435