-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
860 lines (826 loc) · 23.7 KB
/
main.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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
/*
* Title : 2 Channel AC Dimming Thermostat and Timer
* Hardware: ATmega328P @ 8 MHz, PCD8544 LCD controller,
* AHT15/AHT20/AHT21/AHT25 or AM2320 temperature and humidity sensor
*
* Created: 06/10/2024 15:21:25
* Author: Tim Dorssers
*
* This is a graphical menu configurable AC dimming thermostat with two output
* channels. Each channel be set to on/off switching. Up to two temperature
* sensors are detected automatically. Each channel is controlled individually
* when using two sensors. Start of daytime and length of day are used to
* determine day and night temperatures.
* The dimming hardware uses zero-cross detection which gives a positive edge
* at the end of a half sine wave and a negative edge at the start of a half
* sine wave on the ICP1 pin. The OC1x pins connect to photo-TRIACs that drive
* the power TRIACs to control the leading edge.
*
* __ + - + - __ + - ICP1 H_ L H L H_ L H L OC1x
* / \| | | |/ \| | | \ | \
* / | | | | | | | \ | \
* +------+------+------+------+ ==+---+==+---+==+---+==+---+
* | | | | | | / | / | /
* | |\__/| | | |\__/ |_/ |_/
*/
#define F_CPU 8000000
#include <avr/io.h>
#include <avr/cpufunc.h>
#include <avr/pgmspace.h>
#include <avr/sfr_defs.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <util/delay.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "pcd8544.h"
#include "am2320.h"
#include "aht20.h"
#include "i2c.h"
char buffer[15];
uint8_t EEMEM nv_magic;
// AC phase control globals
#define DIM_STEPS 50
uint16_t next_ocr1a = 0, next_ocr1b = 0;
uint8_t ch0_dim = 0, ch1_dim = 0;
bool ch0_auto = true, ch1_auto = true;
bool ch0_on_off = false, ch1_on_off = false;
uint8_t EEMEM nv_ch0_dim, nv_ch1_dim;
uint8_t EEMEM nv_ch0_auto, nv_ch1_auto;
uint8_t EEMEM nv_ch0_on_off, nv_ch1_on_off;
// Button polling globals
volatile bool blink = false;
volatile bool button[4];
volatile uint8_t bl_delay = 0;
#define BL_DELAY 30
// Time keeping globals
uint8_t time_sec = 0, time_min = 0, time_hour = 0;
volatile uint8_t sample_delay = 0, on_off_delay = 0;
#define ON_OFF_DELAY 30
uint8_t on_off_thres = 15;
uint8_t EEMEM nv_on_off_thres;
// Sensor globals
uint16_t humidity0, humidity1;
int16_t temperature0, temperature1;
uint8_t sensor0 = 0, sensor1 = 0;
// Setting globals
uint8_t start_min = 0, start_hour = 8, length_min = 0, length_hour = 10;
uint8_t EEMEM nv_start_min, nv_start_hour, nv_length_min, nv_length_hour;
int16_t min_temp = 200, max_temp = 250;
uint16_t EEMEM nv_min_temp, nv_max_temp;
// Menu globals
enum {HOME, SETUP, CHANNEL, KVAL, ETC};
const char str_auto[] PROGMEM = "Auto";
const char str_on_off[] PROGMEM = "On/Off";
const char str_dimming[] PROGMEM = "Dimming";
const char str_buttons[] PROGMEM = "Back Sel Up Dn";
// PID control globals
#define P_ON_M
uint8_t Kp = 90, Ki = 1, Kd = 10, dT = 2;
uint8_t EEMEM nvKp, nvKi, nvKd, nvdT;
// LCD globals
enum {OFF, ON, AUTO};
uint8_t bl_mode = AUTO, contrast = 60;
uint8_t EEMEM nv_bl_mode, nv_contrast;
uint8_t new_ocr0a = 0;
// Function macros
#define button_init() PORTC |= _BV(PC0) | _BV(PC1) | _BV(PC2) | _BV(PC3)
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
// Button polling and LED control
static void timer0_init(void) {
TCCR0A = 0; // Normal operation
TCCR0B = _BV(CS00) | _BV(CS01); // Prescaler /64, TC0 overflows every 2048 us
TCNT0 = 0; // Reset timer
TIMSK0 = _BV(TOIE0); // Timer0 Overflow Interrupt Enable
OCR0A = 255; // Maximum intensity
}
ISR(TIMER0_OVF_vect) {
static uint8_t count = 0, push[4]; // These overflow every 524 ms
static bool hold[4];
if (++count == 0) blink = !blink;
// Fade to new intensity
uint8_t cur_ocr0a = OCR0A;
if (cur_ocr0a > new_ocr0a) cur_ocr0a--;
if (cur_ocr0a < new_ocr0a) cur_ocr0a++;
// Non-inverting fast PWM on OC0A, only if needed
TCCR0A = (cur_ocr0a ? _BV(COM0A1) | _BV(WGM01) | _BV(WGM00) : 0);
OCR0A = cur_ocr0a;
// Button polling
for (uint8_t pin = PC0; pin < PC4; pin++) {
if bit_is_clear(PINC, pin) {
push[pin]++;
if (push[pin] == 0) hold[pin] = true;
} else {
push[pin] = 0;
hold[pin] = false;
}
// Signal button push after 20,5 ms or every 262 ms when pushed longer then 524 ms
if (push[pin] == 10 || (hold[pin] && push[pin] == 138)) {
if (bl_delay || bl_mode != AUTO) button[pin] = true;
bl_delay = BL_DELAY;
}
}
}
// Time keeping
static void timer2_init(void) {
ASSR |= _BV(AS2); // Set Timer/Counter2 clock source to 32,768 kHz crystal
TCNT2 = 0; // Reset timer
TCCR2B = _BV(CS20) | _BV(CS22); // Make TC2 overflow precisely once every second
while (ASSR & (_BV(OCR2AUB) | _BV(TCN2UB) | _BV(TCR2AUB))); // Wait until TC2 is updated
TIMSK2 = _BV(TOIE2); // Enable Timer/Counter2 Overflow Interrupts
sei(); // Set the Global Interrupt Enable Bit
}
ISR(TIMER2_OVF_vect) {
// Time keeping
if (++time_sec > 59) {
time_sec = 0;
if (++time_min > 59) {
time_min = 0;
if (++time_hour > 23) time_hour = 0;
}
}
// Decrement counters
if (bl_delay) bl_delay--;
if (sample_delay) sample_delay--;
if (on_off_delay) on_off_delay--;
}
// Internal R/C oscilator calibration
static void calibrate(void) {
uint8_t cycles = 128;
ASSR |= _BV(AS2);
TCCR2B = _BV(CS20); // Enable TC2 without prescaler
while (ASSR & (_BV(OCR2AUB) | _BV(TCN2UB) | _BV(TCR2AUB)));
do {
// Clear interrupt flags
TIFR1 = TIFR2 = 0xFF;
// Reset timers
TCNT1 = TCNT2 = 0;
while (ASSR & (_BV(OCR2AUB) | _BV(TCN2UB) | _BV(TCR2AUB)));
// Start Timer/Counter1
TCCR1B = _BV(CS10);
// Stop timer on compare match
loop_until_bit_is_set(TIFR2, OCF2A);
TCCR1B = 0;
// Adjust calibration value
if (TCNT1 > (F_CPU / 32768) * 256 + 128 || bit_is_set(TIFR1, TOV1)) {
OSCCAL--;
_NOP();
} else if (TCNT1 < (F_CPU / 32768) * 256 - 128) {
OSCCAL++;
_NOP();
} else
break;
} while(--cycles);
}
// AC phase control
static void timer1_init(void) {
TCCR1A = 0; // Normal operation
// Input capture noise cancel, positive edge, /8 prescaler
TCCR1B = _BV(ICNC1) | _BV(ICES1) | _BV(CS11);
TIFR1 = 0xFF; // Clear interrupt flags
TCNT1 = 0; // Reset timer
// Enable input capture and compare match interrupts
TIMSK1 = _BV(OCIE1A) | _BV(OCIE1B) | _BV(ICIE1);
DDRB |= _BV(PB1) | _BV(PB2); // Set OC1A and OC1B as outputs
}
ISR(TIMER1_CAPT_vect) {
static uint16_t last_icr1 = 0, half_zero = 0, dim_period = 0;
if bit_is_set(TCCR1B, ICES1) { // Positive edge: end of half sine
dim_period = (ICR1 - last_icr1) / DIM_STEPS;
uint16_t crossing = ICR1 + half_zero;
// Determine when the TRIACs are to be triggered
OCR1A = crossing + dim_period * (DIM_STEPS - ch0_dim);
OCR1B = crossing + dim_period * (DIM_STEPS - ch1_dim);
next_ocr1a = dim_period * ch0_dim;
if (ch0_dim == DIM_STEPS) next_ocr1a += half_zero;
next_ocr1b = dim_period * ch1_dim;
if (ch1_dim == DIM_STEPS) next_ocr1b += half_zero;
// Set OC1x on compare match, only if enabled
TCCR1A = (ch0_dim ? _BV(COM1A0) | _BV(COM1A1) : 0) | (ch1_dim ? _BV(COM1B0) | _BV(COM1B1) : 0);
TIFR1 = 0xFF; // Clear interrupt flags
} else { // Negative edge: begin of half sine
half_zero = (ICR1 - last_icr1) / 2;
}
TCCR1B ^= _BV(ICES1); // Toggle edge trigger
last_icr1 = ICR1;
}
ISR(TIMER1_COMPA_vect) {
TCCR1A &= ~_BV(COM1A0); // Clear OC1A on compare match
OCR1A += next_ocr1a;
}
ISR(TIMER1_COMPB_vect) {
TCCR1A &= ~_BV(COM1B0); // Clear OC1B on compare match
OCR1B += next_ocr1b;
}
// Convert (scaled) integer to (zero filled) string
static char *itostr(int16_t num, char *str, uint8_t decimal, uint8_t padding) {
uint8_t i = 0;
uint16_t sum = abs(num);
if (decimal) padding++;
do {
str[i++] = '0' + sum % 10;
if (i == decimal) str[i++] = '.';
} while ((sum /= 10) || i < decimal);
while (i < padding) str[i++] = '0';
if (num < 0) str[i++] = '-';
str[i] = '\0';
return strrev(str);
}
static bool is_daytime(void) {
uint16_t now = time_hour * 60 + time_min;
uint16_t start = start_hour * 60 + start_min;
return now >= start && now < start + length_hour * 60 + length_min;
}
// Home screen
static uint8_t home(void) {
for (uint8_t i = 0; i < 4; i++) {
if (button[i]) {
button[i] = false;
return 4 - i;
}
}
pcd8544_clear();
pcd8544_write_string(itostr(ch0_dim, buffer, 0, 1), 0);
pcd8544_write_char('/', 0);
pcd8544_write_string(itostr(ch1_dim, buffer, 0, 1), 0);
if (is_daytime()) {
pcd8544_set_cursor(42, 0);
pcd8544_write_char('*', 0);
}
pcd8544_set_cursor(54, 0);
itostr(time_hour, buffer, 0, 2);
buffer[2] = time_sec % 2 ? ':' : ' ';
itostr(time_min, &buffer[3], 0, 2);
pcd8544_write_string(buffer, 0);
if (sensor1 != 1) {
pcd8544_set_font(Font6x14B);
pcd8544_set_cursor(0, 8);
pcd8544_write_string(sensor0 ? strcpy_P(buffer, PSTR("--")) : itostr(temperature0, buffer, 1, 2), 0);
pcd8544_write_char('/', 0);
pcd8544_write_string(sensor1 ? strcpy_P(buffer, PSTR("--")) : itostr(temperature1, buffer, 1, 2), 0);
pcd8544_write_string_P("\x7f\x43\n", 0);
pcd8544_write_string(sensor0 ? strcpy_P(buffer, PSTR("--")) : itostr(humidity0, buffer, 1, 2), 0);
pcd8544_write_char('/', 0);
pcd8544_write_string(sensor1 ? strcpy_P(buffer, PSTR("--")) : itostr(humidity1, buffer, 1, 2), 0);
pcd8544_write_char('%', 0);
pcd8544_set_font(Font5x7);
} else {
switch (sensor0) {
case 0:
pcd8544_set_font(Font10x15B);
pcd8544_set_cursor(temperature0 < 0 ? 0 : 12, 8);
pcd8544_write_string(itostr(temperature0, buffer, 1, 2), 0);
pcd8544_write_string_P("\x7f\x43", 0);
pcd8544_set_cursor(12, 24);
pcd8544_write_string(itostr(humidity0, buffer, 1, 2), 0);
pcd8544_write_char('%', 0);
pcd8544_set_font(Font5x7);
break;
case 1:
pcd8544_write_string_P("\nNo response", 0);
break;
case 2:
pcd8544_write_string_P("\nCRC error", 0);
}
}
pcd8544_set_cursor(0, 40);
pcd8544_write_string_P("Set Ch Pid Lcd", 0);
pcd8544_update();
return HOME;
}
static void eeprom_save(void) {
eeprom_update_byte(&nv_magic, 0x55);
eeprom_update_byte(&nv_ch0_auto, ch0_auto);
eeprom_update_byte(&nv_ch0_dim, ch0_dim);
eeprom_update_byte(&nv_ch0_on_off, ch0_on_off);
eeprom_update_byte(&nv_ch1_auto, ch1_auto);
eeprom_update_byte(&nv_ch1_dim, ch1_dim);
eeprom_update_byte(&nv_ch1_on_off, ch1_on_off);
eeprom_update_byte(&nv_start_hour, start_hour);
eeprom_update_byte(&nv_start_min, start_min);
eeprom_update_byte(&nv_length_hour, length_hour);
eeprom_update_byte(&nv_length_min, length_min);
eeprom_update_word(&nv_min_temp, min_temp);
eeprom_update_word(&nv_max_temp, max_temp);
eeprom_update_byte(&nvKp, Kp);
eeprom_update_byte(&nvKi, Ki);
eeprom_update_byte(&nvKd, Kd);
eeprom_update_byte(&nvdT, dT);
eeprom_update_byte(&nv_bl_mode, bl_mode);
eeprom_update_byte(&nv_contrast, contrast);
eeprom_update_byte(&nv_on_off_thres, on_off_thres);
}
static void blink_buffer(void) {
if (blink) memset(buffer, ' ', strlen(buffer));
}
// Setup screen
static uint8_t setup(void) {
static uint8_t item = 1, select = 0, sub = 0;
if (button[3]) { // Back
button[3] = false;
if (select) {
select = 0;
} else {
eeprom_save();
return HOME;
}
}
if (button[2]) { // Select
button[2] = false;
switch (select) {
case 0:
select = item;
sub = 1;
break;
case 1:
if (++sub > 3) select = 0;
break;
case 2:
case 3:
if (++sub > 2) select = 0;
break;
case 4:
case 5:
select = 0;
}
}
if (button[1]) { // Up
button[1] = false;
switch (select) {
case 0:
if (--item == 0) item = 5;
break;
case 1:
if (sub == 1) if (++time_hour > 23) time_hour = 0;
if (sub == 2) if (++time_min > 59) time_min = 0;
if (sub == 3) if (++time_sec > 59) time_sec = 0;
break;
case 2:
if (sub == 1) if (++start_hour > 23) start_hour = 0;
if (sub == 2) if (++start_min > 59) start_min = 0;
break;
case 3:
if (sub == 1) if (++length_hour > 23) length_hour = 0;
if (sub == 2) if (++length_min > 59) length_min = 0;
break;
case 4:
if (min_temp < 800) min_temp += 5;
break;
case 5:
if (min_temp < 800) max_temp += 5;
}
}
if (button[0]) { // Down
button[0] = false;
switch (select) {
case 0:
if (++item > 5) item = 1;
break;
case 1:
if (sub == 1) if (--time_hour > 23) time_hour = 23;
if (sub == 2) if (--time_min > 59) time_min = 59;
if (sub == 3) if (--time_sec > 59) time_sec = 59;
break;
case 2:
if (sub == 1) if (--start_hour > 23) start_hour = 23;
if (sub == 2) if (--start_min > 59) start_min = 59;
break;
case 3:
if (sub == 1) if (--length_hour > 23) length_hour = 23;
if (sub == 2) if (--length_min > 59) length_min = 59;
break;
case 4:
if (min_temp > -400) min_temp -= 5;
break;
case 5:
if (min_temp > -400) max_temp -= 5;
}
}
pcd8544_clear();
bool inv = item == 1;
pcd8544_write_string_P("Time ", inv);
memset(buffer, ' ', 8);
if (!(select == 1 && sub == 1 && blink)) itostr(time_hour, buffer, 0, 2);
buffer[2] = ':';
if (!(select == 1 && sub == 2 && blink)) itostr(time_min, &buffer[3], 0, 2);
buffer[5] = ':';
if (!(select == 1 && sub == 3 && blink)) itostr(time_sec, &buffer[6], 0, 2);
buffer[8] = 0;
pcd8544_write_string(buffer, inv);
inv = item == 2;
pcd8544_write_string_P("\nStart ", inv);
memset(buffer, ' ', 8);
if (!(select == 2 && sub == 1 && blink)) itostr(start_hour, buffer, 0, 2);
buffer[2] = ':';
if (!(select == 2 && sub == 2 && blink)) itostr(start_min, &buffer[3], 0, 2);
buffer[5] = 0;
pcd8544_write_string(buffer, inv);
inv = item == 3;
pcd8544_write_string_P("\nLength ", inv);
memset(buffer, ' ', 5);
if (!(select == 3 && sub == 1 && blink)) itostr(length_hour, buffer, 0, 2);
buffer[2] = ':';
if (!(select == 3 && sub == 2 && blink)) itostr(length_min, &buffer[3], 0, 2);
buffer[5] = 0;
pcd8544_write_string(buffer, inv);
inv = item == 4;
pcd8544_write_string_P("\nMin temp ", inv);
itostr(min_temp, buffer, 1, 2);
if (select == 4) blink_buffer();
pcd8544_write_string(buffer, inv);
inv = item == 5;
pcd8544_set_cursor(0, 32);
pcd8544_write_string_P("Max temp ", inv);
itostr(max_temp, buffer, 1, 2);
if (select == 5) blink_buffer();
pcd8544_write_string(buffer, inv);
pcd8544_set_cursor(0, 40);
pcd8544_write_string_p(str_buttons, 0);
pcd8544_update();
return SETUP;
}
// Channel screen
static uint8_t channel(void) {
static uint8_t item = 1, select = 0;
if (button[3]) { // Back
button[3] = false;
if (select) {
select = 0;
} else {
eeprom_save();
return HOME;
}
}
if (button[2]) { // Select
button[2] = false;
select = select ? 0 : item;
}
if (button[1]) { // Up
button[1] = false;
switch (select) {
case 0:
if (--item == 0) item = 5;
break;
case 1:
if (ch0_auto) {
ch0_auto = false;
ch0_dim = 0;
} else if (ch0_on_off && ch0_dim == 0) {
ch0_dim = DIM_STEPS;
} else if ((ch0_on_off && ch0_dim) || ++ch0_dim > DIM_STEPS) {
ch0_auto = true;
ch0_dim = 0;
}
break;
case 2:
ch0_on_off = !ch0_on_off;
if (ch0_on_off) ch0_dim = ch0_dim ? DIM_STEPS : 0;
break;
case 3:
if (ch1_auto) {
ch1_auto = false;
ch1_dim = 0;
} else if (ch1_on_off && ch1_dim == 0) {
ch1_dim = DIM_STEPS;
} else if ((ch1_on_off && ch1_dim) || ++ch1_dim > DIM_STEPS) {
ch1_auto = true;
ch1_dim = 0;
}
break;
case 4:
ch1_on_off = !ch1_on_off;
if (ch1_on_off) ch1_dim = ch1_dim ? DIM_STEPS : 0;
break;
case 5:
if (++on_off_thres > DIM_STEPS) on_off_thres = 0;
}
}
if (button[0]) { // Down
button[0] = false;
switch (select) {
case 0:
if (++item > 5) item = 1;
break;
case 1:
if (ch0_auto) {
ch0_auto = false;
ch0_dim = DIM_STEPS;
} else if (ch0_on_off && ch0_dim) {
ch0_dim = 0;
} else if ((ch0_on_off && ch0_dim == 0) || ch0_dim-- == 0) {
ch0_auto = true;
ch0_dim = 0;
}
break;
case 2:
ch1_on_off = !ch1_on_off;
if (ch1_on_off) ch1_dim = ch1_dim ? DIM_STEPS : 0;
break;
case 3:
if (ch1_auto) {
ch1_auto = false;
ch1_dim = DIM_STEPS;
} else if (ch1_on_off && ch1_dim) {
ch1_dim = 0;
} else if ((ch1_on_off && ch1_dim == 0) || ch1_dim-- == 0) {
ch1_auto = true;
ch1_dim = 0;
}
break;
case 4:
ch1_on_off = !ch1_on_off;
if (ch1_on_off) ch1_dim = ch1_dim ? DIM_STEPS : 0;
break;
case 5:
if (--on_off_thres > DIM_STEPS) on_off_thres = DIM_STEPS;
}
}
pcd8544_clear();
bool inv = item == 1;
pcd8544_write_string_P("Ch 0 ", inv);
if (ch0_auto)
strcpy_P(buffer, str_auto);
else
itostr(ch0_dim, buffer, 0, 1);
if (select == 1) blink_buffer();
pcd8544_write_string(buffer, inv);
inv = item == 2;
pcd8544_write_string_P("\nCh 0 ", inv);
strcpy_P(buffer, ch0_on_off ? str_on_off : str_dimming);
if (select == 2) blink_buffer();
pcd8544_write_string(buffer, inv);
inv = item == 3;
pcd8544_write_string_P("\nCh 1 ", inv);
if (ch1_auto)
strcpy_P(buffer, str_auto);
else
itostr(ch1_dim, buffer, 0, 1);
if (select == 3) blink_buffer();
pcd8544_write_string(buffer, inv);
inv = item == 4;
pcd8544_write_string_P("\nCh 1 ", inv);
strcpy_P(buffer, ch1_on_off ? str_on_off : str_dimming);
if (select == 4) blink_buffer();
pcd8544_write_string(buffer, inv);
inv = item == 5;
pcd8544_write_string_P("\nThreshold ", inv);
itostr(on_off_thres, buffer, 0, 1);
if (select == 5) blink_buffer();
pcd8544_write_string(buffer, inv);
pcd8544_set_cursor(0, 40);
pcd8544_write_string_p(str_buttons, 0);
pcd8544_update();
return CHANNEL;
}
// PID control K values screen
static uint8_t kval(void) {
static uint8_t item = 1, select = 0;
if (button[3]) { // Back
button[3] = false;
if (select) {
select = 0;
} else {
eeprom_save();
return HOME;
}
}
if (button[2]) { // Select
button[2] = false;
select = select ? 0 : item;
}
if (button[1]) { // Up
button[1] = false;
switch (select) {
case 0:
if (--item == 0) item = 4;
break;
case 1:
Kp++;
break;
case 2:
Ki++;
break;
case 3:
Kd++;
break;
case 4:
if (++dT > 59) dT = 0;
}
}
if (button[0]) { // Down
button[0] = false;
switch (select) {
case 0:
if (++item > 4) item = 1;
break;
case 1:
Kp--;
break;
case 2:
Ki--;
break;
case 3:
Kd--;
break;
case 4:
if (--dT > 59) dT = 59;
}
}
pcd8544_clear();
bool inv = item == 1;
pcd8544_write_string_P("Kp ", inv);
itostr(Kp, buffer, 2, 3);
if (select == 1) blink_buffer();
pcd8544_write_string(buffer, inv);
inv = item == 2;
pcd8544_write_string_P("\nKi ", inv);
itostr(Ki, buffer, 2, 3);
if (select == 2) blink_buffer();
pcd8544_write_string(buffer, inv);
inv = item == 3;
pcd8544_write_string_P("\nKd ", inv);
itostr(Kd, buffer, 2, 3);
if (select == 3) blink_buffer();
pcd8544_write_string(buffer, inv);
inv = item == 4;
pcd8544_write_string_P("\ndT ", inv);
itostr(dT, buffer, 0, 1);
if (select == 4) blink_buffer();
pcd8544_write_string(buffer, inv);
pcd8544_write_char('s', inv);
pcd8544_set_cursor(0, 40);
pcd8544_write_string_p(str_buttons, 0);
pcd8544_update();
return KVAL;
}
// LCD screen
static uint8_t etc(void) {
static uint8_t item = 1, select = 0;
if (button[3]) { // Back
button[3] = false;
if (select) {
select = 0;
} else {
eeprom_save();
return HOME;
}
}
if (button[2]) { // Select
button[2] = false;
select = select ? 0 : item;
}
if (button[1]) { // Up
button[1] = false;
switch (select) {
case 0:
if (--item == 0) item = 2;
break;
case 1:
if (++bl_mode > 2) bl_mode = 0;
break;
case 2:
if (++contrast > 90) contrast = 90;
pcd8544_contrast(contrast);
}
}
if (button[0]) { // Down
button[0] = false;
switch (select) {
case 0:
if (++item > 2) item = 1;
break;
case 1:
if (bl_mode-- == 0) bl_mode = 2;
break;
case 2:
if (--contrast < 30) contrast = 30;
pcd8544_contrast(contrast);
}
}
pcd8544_clear();
bool inv = item == 1;
pcd8544_write_string_P("Backlight ", inv);
if (bl_mode == ON)
strcpy_P(buffer, PSTR("On"));
else if (bl_mode == AUTO)
strcpy_P(buffer, str_auto);
else
strcpy_P(buffer, PSTR("Off"));
if (select == 1) blink_buffer();
pcd8544_write_string(buffer, inv);
inv = item == 2;
pcd8544_set_cursor(0, 8);
pcd8544_write_string_P("Contrast ", inv);
itostr(contrast, buffer, 0, 1);
if (select == 2) blink_buffer();
pcd8544_write_string(buffer, inv);
pcd8544_set_cursor(0, 40);
pcd8544_write_string_p(str_buttons, 0);
pcd8544_update();
return ETC;
}
// PID control
static int16_t pid(int16_t input, int16_t setpoint, int16_t *lastInput, int16_t *outputSum) {
int16_t output = 0;
int16_t error = setpoint - input;
int16_t dInput = input - *lastInput;
*lastInput = input;
*outputSum += Ki * 2 * error;
#ifdef P_ON_M
*outputSum -= Kp * dInput; // Proportional on Measurement
#else
output = Kp * error; // Proportional on Error
#endif
*outputSum = constrain(*outputSum, 0, DIM_STEPS * 100);
output += *outputSum - Kd / 2 * dInput; // Derivative on Measurement
output /= 100;
return constrain(output, 0, DIM_STEPS);
}
static void eeprom_init(void) {
if (eeprom_read_byte(&nv_magic) != 0x55) return;
ch0_dim = eeprom_read_byte(&nv_ch0_dim);
ch1_dim = eeprom_read_byte(&nv_ch1_dim);
ch0_auto = eeprom_read_byte(&nv_ch0_auto);
ch1_auto = eeprom_read_byte(&nv_ch1_auto);
ch0_on_off = eeprom_read_byte(&nv_ch0_on_off);
ch1_on_off = eeprom_read_byte(&nv_ch1_on_off);
start_hour = eeprom_read_byte(&nv_start_hour);
start_min = eeprom_read_byte(&nv_start_min);
length_hour = eeprom_read_byte(&nv_length_hour);
length_min = eeprom_read_byte(&nv_length_min);
min_temp = eeprom_read_word(&nv_min_temp);
max_temp = eeprom_read_word(&nv_max_temp);
Kp = eeprom_read_byte(&nvKp);
Ki = eeprom_read_byte(&nvKi);
Kd = eeprom_read_byte(&nvKd);
dT = eeprom_read_byte(&nvdT);
bl_mode = eeprom_read_byte(&nv_bl_mode);
contrast = eeprom_read_byte(&nv_contrast);
on_off_thres = eeprom_read_byte(&nv_on_off_thres);
}
int main(void) {
uint8_t view = HOME, prev_on_off = 0;
int16_t lastInput0 = 0, lastInput1 = 0;
int16_t outputSum0 = 0, outputSum1 = 0;
i2c_init();
eeprom_init();
pcd8544_init();
pcd8544_set_font(Font5x7);
pcd8544_led_on();
pcd8544_write_string_P("Calibrating", 0);
pcd8544_update();
calibrate();
pcd8544_clear();
pcd8544_update();
button_init();
timer0_init();
timer1_init();
timer2_init();
pcd8544_led_off();
// Main loop
while (1) {
if (view == HOME) view = home();
if (view == SETUP) view = setup();
if (view == CHANNEL) view = channel();
if (view == KVAL) view = kval();
if (view == ETC) view = etc();
new_ocr0a = (bl_mode == ON || (bl_mode == AUTO && bl_delay)) ? 255 : 0;
if (sample_delay == 0) {
sample_delay = dT;
int16_t setpoint = is_daytime() ? max_temp : min_temp;
i2c_select(0);
if ((sensor0 = aht20_get(&humidity0, &temperature0)) == 1) {
sensor0 = am2320_get(&humidity0, &temperature0);
}
uint8_t output = pid(temperature0, setpoint, &lastInput0, &outputSum0);
uint8_t on_off = output > on_off_thres ? DIM_STEPS : 0;
if (on_off != prev_on_off) {
prev_on_off = on_off;
on_off_delay = ON_OFF_DELAY;
}
if (ch0_auto && sensor0 == 0) {
if (ch0_on_off) {
if (on_off_delay == 0) ch0_dim = on_off;
} else {
ch0_dim = output;
}
}
i2c_select(1);
if ((sensor1 = aht20_get(&humidity1, &temperature1)) == 1) {
sensor1 = am2320_get(&humidity1, &temperature1);
}
if (sensor1 == 0) {
output = pid(temperature1, setpoint, &lastInput1, &outputSum1);
on_off = output > on_off_thres ? DIM_STEPS : 0;
}
if (ch1_auto && (sensor0 == 0 || sensor1 == 0)) {
if (ch1_on_off) {
if (on_off_delay == 0) ch1_dim = on_off;
} else {
ch1_dim = output;
}
}
}
}
}