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) {
287 /* Display temperature */
290 if((tempk >= 273) && (tempk <= 372)) {
291 display(tempk - 273);
293 dsp[0] = dsp[1] = SEGG;
309 } else if(state == 1) {
321 dsp[0] = dsp[1] = SEGG;
324 if(mnow - utime > 1000000)
330 } else if(state == 2) {
331 /* Display raw temp time reading */
333 display((ttimea / 100) % 100);
338 display(ttimea / 1000);
346 * Set Triac to match temperature
349 if(tempk - 273 < cur) {
351 if(cur - (tempk - 273) > 5) {
352 /* For some reason, the Triac currently doesn't
353 * trigger on one of the AC half-cycles below 0.7
356 } else if(cur - (tempk - 273) >= 3) {
358 } else if(cur - (tempk - 273) >= 2) {
371 dsp[0] = bindisp((ttimea & 0xff00) >> 8);
372 dsp[1] = bindisp(ttimea & 0x00ff);
375 disphex((ttimea & 0xff000) >> 12);
382 display((ttimea / 100) % 100);
387 display(ttimea / 1000);
426 Pulse counter display
464 ttime = now - tstart;
471 ISR(SIG_OUTPUT_COMPARE0A)
475 if(tron && (ztime >= trdelay)) {
480 } else if(trstate == 1) {
489 ISR(SIG_OUTPUT_COMPARE2A)
501 if((sstate == 0) && !(PINB & 4)) {
505 if((sstate == 1) && (PINB & 4)) {
506 stime = oticks - stime;
510 if((PINB & 2) == 0) {
512 } else if((PINB & 1) == 0) {
515 } else if(pstate == 1) {
516 if((PINB & 1) == 0) {
522 } else if(pstate == 2) {
523 if((PINB & 2) == 0) {
530 if((PINB & 2) && (PINB & 1))