-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregulator.c
741 lines (631 loc) · 12.8 KB
/
regulator.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
/*
Compile:
avr-gcc -mmcu=atmega8 -Os regulator.c -o regulator.o
avr-objcopy -j .text -j .data -O ihex regulator.o regulator.hex
Upload:
sudo uisp -dprog=dapa -dlpt=0x378 --erase
sudo uisp -dprog=dapa -dlpt=0x378 --upload if=regulator.hex
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#define F_CPU 16000000UL
#include <util/delay.h>
/* Temperature value from sensor in dC' (i.e. 20'C == 200) */
uint16_t temp_val = 0;
/* Temperature average in cC' (i.e. 20'C == 2000) */
uint16_t temp_avr = 2000;
/* Time keeping */
uint16_t seconds = 0;
uint16_t get_seconds(void);
/* Regulate to get 17.5 'C */
#define TEMP_AVR_GOAL 1750
uint16_t temp_avr_goal = TEMP_AVR_GOAL;
/* Min allowed temp to configure 8 'C */
#define MIN_TEMP_GOAL 800
#define MAX_TEMP_GOAL 2200
/* Max allowed temp to configure 22.9 'C */
#define MAX_MAX_TEMP_GOAL (MAX_TEMP_GOAL + 90)
uint8_t need_eeprom_update = 0;
/* Regulation cycle = 180 sec
must be multiplication of 3 and less than 255 (regulate() and calc_temp_avr() broke otherwise) */
#define CYCLE 180
/* Button on PB1 - active low, need pull-up */
#define BUTTON_GET ((PINB & (1 << PB1)) == 0)
void init_button(void)
{
DDRB &= ~(1 << PB1);
PORTB |= (1 << PB1); // Pull-up
}
/* Generate about 8 ms interrupt for LED display control */
void init_timer2(void)
{
/* 1024 prescaler, clock = 1024 / 16MHz = 64 us */
TCCR2 = (1 << CS22) | (1 << CS21) | (1 << CS20);
/* Clear Timer on Compare mode */
TCCR2 &= ~(1 << WGM20);
TCCR2 |= (1 << WGM21);
/* Clear timer */
TCNT2 = 0;
/* Generate interrupt every 120 cycles ~= 7.6 ms */
OCR2 = 120;
/* Enable timer interrupts */
TIFR |= (1 << OCF2);
TIMSK |= (1 << OCIE2);
}
/*
LED display map:
D2 D3 D4 A B C D E F G H
PD3 PD4 PD5 PD1 PD0 PC5 PC4 PD6 PC2 PC1 PC0
*/
#define SEG_A_ON (PORTD &= ~(1 << PD1))
#define SEG_A_OFF (PORTD |= (1 << PD1))
#define SEG_B_ON (PORTD &= ~(1 << PD0))
#define SEG_B_OFF (PORTD |= (1 << PD0))
#define SEG_C_ON (PORTC &= ~(1 << PC5))
#define SEG_C_OFF (PORTC |= (1 << PC5))
#define SEG_D_ON (PORTC &= ~(1 << PC4))
#define SEG_D_OFF (PORTC |= (1 << PC4))
#define SEG_E_ON (PORTD &= ~(1 << PD6))
#define SEG_E_OFF (PORTD |= (1 << PD6))
#define SEG_F_ON (PORTC &= ~(1 << PC2))
#define SEG_F_OFF (PORTC |= (1 << PC2))
#define SEG_G_ON (PORTC &= ~(1 << PC1))
#define SEG_G_OFF (PORTC |= (1 << PC1))
#define SEG_H_ON (PORTC &= ~(1 << PC0))
#define SEG_H_OFF (PORTC |= (1 << PC0))
#define DIGIT_0_ON (PORTD &= ~(1 << PD5))
#define DIGIT_0_OFF (PORTD |= (1 << PD5))
#define DIGIT_1_ON (PORTD &= ~(1 << PD4))
#define DIGIT_1_OFF (PORTD |= (1 << PD4))
#define DIGIT_2_ON (PORTD &= ~(1 << PD3))
#define DIGIT_2_OFF (PORTD |= (1 << PD3))
void init_led_display(void)
{
/* Set ports as output */
DDRC |= (1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC4) | (1 << PC5);
DDRD |= (1 << PD0) | (1 << PD1) | (1 << PD3) | (1 << PD4) | (1<< PD5) | (1 << PD6);
/* Turn off by default */
DIGIT_0_OFF;
DIGIT_1_OFF;
DIGIT_2_OFF;
SEG_A_OFF;
SEG_B_OFF;
SEG_C_OFF;
SEG_D_OFF;
SEG_E_OFF;
SEG_F_OFF;
SEG_G_OFF;
SEG_H_OFF;
}
uint8_t print_digit(uint8_t x)
{
switch (x) {
case 0:
SEG_A_ON;
SEG_B_ON;
SEG_C_ON;
SEG_D_ON;
SEG_E_ON;
SEG_F_ON;
SEG_G_OFF;
break;
case 1:
SEG_A_OFF;
SEG_B_ON;
SEG_C_ON;
SEG_D_OFF;
SEG_E_OFF;
SEG_F_OFF;
SEG_G_OFF;
break;
case 2:
SEG_A_ON;
SEG_B_ON;
SEG_C_OFF;
SEG_D_ON;
SEG_E_ON;
SEG_F_OFF;
SEG_G_ON;
break;
case 3:
SEG_A_ON;
SEG_B_ON;
SEG_C_ON;
SEG_D_ON;
SEG_E_OFF;
SEG_F_OFF;
SEG_G_ON;
break;
case 4:
SEG_A_OFF;
SEG_B_ON;
SEG_C_ON;
SEG_D_OFF;
SEG_E_OFF;
SEG_F_ON;
SEG_G_ON;
break;
case 5:
SEG_A_ON;
SEG_B_OFF;
SEG_C_ON;
SEG_D_ON;
SEG_E_OFF;
SEG_F_ON;
SEG_G_ON;
break;
case 6:
SEG_A_ON;
SEG_B_OFF;
SEG_C_ON;
SEG_D_ON;
SEG_E_ON;
SEG_F_ON;
SEG_G_ON;
break;
case 7:
SEG_A_ON;
SEG_B_ON;
SEG_C_ON;
SEG_D_OFF;
SEG_E_OFF;
SEG_F_OFF;
SEG_G_OFF;
break;
case 8:
SEG_A_ON;
SEG_B_ON;
SEG_C_ON;
SEG_D_ON;
SEG_E_ON;
SEG_F_ON;
SEG_G_ON;
break;
case 9:
SEG_A_ON;
SEG_B_ON;
SEG_C_ON;
SEG_D_ON;
SEG_E_OFF;
SEG_F_ON;
SEG_G_ON;
break;
default:
SEG_A_OFF;
SEG_B_OFF;
SEG_C_OFF;
SEG_D_OFF;
SEG_E_OFF;
SEG_F_OFF;
SEG_G_OFF;
break;
}
}
void print_number(uint16_t val, uint8_t blink)
{
static uint8_t digit_no = 0;
static uint8_t count = 0;
uint8_t digit;
/* This called every 8ms, 30*8ms ~ 0.25s */
if (blink) {
count++;
/* 0.25s OFF - 0.5s ON */
if (count < 30) {
/* Blink fraction digit */
if ((blink & 1) && digit_no == 0)
goto out;
/* Blink integer digits */
if ((blink & 2) && digit_no != 0)
goto out;
} else if (count > 90) {
count = 0;
}
}
switch (digit_no) {
case 0:
digit = val % 10;
DIGIT_0_ON;
break;
case 1:
digit = (val / 10) % 10;
DIGIT_1_ON;
SEG_H_ON;
break;
case 2:
digit = val / 100;
DIGIT_2_ON;
break;
}
print_digit(digit);
/* Changing this value regulate brightness */
_delay_us(100);
switch (digit_no) {
case 0:
DIGIT_0_OFF;
break;
case 1:
DIGIT_1_OFF;
SEG_H_OFF;
break;
case 2:
DIGIT_2_OFF;
break;
}
out:
digit_no++;
if (digit_no > 2)
digit_no = 0;
}
typedef enum { MODE_PRINT_TEMP, MODE_PRINT_GOAL, MODE_CONFIGURE } ModeEnum;
typedef enum { INCREASE_FRACTION = 1, INCREASE_INTEGER = 2} IncEnum;
uint16_t temp_avr_goal_new;
void change_new_goal(IncEnum inc)
{
uint8_t rem, div;
div = temp_avr_goal_new / 100;
rem = temp_avr_goal_new % 100;
switch (inc) {
default:
case INCREASE_FRACTION:
/* Decimal part between 0 and 9 'dC */
rem += 10;
if (rem > 90)
rem = 0;
break;
case INCREASE_INTEGER:
/* Interger part between 8 and 22 'C */
div++;
if (div > (MAX_TEMP_GOAL/100))
div = (MIN_TEMP_GOAL/100);
break;
}
temp_avr_goal_new = ((uint16_t) div) * 100 + rem;
}
ISR(TIMER2_COMP_vect)
{
/* This called every 8ms, 30*8ms ~ 0.25s */
static ModeEnum mode = MODE_PRINT_TEMP;
static IncEnum inc = INCREASE_FRACTION;
static uint8_t count = 0;
static uint8_t press = 0;
uint8_t button;
uint8_t blink;
uint16_t val;
button = BUTTON_GET;
switch (mode) {
default:
case MODE_PRINT_TEMP:
if (button) {
count++;
if (count > 5) {
count = 0;
mode = MODE_PRINT_GOAL;
}
} else {
count = 0;
}
val = temp_val;
/* Show blinking goal during init or updating EEPROM */
if (val == 0 || need_eeprom_update) {
blink = 3;
val = temp_avr_goal / 10;
} else {
blink = 0;
}
break;
case MODE_PRINT_GOAL:
if (button) {
count++;
if (count > 250) { // 2 seconds
count = 0;
press = 0;
inc = INCREASE_FRACTION;
temp_avr_goal_new = temp_avr_goal;
mode = MODE_CONFIGURE;
}
} else {
mode = MODE_PRINT_TEMP;
count = 0;
}
val = temp_avr_goal / 10;
blink = 0;
break;
case MODE_CONFIGURE:
if (button) {
if (press == 1) {
change_new_goal(inc);
count = 0;
}
press = 0;
} else {
count++;
if (count > 250) {
count = 0;
if (inc == INCREASE_INTEGER) {
temp_avr_goal = temp_avr_goal_new;
need_eeprom_update = 1;
mode = MODE_PRINT_TEMP;
} else {
inc = INCREASE_INTEGER;
}
}
press = 1;
}
val = temp_avr_goal_new / 10;
blink = inc;
break;
}
print_number(val, blink);
}
/* Generate one second interrupt for time keeping */
void init_timer1(void)
{
/* 1024 prescaler, clock = 1024 / 16MHz = 64 us */
TCCR1B &= ~(1 << CS12);
TCCR1B = (1 << CS12) | (1 << CS10);
/* Clear Timer on Compare (CTC) mode */
TCCR1A &= ~((1 << WGM11) | (1 << WGM10));
TCCR1B &= ~(1 << WGM13);
TCCR1B |= (1 << WGM12);
/* Generate interrupt every 15625 (125*125) cycles = 1 second */
OCR1A = 0x3d09;
/* Clear timer */
TCNT1 = 0;
/* Enable timer interrupts */
TIFR |= (1 << OCF1A);
TIMSK |= (1 << OCIE1A);
}
ISR(TIMER1_COMPA_vect)
{
seconds++;
}
uint16_t get_seconds(void)
{
uint16_t sec;
cli();
sec = seconds;
sei();
return sec;
}
/* One Wire sensor interface on pin PB0 */
#define OW_GET_IN (PINB & (1 << PB0))
#define OW_OUT_LOW (PORTB &= ~(1 << PB0))
#define OW_OUT_HIGH (PORTB |= (1 << PB0))
#define OW_DIR_IN (DDRB &= ~(1 << PB0))
#define OW_DIR_OUT (DDRB |= (1 << PB0))
uint8_t ow_reset(void)
{
uint8_t ret;
OW_OUT_LOW;
OW_DIR_OUT;
_delay_us(480);
cli();
OW_DIR_IN;
_delay_us(64);
ret = OW_GET_IN;
sei();
_delay_us(480 - 64);
if (ret != 0 || OW_GET_IN == 0)
return 1;
else
return 0;
}
#define OW_DELAY_OFF 7
void ow_wr_bit(uint8_t bit)
{
cli();
OW_OUT_LOW;
OW_DIR_OUT;
_delay_us(15 - OW_DELAY_OFF);
if (bit)
OW_DIR_IN;
_delay_us(60 - 15 + OW_DELAY_OFF);
OW_DIR_IN;
sei();
/* Recovery */
_delay_us(1);
}
uint8_t ow_rd_bit(void)
{
uint8_t bit;
cli();
OW_OUT_LOW;
OW_DIR_OUT;
_delay_us(2);
OW_DIR_IN;
_delay_us(15 - 2 - OW_DELAY_OFF);
bit = OW_GET_IN;
_delay_us(60 - 15 + OW_DELAY_OFF);
sei();
/* Recovery */
_delay_us(1);
return bit;
}
void ow_write(uint8_t byte)
{
uint8_t i;
for (i = 0; i < 8; i++) {
ow_wr_bit(byte & 1);
byte >>= 1;
}
}
uint8_t ow_read(void)
{
uint8_t byte = 0;
uint8_t bit, i;
for (i = 0; i < 8; i++) {
bit = ow_rd_bit();
byte >>= 1;
if (bit)
byte |= 0x80;
}
return byte;
}
#define OW_CMD_READ_ROM 0x33
#define OW_CMD_MATCH_ROM 0x55
#define OW_CMD_READ_SCRATCH 0xbe
#define OW_CMD_CONVERT_T 0x44
void ow_command(uint8_t cmd, uint8_t *data, uint8_t len, uint8_t rd)
{
int i;
ow_write(cmd);
for (i = 0; i < len; i++) {
if (rd)
data[i] = ow_read();
else
ow_write(data[i]);
}
}
/* Broken RELAY - connects 2 from 3 outputs only if disabled, otherwise all outputs are separated */
#define SWITCH_ON (PORTD &= ~(1 << PD7))
#define SWITCH_OFF (PORTD |= (1 << PD7))
uint8_t switch_on;
uint8_t switch_is_on;
/* Time of last relay switch */
uint16_t last_switch = 0;
#define SWITCH_DELAY 10
void init_switch(void)
{
DDRD |= (1 << PD7);
_delay_ms(10);
SWITCH_ON; // broken RELAY is ON by default, even on POWER OFF
switch_on = 1;
switch_is_on = 1;
_delay_ms(10);
/* We did not make RELAY switch here - allow to switch instantly */
last_switch = get_seconds() - SWITCH_DELAY;
}
void change_switch(uint16_t sec)
{
/* Don't switch more often than SWITCH_DELAY seconds */
if (sec - last_switch < SWITCH_DELAY)
return;
if (switch_on == switch_is_on)
return;
if (switch_on)
SWITCH_ON;
else
SWITCH_OFF;
switch_is_on = switch_on;
last_switch = sec;
}
/* TODO: moving average */
void calc_temp_avr(uint16_t sec)
{
static uint8_t n = 0;
static int32_t temp_avr_sum = 0;
int16_t temp = temp_val * 10;
if (n == 0) { // Init
temp_avr = temp;
temp_avr_sum = temp;
n = 1;
return;
}
if ((sec % CYCLE) == 0) {
if (n != 0)
temp_avr = temp_avr_sum / n;
else
temp_avr = temp; // Not happen
temp_avr_sum = temp;
n = 1;
} else {
if (n < 255) {
temp_avr_sum += temp;
n++;
}
}
}
void regulate(uint16_t sec)
{
uint8_t on;
uint8_t sec_in_cycle = sec % CYCLE;
uint16_t goal;
/* Race with MODE_CONFIGURE on timer2 */
cli();
goal = temp_avr_goal;
sei();
/* Output 0% if temp <= GOAL
Output 33% if temp > GOAL
Output 66% if temp > GOAL + 0.15 'C
Output 100% if temp > GOAL + 0.30 'C
*/
if (temp_avr > goal + 30)
on = 1;
else if (temp_avr > goal + 15)
on = (sec_in_cycle < 2*CYCLE/3) ? 1 : 0;
else if (temp_avr > goal)
on = (sec_in_cycle < 1*CYCLE/3) ? 1 : 0;
else
on = 0;
switch_on = on;
}
void init_eeprom(void)
{
uint16_t goal;
eeprom_busy_wait(); // Not needed ?
goal = eeprom_read_word((uint16_t *) 0);
if (goal >= MIN_TEMP_GOAL && goal <= MAX_MAX_TEMP_GOAL) {
cli();
temp_avr_goal = goal;
sei();
}
}
void update_eeprom(void)
{
uint16_t goal;
if (need_eeprom_update && eeprom_is_ready()) {
cli();
goal = temp_avr_goal;
sei();
eeprom_write_word((uint16_t *) 0, goal);
need_eeprom_update = 0;
}
}
void main(void)
{
static uint8_t ds18b20_rom[8];
static uint8_t scratch[9];
uint8_t i;
uint16_t sec, val;
uint16_t done_second = 0;
ow_reset();
ow_command(OW_CMD_READ_ROM, ds18b20_rom, 8, 1);
init_led_display();
init_timer1();
init_timer2();
sei();
init_switch();
init_button();
init_eeprom();
/* Print goal for short period after power ON */
done_second = get_seconds() + 2;
while ((sec = get_seconds()) != done_second)
;
done_second = sec;
while (1) {
ow_reset();
ow_command(OW_CMD_MATCH_ROM, ds18b20_rom, 8, 0);
ow_write(OW_CMD_CONVERT_T);
/* 750 ms delay for conversion */
for (i = 0; i < 75; i++)
_delay_ms(10);
ow_reset();
ow_command(OW_CMD_MATCH_ROM, ds18b20_rom, 8, 0);
ow_command(OW_CMD_READ_SCRATCH, scratch, 9, 1);
val = ((int16_t)scratch[1] << 4) + (scratch[0] >> 4);
val *= 10;
val += (10 * (scratch[0] & 0xf)) / 16;
cli(); // Race with Timer2 (LED Display) interrupt
temp_val = val;
sei();
update_eeprom(); // if needed
/* Wait till 1 second from last iteration */
while ((sec = get_seconds()) == done_second)
;
done_second = sec;
calc_temp_avr(sec);
regulate(sec);
change_switch(sec);
}
}