2 #include <avr/interrupt.h>
16 SEGA | SEGB | SEGC | SEGD | SEGE | SEGF,
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,
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,
34 uint8_t dsp[2] = {0, 0};
38 volatile int oticks = 0;
41 volatile char pstate = 0;
44 volatile char sstate = 0;
47 volatile char tstate = 0;
48 volatile char tlock = 0;
51 unsigned long ttimea = 10000;
56 /* Zero-cross detector*/
57 volatile char zok = 0;
58 volatile char ztime = 0;
63 volatile char trdelay = 0;
68 * Timer 0 cycles the Triac
69 * Timer 1 is used for global timing
70 * Timer 2 cycles the LED display
85 * B0..2 = Pulse sensor
104 * D3 = NTC Op-amp (INT1)
114 unsigned char bindisp(unsigned char num)
138 void display(char num)
140 dsp[0] = font[(num / 10) % 10];
141 dsp[1] = font[num % 10];
144 void disphex(unsigned char num)
146 dsp[0] = font[(num & 0xf0) >> 4];
147 dsp[1] = font[num & 0x0f];
150 unsigned long getticks(void)
157 r = v + (((unsigned long)oticks) << 16);
158 if((TIFR1 & 0x01) && !(v & 0x8000))
173 if(dsp[ledc] & (1 << leda)) {
179 d = 0x10 << (leda - 6);
186 PORTD = (PORTD & 0x0f) | d;
192 if((PIND & 8) && (tlock == 0)) {
199 } else if(tstate == 1) {
200 if(mnow - tstart > 1000) {
214 ttimea = ((ttimea * 15) + ttime) >> 4;
222 static char state = 0;
223 static unsigned long last = 0;
224 static float a, ra, l, t;
228 * t = RC * ln(2) => R = t / (C * ln(2))
229 * R = A * exp(B / T) => T = B / ln(R / A)
230 * T = B / ln(R / (A * C * ln(2)))
232 * a = ttimea as float
233 * C = 1e6 / (A * C * ln(2))
237 * Note, temperature is in Kelvin
242 if((mnow - last > 200000) && tavgok) {
248 } else if(state == 1) {
251 } else if(state == 2) {
254 } else if(state == 3) {
257 } else if(state == 4) {
266 int state, cur, run, rstate, delta;
288 /* Display temperature */
291 if((tempk >= 273) && (tempk <= 372)) {
292 display(tempk - 273);
294 dsp[0] = dsp[1] = SEGG;
310 } else if(state == 1) {
322 dsp[0] = dsp[1] = SEGG;
325 if(mnow - utime > 1000000)
331 } else if(state == 2) {
332 /* Display raw temp time reading */
334 display((ttimea / 100) % 100);
339 display(ttimea / 1000);
347 * Set Triac to match temperature
350 delta = cur - (tempk - 273);
355 /* For some reason, the Triac currently doesn't
356 * trigger on one of the AC half-cycles below 0.7
360 trdelay = 79 - (delta * 9);
366 } else if(rstate == 1) {
376 dsp[0] = bindisp((ttimea & 0xff00) >> 8);
377 dsp[1] = bindisp(ttimea & 0x00ff);
380 disphex((ttimea & 0xff000) >> 12);
387 display((ttimea / 100) % 100);
392 display(ttimea / 1000);
431 Pulse counter display
469 ttime = now - tstart;
476 ISR(SIG_OUTPUT_COMPARE0A)
480 if(tron && (ztime >= trdelay)) {
485 } else if(trstate == 1) {
494 ISR(SIG_OUTPUT_COMPARE2A)
506 if((sstate == 0) && !(PINB & 4)) {
510 if((sstate == 1) && (PINB & 4)) {
511 stime = oticks - stime;
515 if((PINB & 2) == 0) {
517 } else if((PINB & 1) == 0) {
520 } else if(pstate == 1) {
521 if((PINB & 1) == 0) {
527 } else if(pstate == 2) {
528 if((PINB & 2) == 0) {
535 if((PINB & 2) && (PINB & 1))