Removed the obsolete `of' variable.
[kokare.git] / kokare.c
1 #include <avr/io.h>
2 #include <avr/interrupt.h>
3 #include <inttypes.h>
4 #include <math.h>
5
6 #define SEGA 4
7 #define SEGB 2
8 #define SEGC 1
9 #define SEGD 32
10 #define SEGE 64
11 #define SEGF 16
12 #define SEGG 8
13 #define SEGP 128
14
15 uint8_t font[16] = {
16     SEGA | SEGB | SEGC | SEGD | SEGE | SEGF,
17     SEGB | SEGC,
18     SEGA | SEGB | SEGD | SEGE | SEGG,
19     SEGA | SEGB | SEGC | SEGD | SEGG,
20     SEGB | SEGC | SEGF | SEGG,
21     SEGA | SEGC | SEGD | SEGF | SEGG,
22     SEGA | SEGC | SEGD | SEGE | SEGF | SEGG,
23     SEGA | SEGB | SEGC,
24     SEGA | SEGB | SEGC | SEGD | SEGE | SEGF | SEGG,
25     SEGA | SEGB | SEGC | SEGD | SEGF | SEGG,
26     SEGA | SEGB | SEGC | SEGE | SEGF | SEGG,
27     SEGC | SEGD | SEGE | SEGF | SEGG,
28     SEGA | SEGD | SEGE | SEGF,
29     SEGB | SEGC | SEGD | SEGE | SEGG,
30     SEGA | SEGD | SEGE | SEGF | SEGG,
31     SEGA | SEGE | SEGF | SEGG,
32 };
33 /* LED */
34 uint8_t dsp[2] = {0, 0};
35 char leda = 0;
36 char ledc = 0;
37 /* Timer */
38 volatile int oticks = 0;
39 unsigned long mnow;
40 /* Pulse counter */
41 volatile char pstate = 0;
42 char pval = 0;
43 /* Switch */
44 volatile char sstate = 0;
45 int stime = 0;
46 /* Temp sensor */
47 volatile char tstate = 0;
48 volatile char tlock = 0;
49 unsigned long tstart;
50 unsigned long ttime;
51 unsigned long ttimea = 10000;
52 char tavgok = 0;
53 /* Conversion loop */
54 int tempk;
55 volatile ktok = 0;
56 /* Zero-cross detector*/
57 volatile char zok = 0;
58 volatile char ztime = 0;
59 /* Triac */
60 char trstate = 0;
61 char tron = 0;
62 volatile char trtime;
63 volatile char trdelay = 0;
64
65 void init(void)
66 {
67     /* Timer init
68      * Timer 0 cycles the Triac
69      * Timer 1 is used for global timing
70      * Timer 2 cycles the LED display
71      */
72     OCR0A = 100;
73     TCCR0A = 2;
74     TCCR0B = 1;
75     TIMSK0 = 2;
76     TCCR1A = 0;
77     TCCR1B = 1;
78     TIMSK1 = 1;
79     OCR2A = 16;
80     TCCR2A = 2;
81     TCCR2B = 4;
82     TIMSK2 = 2;
83     
84     /*
85      * B0..2 = Pulse sensor
86      * B3..5 = ISP
87      * B6..7 = CLK
88      */
89     DDRB = 0x38;
90     PORTB = 0x07;
91     PCMSK0 = 0x07;
92     PCICR = 0x01;
93     /*
94      * C0..5 = LEDA0..5
95      * C6 = /RESET
96      * C7 = NC
97      */
98     DDRC = 0x3f;
99     PORTC = 0x00;
100     /*
101      * D0 = Triac
102      * D1 = NTC FET
103      * D2 = ZCD (INT0)
104      * D3 = NTC Op-amp (INT1)
105      * D4..5 = LEDA6..7
106      * D6..7 = LEDC0..1
107      */
108     DDRD = 0xf3;
109     PORTD = 0x00;
110     EICRA = 0x0d;
111     EIMSK = 0x03;
112 }
113
114 unsigned char bindisp(unsigned char num)
115 {
116     unsigned char ret;
117     
118     ret = 0;
119     if(num & 1)
120         ret |= SEGA;
121     if(num & 2)
122         ret |= SEGB;
123     if(num & 4)
124         ret |= SEGC;
125     if(num & 8)
126         ret |= SEGD;
127     if(num & 16)
128         ret |= SEGE;
129     if(num & 32)
130         ret |= SEGF;
131     if(num & 64)
132         ret |= SEGG;
133     if(num & 128)
134         ret |= SEGP;
135     return(ret);
136 }
137
138 void display(char num)
139 {
140     dsp[0] = font[(num / 10) % 10];
141     dsp[1] = font[num % 10];
142 }
143
144 void disphex(unsigned char num)
145 {
146     dsp[0] = font[(num & 0xf0) >> 4];
147     dsp[1] = font[num & 0x0f];
148 }
149
150 unsigned long getticks(void)
151 {
152     return(TCNT1 + (((unsigned long)oticks) << 16));
153 }
154
155 void ledcycle(void)
156 {
157     uint8_t c, d, v;
158     
159     if(++leda >= 8) {
160         leda = 0;
161         if(++ledc >= 2)
162             ledc = 0;
163     }
164     if(dsp[ledc] & (1 << leda)) {
165         if(leda < 6) {
166             c = 1 << leda;
167             d = 0;
168         } else {
169             c = 0;
170             d = 0x10 << (leda - 6);
171         }
172         d |= ledc?0x40:0x80;
173     } else {
174         c = d = 0;
175     }
176     PORTC = c;
177     PORTD = (PORTD & 0x0f) | d;
178 }
179
180 void tempcycle(void)
181 {
182     if(tstate == 0) {
183         if((PIND & 8) && (tlock == 0)) {
184             cli();
185             PORTD |= 2;
186             sei();
187             tstart = mnow;
188             tstate = 1;
189         }
190     } else if(tstate == 1) {
191         if(mnow - tstart > 1000) {
192             cli();
193             PORTD &= ~2;
194             sei();
195             tstate = 0;
196             tstart = mnow;
197         }
198     }
199 }
200
201 void calcavg(void)
202 {
203     if(tlock == 1) {
204         tlock = 2;
205         ttimea = ((ttimea * 15) + ttime) >> 4;
206         tlock = 0;
207         tavgok = 1;
208     }
209 }
210
211 void convcycle(void)
212 {
213     static char state = 0;
214     static unsigned long last = 0;
215     static float a, ra, l, t;
216     
217     /*
218      * Theoretically:
219      *  t = RC * ln(2) => R = t / (C * ln(2))
220      *  R = A * exp(B / T) => T = B / ln(R / A)
221      *  T = B / ln(R / (A * C * ln(2)))
222      * In the following: 
223      *  a = ttimea as float
224      *  C = 1e6 / (A * C * ln(2))
225      *  ra = a * C
226      *  l = ln(ra)
227      *  t = B / l
228      * Note, temperature is in Kelvin
229      */
230 #define C 9.792934
231 #define B 4020.0
232     if(state == 0) {
233         if((mnow - last > 200000) && tavgok) {
234             a = (float)ttimea;
235             state = 1;
236             tavgok = 0;
237             last = mnow;
238         }
239     } else if(state == 1) {
240         ra = a * C;
241         state = 2;
242     } else if(state == 2) {
243         l = log(ra);
244         state = 3;
245     } else if(state == 3) {
246         t = B / l;
247         state = 4;
248     } else if(state == 4) {
249         tempk = (int)t;
250         ktok = 1;
251         state = 0;
252     }
253 }
254
255 int main(void)
256 {
257     int state, cur;
258     unsigned long utime;
259     
260     state = 0;
261     cur = 99;
262     init();
263     sei();
264     display(0);
265
266     while(1) {
267         mnow = getticks();
268         tempcycle();
269         calcavg();
270         convcycle();
271
272 #if 1
273         /*
274          * User interface
275          */
276         if(state == 0) {
277             /* Display temperature */
278             if(ktok) {
279                 ktok = 0;
280                 if((tempk >= 273) && (tempk <= 372)) {
281                     display(tempk - 273);
282                 } else {
283                     dsp[0] = dsp[1] = SEGG;
284                 }
285             }
286             if(pval != 0) {
287                 state = 1;
288                 utime = mnow;
289             }
290             if(sstate == 2) {
291                 sstate = 0;
292                 if(stime > 10) {
293                     state = 2;
294                 } else {
295                     tron = !tron;
296                 }
297             }
298         } else if(state == 1) {
299             /* Triac control */
300             if(pval != 0) {
301                 cur += pval;
302                 pval = 0;
303                 if(cur < 0)
304                     cur = 0;
305                 if(cur > 99)
306                     cur = 99;
307                 display(cur);
308                 trdelay = 99 - cur;
309                 utime = mnow;
310             }
311             if(mnow - utime > 1000000) {
312                 state = 0;
313             }
314             if(sstate == 2) {
315                 tron = !tron;
316                 sstate = 0;
317             }
318         } else if(state == 2) {
319             if(ttimea < 20000) {
320                 display((ttimea / 100) % 100);
321                 dsp[0] |= SEGP;
322                 if(ttimea >= 10000)
323                     dsp[1] |= SEGP;
324             } else {
325                 display(ttimea / 1000);
326             }
327             if(sstate == 2) {
328                 state = 0;
329                 sstate = 0;
330             }
331         }
332 #endif
333         /*
334           dsp[0] = bindisp((ttimea & 0xff00) >> 8);
335           dsp[1] = bindisp(ttimea & 0x00ff);
336         */
337         /*
338           disphex((ttimea & 0xff000) >> 12);
339         */
340 #if 0
341         /*
342           Temp display
343         */
344         if(ttimea < 20000) {
345             display((ttimea / 100) % 100);
346             dsp[0] |= SEGP;
347             if(ttimea >= 10000)
348                 dsp[1] |= SEGP;
349         } else {
350             display(ttimea / 1000);
351         }
352 #endif
353 #if 0
354         /*
355          * ZVD debug
356          */
357         if(zok) {
358             if(++cur > 99)
359                 cur = 0;
360             display(cur);
361             zok = 0;
362         }
363 #endif
364 #if 0
365         /*
366           Phony Triac control
367          */
368         if(pval != 0) {
369             cur += pval;
370             if(cur < 0)
371                 cur = 0;
372             if(cur > 99)
373                 cur = 99;
374             display(cur);
375             trdelay = 99 - cur;
376             pval = 0;
377         }
378         if(sstate == 2) {
379             tron = !tron;
380             sstate = 0;
381         }
382         if(tron)
383             dsp[1] |= SEGP;
384         else
385             dsp[1] &= ~SEGP;
386 #endif
387 #if 0
388         /*
389           Pulse counter display
390         */
391         cur += pval;
392         pval = 0;
393         if(sstate == 2) {
394             cur = stime;
395             sstate = 0;
396         }
397         if(cur > 99)
398             cur = 99;
399         if(cur < -99)
400             cur = -99;
401         if(cur < 0) {
402             display(-cur);
403             dsp[0] |= SEGP;
404         } else {
405             display(cur);
406         }
407         if(PINB & 4)
408             dsp[1] |= SEGP;
409 #endif
410     }
411 }
412
413 ISR(SIG_INTERRUPT0)
414 {
415     ztime = 0;
416     zok = 1;
417 }
418
419 ISR(SIG_INTERRUPT1)
420 {
421     unsigned long now;
422     
423     now = getticks();
424     if(tstate == 0) {
425         tstate = 1;
426         if(tlock != 2)
427             ttime = now - tstart;
428         tstart = now;
429         PORTD |= 2;
430         tlock = 1;
431     }
432 }
433
434 ISR(SIG_OUTPUT_COMPARE0A)
435 {
436     if(trstate == 0) {
437         ztime++;
438         if(tron && (ztime >= trdelay)) {
439             PORTD |= 1;
440             trstate = 1;
441             trtime = 0;
442         }
443     } else if(trstate == 1) {
444         trtime++;
445         if(trtime >= 5) {
446             PORTD &= ~1;
447             trstate = 0;
448         }
449     }
450 }
451
452 ISR(SIG_OUTPUT_COMPARE2A)
453 {
454     ledcycle();
455 }
456
457 ISR(SIG_OVERFLOW1)
458 {
459     oticks++;
460 }
461
462 ISR(SIG_PIN_CHANGE0)
463 {
464     if((sstate == 0) && !(PINB & 4)) {
465         stime = oticks;
466         sstate = 1;
467     }
468     if((sstate == 1) && (PINB & 4)) {
469         stime = oticks - stime;
470         sstate = 2;
471     }
472     if(pstate == 0) {
473         if((PINB & 2) == 0) {
474             pstate = 1;
475         } else if((PINB & 1) == 0) {
476             pstate = 2;
477         }
478     } else if(pstate == 1) {
479         if((PINB & 1) == 0) {
480             pval++;
481             pstate = 3;
482         } else {
483             pstate = 0;
484         }
485     } else if(pstate == 2) {
486         if((PINB & 2) == 0) {
487             pval--;
488             pstate = 3;
489         } else {
490             pstate = 0;
491         }
492     } else {
493         if((PINB & 2) && (PINB & 1))
494             pstate = 0;
495     }
496 }