Mercurial > ~darius > hgwebdir.cgi > avr
comparison tempctrl.c @ 46:fb272cb82bcb
Add watchdog reset to timer IRQ.
Swap heat and cool bits by default.
Init to INT32_MIN so we don't get odd behaviour at startup.
Print out mode in tc list.
Silence compiler warning regarding PROGMEM pointers.
author | darius@Inchoate |
---|---|
date | Mon, 20 Oct 2008 22:21:21 +1030 |
parents | f219538d6ea7 |
children | 91e06007fe23 |
comparison
equal
deleted
inserted
replaced
45:efd44dc40934 | 46:fb272cb82bcb |
---|---|
30 #include <stdint.h> | 30 #include <stdint.h> |
31 #include <stdlib.h> | 31 #include <stdlib.h> |
32 #include <avr/interrupt.h> | 32 #include <avr/interrupt.h> |
33 #include <avr/pgmspace.h> | 33 #include <avr/pgmspace.h> |
34 #include <avr/eeprom.h> | 34 #include <avr/eeprom.h> |
35 #include <avr/wdt.h> | |
35 #include <util/crc16.h> | 36 #include <util/crc16.h> |
36 | 37 |
37 #include "cons.h" | 38 #include "cons.h" |
38 #include "1wire.h" | 39 #include "1wire.h" |
39 #include "tempctrl.h" | 40 #include "tempctrl.h" |
101 .mincoolontime = 300, | 102 .mincoolontime = 300, |
102 .mincoolofftime = 600, | 103 .mincoolofftime = 600, |
103 .minheatontime = 60, | 104 .minheatontime = 60, |
104 .minheatofftime = 60, | 105 .minheatofftime = 60, |
105 .mode = TC_MODE_AUTO, | 106 .mode = TC_MODE_AUTO, |
106 .coolbits = _BV(7), | 107 .coolbits = _BV(6), |
107 .heatbits = _BV(6), | 108 .heatbits = _BV(7), |
108 .idlebits = 0x00, | 109 .idlebits = 0x00, |
109 .check_interval = 10, | 110 .check_interval = 10, |
110 .stale_factor = 3 | 111 .stale_factor = 3 |
111 }; | 112 }; |
112 | 113 |
113 /* Local variable declarations */ | 114 /* Local variable declarations */ |
114 volatile static time_t now; | 115 volatile static time_t now; |
115 | 116 |
116 /* Local function prototypes */ | 117 /* Local function prototypes */ |
117 static int gettemp(const PROGMEM char *name, uint8_t *ROM, int16_t *temp, uint8_t last); | 118 static int gettemp(const char *name, uint8_t *ROM, int16_t *temp, uint8_t last); |
118 static void tempctrl_load_or_init_settings(void); | 119 static void tempctrl_load_or_init_settings(void); |
119 static void tempctrl_default_settings(void); | 120 static void tempctrl_default_settings(void); |
120 static void tempctrl_write_settings(void); | 121 static void tempctrl_write_settings(void); |
121 static void setstate(char state); | 122 static void setstate(char state); |
122 static const PROGMEM char*state2long(char s); | 123 static const PROGMEM char*state2long(char s); |
152 * | 153 * |
153 * Update time counter | 154 * Update time counter |
154 */ | 155 */ |
155 | 156 |
156 ISR(TIMER0_COMP_vect) { | 157 ISR(TIMER0_COMP_vect) { |
158 wdt_reset(); | |
159 | |
157 now.usec += 8000; /* 1000000 * 1 / F_CPU / 1024 / 125 */ | 160 now.usec += 8000; /* 1000000 * 1 / F_CPU / 1024 / 125 */ |
158 while (now.usec > 1000000) { | 161 while (now.usec > 1000000) { |
159 now.usec -= 1000000; | 162 now.usec -= 1000000; |
160 now.sec++; | 163 now.sec++; |
161 } | 164 } |
170 */ | 173 */ |
171 void | 174 void |
172 tempctrl_update(void) { | 175 tempctrl_update(void) { |
173 /* State variables */ | 176 /* State variables */ |
174 static int32_t checktime = 0; // Time of next check | 177 static int32_t checktime = 0; // Time of next check |
175 static int32_t lastdata = -100000; // Last time we got data | 178 static int32_t lastdata = INT32_MIN; // Last time we got data |
176 | 179 |
177 static int16_t fermenter_temp = 0; // Fermenter temperature | 180 static int16_t fermenter_temp = 0; // Fermenter temperature |
178 static int16_t fridge_temp = 0; // Fridge temperature | 181 static int16_t fridge_temp = 0; // Fridge temperature |
179 static int16_t ambient_temp = 0; // Ambient temperature | 182 static int16_t ambient_temp = 0; // Ambient temperature |
180 // These are inited like this so we will still heat/cool when | 183 static int32_t lastheaton = INT32_MIN; // Last time the heater was on |
181 // now < settings.minheatofftime | 184 static int32_t lastheatoff = INT32_MIN;// Last time the heater was off |
182 static int32_t lastheaton = -100000; // Last time the heater was on | 185 static int32_t lastcoolon = INT32_MIN; // Last time the cooler was on |
183 static int32_t lastheatoff = -100000; // Last time the heater was off | 186 static int32_t lastcooloff = INT32_MIN;// Last time the cooler was off |
184 static int32_t lastcoolon = -100000; // Last time the cooler was on | |
185 static int32_t lastcooloff = -100000; // Last time the cooler was off | |
186 static char currstate = 'i'; // Current state | 187 static char currstate = 'i'; // Current state |
188 /* We init to times to INT32_MIN so that things function properly when | |
189 * now < settings.minheat/cool/on/offtime */ | |
187 | 190 |
188 /* Temporary variables */ | 191 /* Temporary variables */ |
189 int32_t t; | 192 int32_t t; |
190 int16_t diff; | 193 int16_t diff; |
191 char nextstate; | 194 char nextstate; |
312 * Log a temperature & store it if valid | 315 * Log a temperature & store it if valid |
313 * | 316 * |
314 * Returns 1 if it was valid, 0 otherwise | 317 * Returns 1 if it was valid, 0 otherwise |
315 */ | 318 */ |
316 static int | 319 static int |
317 gettemp(const PROGMEM char *name, uint8_t *ROM, int16_t *temp, uint8_t last) { | 320 gettemp(const char *name, uint8_t *ROM, int16_t *temp, uint8_t last) { |
318 int16_t tmp; | 321 int16_t tmp; |
319 | 322 |
320 tmp = OWGetTemp(ROM); | 323 tmp = OWGetTemp(ROM); |
321 printf_P(PSTR("%S: "), name); | 324 printf_P(PSTR("%S: "), name); |
322 if (tmp > OW_TEMP_BADVAL) { | 325 if (tmp > OW_TEMP_BADVAL) { |
470 } | 473 } |
471 if (!strcasecmp_P(cmd, PSTR("list"))) { | 474 if (!strcasecmp_P(cmd, PSTR("list"))) { |
472 printf_P(PSTR("Fermenter ROM ID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n" | 475 printf_P(PSTR("Fermenter ROM ID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n" |
473 "Fridge ROM ID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n" | 476 "Fridge ROM ID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n" |
474 "Ambient ROM ID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n" | 477 "Ambient ROM ID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n" |
475 "Target - %d, Hystersis - %d\r\n" | 478 "Mode - %c, Target - %d, Hystersis - %d\r\n" |
476 "Min heat overshoot - %d, Min cool overshoot - %d\r\n" | 479 "Min heat overshoot - %d, Min cool overshoot - %d\r\n" |
477 "Min cool on time - %d, Min cool off time - %d\r\n" | 480 "Min cool on time - %d, Min cool off time - %d\r\n" |
478 "Min heat on time - %d, Min heat off time - %d\r\n"), | 481 "Min heat on time - %d, Min heat off time - %d\r\n"), |
479 settings.fermenter_ROM[0], settings.fermenter_ROM[1], settings.fermenter_ROM[2], settings.fermenter_ROM[3], | 482 settings.fermenter_ROM[0], settings.fermenter_ROM[1], settings.fermenter_ROM[2], settings.fermenter_ROM[3], |
480 settings.fermenter_ROM[4], settings.fermenter_ROM[5], settings.fermenter_ROM[6], settings.fermenter_ROM[7], | 483 settings.fermenter_ROM[4], settings.fermenter_ROM[5], settings.fermenter_ROM[6], settings.fermenter_ROM[7], |
481 settings.fridge_ROM[0], settings.fridge_ROM[1], settings.fridge_ROM[2], settings.fridge_ROM[3], | 484 settings.fridge_ROM[0], settings.fridge_ROM[1], settings.fridge_ROM[2], settings.fridge_ROM[3], |
482 settings.fridge_ROM[4], settings.fridge_ROM[5], settings.fridge_ROM[6], settings.fridge_ROM[7], | 485 settings.fridge_ROM[4], settings.fridge_ROM[5], settings.fridge_ROM[6], settings.fridge_ROM[7], |
483 settings.ambient_ROM[0], settings.ambient_ROM[1], settings.ambient_ROM[2], settings.ambient_ROM[3], | 486 settings.ambient_ROM[0], settings.ambient_ROM[1], settings.ambient_ROM[2], settings.ambient_ROM[3], |
484 settings.ambient_ROM[4], settings.ambient_ROM[5], settings.ambient_ROM[6], settings.ambient_ROM[7], | 487 settings.ambient_ROM[4], settings.ambient_ROM[5], settings.ambient_ROM[6], settings.ambient_ROM[7], |
485 settings.target_temp, settings.hysteresis, | 488 settings.mode, settings.target_temp, settings.hysteresis, |
486 settings.minheatovershoot, settings.mincoolovershoot, | 489 settings.minheatovershoot, settings.mincoolovershoot, |
487 settings.mincoolontime, settings.minheatontime, | 490 settings.mincoolontime, settings.minheatontime, |
488 settings.minheatontime, settings.minheatofftime); | 491 settings.minheatontime, settings.minheatofftime); |
489 return; | 492 return; |
490 } | 493 } |