Added temperature conversion loop and Celsius display.
[kokare.git] / kokare.c
index 81aab66..12fbf9a 100644 (file)
--- a/kokare.c
+++ b/kokare.c
@@ -1,15 +1,16 @@
 #include <avr/io.h>
 #include <avr/interrupt.h>
 #include <inttypes.h>
+#include <math.h>
 
-#define SEGA 128
-#define SEGB 64
-#define SEGC 4
-#define SEGD 16
-#define SEGE 32
-#define SEGF 2
-#define SEGG 1
-#define SEGP 8
+#define SEGA 4
+#define SEGB 2
+#define SEGC 1
+#define SEGD 32
+#define SEGE 64
+#define SEGF 16
+#define SEGG 8
+#define SEGP 128
 
 uint8_t font[16] = {
     SEGA | SEGB | SEGC | SEGD | SEGE | SEGF,
@@ -30,31 +31,37 @@ uint8_t font[16] = {
     SEGA | SEGE | SEGF | SEGG,
 };
 /* LED */
+#define LCDELAY 1000
 uint8_t dsp[2] = {0, 0};
 char leda = 0;
 char ledc = 0;
 /* Timer */
-char of = 0;
-int oticks = 0;
+volatile char of = 0;
+volatile int oticks = 0;
 unsigned long mnow;
 /* Pulse counter */
-char pstate = 0;
+volatile char pstate = 0;
 char pval = 0;
 /* Switch */
-char sstate = 0;
+volatile char sstate = 0;
 int stime = 0;
 /* Temp sensor */
-char tstate = 0;
-char tlock = 0;
+volatile char tstate = 0;
+volatile char tlock = 0;
 unsigned long tstart;
 unsigned long ttime;
 unsigned long ttimea = 10000;
+char tavgok = 0;
+/* Conversion loop */
+int tempk;
+volatile ktok = 0;
 /* Zero-cross detector*/
-char zok = 0;
+volatile char zok = 0;
 unsigned long ztime;
 /* Triac */
 char trstate = 0;
 char tron = 0;
+unsigned long trtime;
 unsigned short trdelay = 0;
 
 void init(void)
@@ -69,7 +76,7 @@ void init(void)
      * B3..5 = ISP
      * B6..7 = CLK
      */
-    DDRB = 0x30;
+    DDRB = 0x38;
     PORTB = 0x07;
     PCMSK0 = 0x07;
     PCICR = 0x01;
@@ -140,7 +147,7 @@ void ledcycle(void)
     static uint16_t last = 0;
     uint8_t c, d, v;
     
-    if(TCNT1 - last > 500) {
+    if(TCNT1 - last > LCDELAY) {
        last = TCNT1;
        if(++leda >= 8) {
            leda = 0;
@@ -187,6 +194,50 @@ void calcavg(void)
        tlock = 2;
        ttimea = ((ttimea * 15) + ttime) >> 4;
        tlock = 0;
+       tavgok = 1;
+    }
+}
+
+void convcycle(void)
+{
+    static char state = 0;
+    static unsigned long last = 0;
+    static float a, ra, l, t;
+    
+    /*
+     * Theoretically:
+     *  t = RC * ln(2) => R = t / (C * ln(2))
+     *  R = A * exp(B / T) => T = B / ln(R / A)
+     *  T = B / ln(R / (A * C * ln(2)))
+     * In the following: 
+     *  a = ttimea as float
+     *  C = 1e6 / (A * C * ln(2))
+     *  ra = a * C
+     *  l = ln(ra)
+     *  t = B / l
+     * Note, temperature is in Kelvin
+     */
+#define C 9.792934
+#define B 4020.0
+    if(state == 0) {
+       if((mnow - last > 200000) && tavgok) {
+           a = (float)ttimea;
+           state = 1;
+           tavgok = 0;
+       }
+    } else if(state == 1) {
+       ra = a * C;
+       state = 2;
+    } else if(state == 2) {
+       l = log(ra);
+       state = 3;
+    } else if(state == 3) {
+       t = B / l;
+       state = 4;
+    } else if(state == 4) {
+       tempk = (int)t;
+       ktok = 1;
+       state = 0;
     }
 }
 
@@ -197,32 +248,96 @@ void triaccycle(void)
            PORTD |= 1;
            zok = 0;
            trstate = 1;
+           trtime = mnow;
        }
     } else if(trstate == 1) {
-       PORTD &= ~1;
-       trstate = 0;
+       if(mnow > trtime + 500) {
+           PORTD &= ~1;
+           trstate = 0;
+       }
     }
 }
 
 int main(void)
 {
-    int cur;
+    int state, cur;
+    unsigned long utime;
     
-    cur = 0;
+    state = 0;
+    cur = 99;
     init();
     sei();
     display(0);
 
-    tron = 1;
-    trdelay = 5000;
-
     while(1) {
        mnow = getticks();
        ledcycle();
        tempcycle();
        calcavg();
+       convcycle();
        triaccycle();
 
+#if 1
+       /*
+        * User interface
+        */
+       if(state == 0) {
+           /* Display temperature */
+           if(ktok) {
+               ktok = 0;
+               if((tempk >= 273) && (tempk <= 372)) {
+                   display(tempk - 273);
+               } else {
+                   dsp[0] = dsp[1] = SEGG;
+               }
+           }
+           if(pval != 0) {
+               state = 1;
+               utime = mnow;
+           }
+           if(sstate == 2) {
+               sstate = 0;
+               if(stime > 10) {
+                   state = 2;
+               } else {
+                   tron = !tron;
+               }
+           }
+       } else if(state == 1) {
+           /* Triac control */
+           if(pval != 0) {
+               cur += pval;
+               pval = 0;
+               if(cur < 0)
+                   cur = 0;
+               if(cur > 99)
+                   cur = 99;
+               display(cur);
+               trdelay = 10000 - ((unsigned short)cur * 100);
+               utime = mnow;
+           }
+           if(mnow - utime > 1000000) {
+               state = 0;
+           }
+           if(sstate == 2) {
+               tron = !tron;
+               sstate = 0;
+           }
+       } else if(state == 2) {
+           if(ttimea < 20000) {
+               display((ttimea / 100) % 100);
+               dsp[0] |= SEGP;
+               if(ttimea >= 10000)
+                   dsp[1] |= SEGP;
+           } else {
+               display(ttimea / 1000);
+           }
+           if(sstate == 2) {
+               state = 0;
+               sstate = 0;
+           }
+       }
+#endif
        /*
          dsp[0] = bindisp((ttimea & 0xff00) >> 8);
          dsp[1] = bindisp(ttimea & 0x00ff);
@@ -230,7 +345,7 @@ int main(void)
        /*
          disphex((ttimea & 0xff000) >> 12);
        */
-#if 1
+#if 0
        /*
          Temp display
        */
@@ -245,6 +360,40 @@ int main(void)
 #endif
 #if 0
        /*
+        * ZVD debug
+        */
+       if(zok) {
+           if(++cur > 99)
+               cur = 0;
+           display(cur);
+           zok = 0;
+       }
+#endif
+#if 0
+       /*
+         Phony Triac control
+        */
+       if(pval != 0) {
+           cur += pval;
+           if(cur < 0)
+               cur = 0;
+           if(cur > 99)
+               cur = 99;
+           display(cur);
+           trdelay = 10000 - ((unsigned short)cur * 100);
+           pval = 0;
+       }
+       if(sstate == 2) {
+           tron = !tron;
+           sstate = 0;
+       }
+       if(tron)
+           dsp[1] |= SEGP;
+       else
+           dsp[1] &= ~SEGP;
+#endif
+#if 0
+       /*
          Pulse counter display
        */
        cur += pval;
@@ -263,6 +412,8 @@ int main(void)
        } else {
            display(cur);
        }
+       if(PINB & 4)
+           dsp[1] |= SEGP;
 #endif
     }
 }
@@ -296,22 +447,22 @@ ISR(SIG_OVERFLOW1)
 
 ISR(SIG_PIN_CHANGE0)
 {
-    if((sstate == 0) & ((PINB & 1) == 0)) {
+    if((sstate == 0) && !(PINB & 4)) {
        stime = oticks;
        sstate = 1;
     }
-    if((sstate == 1) & ((PINB & 1) == 1)) {
+    if((sstate == 1) && (PINB & 4)) {
        stime = oticks - stime;
        sstate = 2;
     }
     if(pstate == 0) {
        if((PINB & 2) == 0) {
            pstate = 1;
-       } else if((PINB & 4) == 0) {
+       } else if((PINB & 1) == 0) {
            pstate = 2;
        }
     } else if(pstate == 1) {
-       if((PINB & 4) == 0) {
+       if((PINB & 1) == 0) {
            pval++;
            pstate = 3;
        } else {
@@ -325,7 +476,7 @@ ISR(SIG_PIN_CHANGE0)
            pstate = 0;
        }
     } else {
-       if((PINB & 2) && (PINB & 4))
+       if((PINB & 2) && (PINB & 1))
            pstate = 0;
     }
 }