forked from matsstaff/stc1000p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage0.c
521 lines (437 loc) · 14.2 KB
/
page0.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
* STC1000+, improved firmware and Arduino based firmware uploader for the STC-1000 dual stage thermostat.
*
* Copyright 2014 Mats Staffansson
*
* This file is part of STC1000+.
*
* STC1000+ is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* STC1000+ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with STC1000+. If not, see <http://www.gnu.org/licenses/>.
*
*
* Schematic of the connections to the MCU.
*
* PIC16F1828
* ------------
* VDD | 1 20 | VSS
* Relay Heat RA5 | 2 19 | RA0/ICSPDAT (Programming header), Piezo buzzer
* Relay Cool RA4 | 3 18 | RA1/ICSPCLK (Programming header)
* (Programming header) nMCLR/VPP/RA3 | 4 17 | RA2/AN2 Thermistor
* LED 5 RC5 | 5 16 | RC0 LED 0
* LED 4, BTN 4 RC4 | 6 15 | RC1 LED 1
* LED 3, BTN 3 RC3 | 7 14 | RC2 LED 2
* LED 6, BTN 2 RC6 | 8 13 | RB4 LED Common Anode 10's digit
* LED 7, BTN 1 RC7 | 9 12 | RB5 LED Common Anode 1's digit
* LED Common Anode extras RB7 | 10 11 | RB6 LED Common Anode 0.1's digit
* ------------
*
*
* Schematic of the bit numbers for the display LED's. Useful if custom characters are needed.
*
* * 7 -------- * -------- * C
* / 7 / 1 / 7 / 5 2
* 2 / / 6 2 / / 6 ----
* ------- ------- 2 / 7 / 6
* * / 1 / / 1 / ---
* 3 5 / / 3 5 / / 3 5 / 1 / 3
* -------- * -------- * ---- *
* 4 0 4 0 4 0
*
*
*
*
*/
#define __16f1828
#include "pic14/pic16f1828.h"
#include "stc1000p.h"
/* Defines */
#define ClrWdt() { __asm CLRWDT __endasm; }
/* Configuration words */
unsigned int __at _CONFIG1 __CONFIG1 = 0xFD4;
unsigned int __at _CONFIG2 __CONFIG2 = 0x3AFF;
/* Temperature lookup table */
#ifdef FAHRENHEIT
const int ad_lookup[32] = { 0, -555, -319, -167, -49, 48, 134, 211, 282, 348, 412, 474, 534, 593, 652, 711, 770, 831, 893, 957, 1025, 1096, 1172, 1253, 1343, 1444, 1559, 1694, 1860, 2078, 2397, 2987 };
#else // CELSIUS
const int ad_lookup[32] = { 0, -486, -355, -270, -205, -151, -104, -61, -21, 16, 51, 85, 119, 152, 184, 217, 250, 284, 318, 354, 391, 431, 473, 519, 569, 624, 688, 763, 856, 977, 1154, 1482 };
#endif
/* LED character lookup table (0-15), includes hex */
unsigned const char led_lookup[16] = { 0x3, 0xb7, 0xd, 0x25, 0xb1, 0x61, 0x41, 0x37, 0x1, 0x21, 0x5, 0xc1, 0xcd, 0x85, 0x9, 0x59 };
/* Global variables to hold LED data (for multiplexing purposes) */
_led_e_bits led_e = {0xff};
unsigned char led_10, led_1, led_01;
static int temperature=0;
/* Functions.
* Note: Functions used from other page cannot be static, but functions
* not used from other page SHOULD be static to decrease overhead.
* Functions SHOULD be defined before used (ie. not just declared), to
* decrease overhead. Refer to SDCC manual for more info.
*/
/* Read one configuration data from specified address.
* arguments: Config address (0-127)
* return: the read data
*/
unsigned int eeprom_read_config(unsigned char eeprom_address){
unsigned int data = 0;
eeprom_address = (eeprom_address << 1);
do {
EEADRL = eeprom_address; // Data Memory Address to read
CFGS = 0; // Deselect config space
EEPGD = 0; // Point to DATA memory
RD = 1; // Enable read
data = ((((unsigned int) EEDATL) << 8) | (data >> 8));
} while(!(eeprom_address++ & 0x1));
return data; // Return data
}
/* Store one configuration data to the specified address.
* arguments: Config address (0-127), data
* return: nothing
*/
void eeprom_write_config(unsigned char eeprom_address,unsigned int data)
{
// Avoid unnecessary EEPROM writes
if(data == eeprom_read_config(eeprom_address)){
return;
}
// multiply address by 2 to get eeprom address, as we will be storing 2 bytes.
eeprom_address = (eeprom_address << 1);
do {
// Address to write
EEADRL = eeprom_address;
// Data to write
EEDATL = (unsigned char) data;
// Deselect configuration space
CFGS = 0;
//Point to DATA memory
EEPGD = 0;
// Enable write
WREN = 1;
// Disable interrupts during write
GIE = 0;
// Write magic words to EECON2
EECON2 = 0x55;
EECON2 = 0xAA;
// Initiate a write cycle
WR = 1;
// Re-enable interrupts
GIE = 1;
// Disable writes
WREN = 0;
// Wait for write to complete
while(WR);
// Clear write complete flag (not really needed
// as we use WR for check, but is nice)
EEIF=0;
// Shift data for next pass
data = data >> 8;
} while(!(eeprom_address++ & 0x01)); // Run twice for 16 bits
}
/* Update LED globals with temperature or integer data.
* arguments: value (actual temperature multiplied by 10 or an integer)
* decimal indicates if the value is multiplied by 10 (i.e. a temperature)
* return: nothing
*/
void value_to_led(int value, unsigned char decimal) {
unsigned char i;
// Handle negative values
if (value < 0) {
led_e.e_negative = 0;
value = -value;
} else {
led_e.e_negative = 1;
}
// This assumes that only temperatures and all temperatures are decimal
if(decimal){
led_e.e_deg = 0;
#ifdef FAHRENHEIT
led_e.e_c = 1;
#else
led_e.e_c = 0;
#endif // FAHRENHEIT
}
// If temperature > 100 we must lose decimal...
if (value >= 1000) {
value = ((unsigned int) value) / 10;
decimal = 0;
}
// Convert value to BCD and set LED outputs
if(value >= 100){
for(i=0; value >= 100; i++){
value -= 100;
}
led_10 = led_lookup[i & 0xf];
} else {
led_10 = 0xff; // Turn off led if zero (lose leading zeros)
}
if(value >= 10 || decimal || led_10!=0xff){ // If decimal, we want 1 leading zero
for(i=0; value >= 10; i++){
value -= 10;
}
led_1 = led_lookup[i] & (decimal ? 0xfe : 0xff);
} else {
led_1 = 0xff; // Turn off led if zero (lose leading zeros)
}
led_01 = led_lookup[value];
}
static void update_profile(){
unsigned char profile_no;
profile_no = eeprom_read_config(EEADR_RUN_MODE);
if (profile_no < 6) { // Running profile
unsigned char curr_step;
unsigned int curr_dur;
// Load step and duration
curr_step = eeprom_read_config(EEADR_CURRENT_STEP);
curr_step = curr_step > 8 ? 8 : curr_step; // sanity check
curr_dur = eeprom_read_config(EEADR_CURRENT_STEP_DURATION);
curr_dur++; // TODO Maybe increment conditionally to be able to call this function outside of on one hour marks?
if (curr_dur >= eeprom_read_config(EEADR_PROFILE_DURATION(profile_no, curr_step))) {
curr_step++;
curr_dur = 0;
// Is this the last step? Update settings and switch to thermostat mode.
if (curr_step == 9 || eeprom_read_config(EEADR_PROFILE_DURATION(profile_no, curr_step)) == 0) {
eeprom_write_config(EEADR_SETPOINT, eeprom_read_config(EEADR_PROFILE_SETPOINT(profile_no, curr_step)));
eeprom_write_config(EEADR_RUN_MODE, 6);
return; // Fastest way out...
}
eeprom_write_config(EEADR_CURRENT_STEP, curr_step);
eeprom_write_config(EEADR_SETPOINT, eeprom_read_config(EEADR_PROFILE_SETPOINT(profile_no, curr_step)));
} else if(eeprom_read_config(EEADR_RAMPING)) {
unsigned int step_dur = eeprom_read_config(EEADR_PROFILE_DURATION(profile_no, curr_step));
int sp1 = eeprom_read_config(EEADR_PROFILE_SETPOINT(profile_no, curr_step));
int sp2 = eeprom_read_config(EEADR_PROFILE_SETPOINT(profile_no, curr_step + 1));
unsigned int t = curr_dur << 6;
long sp = 32;
unsigned char i;
for (i = 0; i < 64; i++) {
if (t >= step_dur) {
t -= step_dur;
sp += sp2;
} else {
sp += sp1;
}
}
sp >>= 6;
eeprom_write_config(EEADR_SETPOINT, sp);
}
eeprom_write_config(EEADR_CURRENT_STEP_DURATION, curr_dur);
}
}
/* Due to a fault in SDCC, static local variables are not initialized
* properly, so the variables below were moved from temperature_control()
* and made global.
*/
static unsigned int cooling_delay = 60; // Initial cooling delay
static unsigned int heating_delay = 60; // Initial heating delay
static void temperature_control(){
int setpoint;
setpoint = eeprom_read_config(EEADR_SETPOINT);
if(cooling_delay){
cooling_delay--;
}
if(heating_delay){
heating_delay--;
}
// Set LED outputs
led_e.e_cool = !LATA4;
led_e.e_heat = !LATA5;
// This is the thermostat logic
if((LATA4 && temperature <= setpoint) || (LATA5 && temperature >= setpoint)){
cooling_delay = ((unsigned char)eeprom_read_config(EEADR_COOLING_DELAY)) * 60;
heating_delay = ((unsigned char)eeprom_read_config(EEADR_HEATING_DELAY)) * 60;
LATA4 = 0;
LATA5 = 0;
}
else if(LATA4 == 0 && LATA5 == 0) {
int hysteresis = eeprom_read_config(EEADR_HYSTERESIS);
if (temperature > setpoint + hysteresis) {
if (cooling_delay) {
led_e.e_cool = led_e.e_cool ^ (cooling_delay & 0x1); // Flash to indicate cooling delay
} else {
LATA4 = 1;
}
} else if (temperature < setpoint - hysteresis) {
if (heating_delay) {
led_e.e_heat = led_e.e_heat ^ (heating_delay & 0x1); // Flash to indicate heating delay
} else {
LATA5 = 1;
}
}
}
}
/* Initialize hardware etc, on startup.
* arguments: none
* returns: nothing
*/
static void init() {
// OSCCON = 0b01100010; // 2MHz
OSCCON = 0b01101010; // 4MHz
// Heat, cool as output, Thermistor as input, piezo output
TRISA = 0b00001110;
LATA = 0; // Drive relays and piezo low
// LED Common anodes
TRISB = 0;
LATB = 0;
// LED data (and buttons) output
TRISC = 0;
// Analog input on thermistor
ANSA2 = 1;
// Select AD channel AN2
CHS1 = 1;
// AD clock FOSC/8 (FOSC = 4MHz)
ADCS0 = 1;
// Right justify AD result
ADFM = 1;
// Enable AD
ADON = 1;
// Start conversion
ADGO = 1;
// IMPORTANT FOR BUTTONS TO WORK!!! Disable analog input -> enables digital input
ANSELC = 0;
// Postscaler 1:1, Enable counter, prescaler 1:4
T2CON = 0b00000101;
// @4MHz, Timer 2 clock is FOSC/4 -> 1MHz prescale 1:4-> 250kHz, 250 gives interrupt every 1 ms
PR2 = 250;
// Enable Timer2 interrupt
TMR2IE = 1;
// Postscaler 1:15, - , prescaler 1:16
T4CON = 0b01110010;
TMR4ON = eeprom_read_config(EEADR_POWER_ON);
// @4MHz, Timer 2 clock is FOSC/4 -> 1MHz prescale 1:16-> 62.5kHz, 250 and postscale 1:15 -> 16.66666 Hz or 60ms
PR4 = 250;
// Postscaler 1:7, Enable counter, prescaler 1:64
T6CON = 0b00110111;
// @4MHz, Timer 2 clock is FOSC/4 -> 1MHz prescale 1:64-> 15.625kHz, 250 and postscale 1:6 -> 8.93Hz or 112ms
PR6 = 250;
// Set PEIE (enable peripheral interrupts, that is for timer2) and GIE (enable global interrupts)
INTCON = 0b11000000;
}
/* Interrupt service routine.
* Receives timer2 interrupts every millisecond.
* Handles multiplexing of the LEDs.
*/
static void interrupt_service_routine(void) __interrupt 0 {
// Check for Timer 2 interrupt
// Kind of excessive when it's the only enabled interrupt
// but is nice as reference if more interrupts should be needed
if (TMR2IF) {
unsigned char latb = (LATB << 1);
if(latb == 0){
latb = 0x10;
}
TRISC = 0; // Ensure LED data pins are outputs
LATB = 0; // Disable LED's while switching
// Multiplex LED's every millisecond
switch(latb) {
case 0x10:
LATC = led_10;
break;
case 0x20:
LATC = led_1;
break;
case 0x40:
LATC = led_01;
break;
case 0x80:
LATC = led_e.led_e;
break;
}
// Enable new LED
LATB = latb;
// Clear interrupt flag
TMR2IF = 0;
}
}
/*
* Main entry point.
*/
void main(void) __naked {
unsigned int millisx60=0;
unsigned int ad_filter;
init();
// Delay for first sample
while(ADGO);
// Initialize 'leaky' integrator
ad_filter = ((ADRESH << 8) | ADRESL) << 6;
//Loop forever
while (1) {
if(TMR6IF) {
// Handle button press and menu
button_menu_fsm();
// Reset timer flag
TMR6IF = 0;
}
if(TMR4IF) {
millisx60++;
// Accumulate and filter A/D values (leaky integrator)
ad_filter = ad_filter - (ad_filter >> 6) + ((ADRESH << 8) | ADRESL);
// Start new conversion
ADGO = 1;
// Alarm on sensor error (AD result out of range)
// LATA0 = (ad_result >= 992 || ad_result < 32);
LATA0 = (ad_filter >= 63488 || ad_filter <= 2047);
// Only run every 16th time called, that is 16x60ms = 960ms
// Close enough to 1s for our purposes.
if((millisx60 & 0xf) == 0) {
{
unsigned char i;
long temp = 32;
// Interpolate between lookup table points
for (i = 0; i < 64; i++) {
if(((ad_filter >> 5) & 0x3f) <= i) {
temp += ad_lookup[((ad_filter >> 11) & 0x1f)];
} else {
temp += ad_lookup[((ad_filter >> 11) & 0x1f) + 1];
}
}
// Divide by 64 to get back to normal temperature
temperature = (temp >> 6);
}
temperature += eeprom_read_config(EEADR_TEMP_CORRECTION);
if(LATA0){ // On alarm, disable outputs
led_10 = 0x11; // A
led_1 = 0xcb; //L
led_e.led_e = led_01 = 0xff;
LATA4 = 0;
LATA5 = 0;
} else {
// Update running profile every hour (if there is one)
// and handle reset of millis x60 counter
if(((unsigned char)eeprom_read_config(EEADR_RUN_MODE)) < 6){
// Indicate profile mode
led_e.e_set = 0;
// Update profile every hour
if(millisx60 >= 60000){
update_profile();
millisx60 = 0;
}
} else {
led_e.e_set = 1;
millisx60 = 0;
}
// Run thermostat
temperature_control();
// Show temperature if menu is idle
if(TMR1GE){
temperature_to_led(temperature);
}
}
// Reset temperature for A/D acc
temperature = 0;
} // End 1 sec section
// Reset timer flag
TMR4IF = 0;
}
// Reset watchdog
ClrWdt();
}
}