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)
152 return(TCNT1 + (((unsigned long)oticks) << 16));
164 if(dsp[ledc] & (1 << leda)) {
170 d = 0x10 << (leda - 6);
177 PORTD = (PORTD & 0x0f) | d;
183 if((PIND & 8) && (tlock == 0)) {
190 } else if(tstate == 1) {
191 if(mnow - tstart > 1000) {
205 ttimea = ((ttimea * 15) + ttime) >> 4;
213 static char state = 0;
214 static unsigned long last = 0;
215 static float a, ra, l, t;
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)))
223 * a = ttimea as float
224 * C = 1e6 / (A * C * ln(2))
228 * Note, temperature is in Kelvin
233 if((mnow - last > 200000) && tavgok) {
239 } else if(state == 1) {
242 } else if(state == 2) {
245 } else if(state == 3) {
248 } else if(state == 4) {
278 /* Display temperature */
281 if((tempk >= 273) && (tempk <= 372)) {
282 display(tempk - 273);
284 dsp[0] = dsp[1] = SEGG;
300 } else if(state == 1) {
312 dsp[0] = dsp[1] = SEGG;
315 if(mnow - utime > 1000000)
321 } else if(state == 2) {
322 /* Display raw temp time reading */
324 display((ttimea / 100) % 100);
329 display(ttimea / 1000);
337 * Set Triac to match temperature
340 if(tempk - 273 < cur) {
342 if(cur - (tempk - 273) > 5) {
343 /* For some reason, the Triac currently doesn't
344 * trigger on one of the AC half-cycles below 0.7
347 } else if(cur - (tempk - 273) >= 3) {
349 } else if(cur - (tempk - 273) >= 2) {
362 dsp[0] = bindisp((ttimea & 0xff00) >> 8);
363 dsp[1] = bindisp(ttimea & 0x00ff);
366 disphex((ttimea & 0xff000) >> 12);
373 display((ttimea / 100) % 100);
378 display(ttimea / 1000);
417 Pulse counter display
455 ttime = now - tstart;
462 ISR(SIG_OUTPUT_COMPARE0A)
466 if(tron && (ztime >= trdelay)) {
471 } else if(trstate == 1) {
480 ISR(SIG_OUTPUT_COMPARE2A)
492 if((sstate == 0) && !(PINB & 4)) {
496 if((sstate == 1) && (PINB & 4)) {
497 stime = oticks - stime;
501 if((PINB & 2) == 0) {
503 } else if((PINB & 1) == 0) {
506 } else if(pstate == 1) {
507 if((PINB & 1) == 0) {
513 } else if(pstate == 2) {
514 if((PINB & 2) == 0) {
521 if((PINB & 2) && (PINB & 1))