-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.cpp
1056 lines (960 loc) · 27.9 KB
/
watch.cpp
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
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*A source file for multiplexing
* Timer0 is used for multiplexing
* Timer1 is used for the button press and general timing needs
*
*
*/
#include "watch.h"
#include <avr/io.h>
#include <stdlib.h>
#include "twi.h"
#include <avr/interrupt.h>
#include <avr/sleep.h>
namespace watch{
//Variables to store the current time from the RT clock
Time time_DEC; //Current time in DEC format
Time time_BCD; //Current time in BCD format
Time time_DEC_new; //New time to be set on the watch in DEC format
Time time_BCD_new; //New time to be set on the watch in BCD format
uint8_t col; //A variable to store current column to be displayed - used for multiplexing
uint16_t press_how_long = 0; //An internal variable to determine the length of the press
uint8_t pressed = NOT; //A variable for the length of the last press. Set to not-pressed
float battery_level = 0; //A variable to store the battery level (initialized to 0)
State state = GO_SLEEP; //A variable to indicate the current state of the watch. Set to enter the sleep mode
float u_supply = 0; //A variable to store the current voltage of the battery
uint16_t u_supply_sum= 0; //Raw sum of the battery voltage readouts
float u_supply_show = 0; //A variable to store the voltage of the battery to be displayed on the LEDs
uint16_t blink_counter = 0; //A counter for blinking function
bool blink = true; //Should the LEDs blink?
uint8_t error = 0; //An internal error variable
uint8_t light = 255; //A variable to store how much light it is outside (values from 0 (high light) - 255 (low light))
uint8_t light_vect[] = {255,255,255,255};
uint16_t light_sum = 0;
adc_stat ADC_stat = MEASURE_LIGHT; //A variable to store the ADC internal state
uint8_t adc_counter = 0; //Counter for the light average
//Instance of the Twi class - communication with the RT clock
twi Twi;
}
ISR(BADISR_vect)
{
// skip a random interrupt
}
////////////////////LED multiplexing and visualization ////////////////////////////////
ISR(TIMER2_COMPA_vect){
//dim the LEDs according to the light
watch::clear_LED();
}
ISR(TIMER2_COMPB_vect){
//set the LEDs according to what should be shown
watch::ISR_handler(); //Turn on the correct LEDs
}
//////////////////Functions to handle bottom interrupts/////////////////////////////
ISR (INT0_vect)
{
EIMSK &= ~(1 << INT0); //Turns off INT0
TIMSK1 |= (1 << OCIE1A)|(1 << OCIE1B); //Enable Timer1 on compare match A and B
TCNT1 = 0; //Reset timer1 counter
}
ISR (TIMER1_COMPA_vect){
EIMSK |= (1 << INT0); //Turns on INT0
TIMSK1 &= ~((1 << OCIE1B)|(1 << OCIE1A)); //Disables Timer1 on compare match A and B
watch::state = GO_SLEEP;
}
ISR (TIMER1_COMPB_vect){
TCNT1 = 0; //set the counter to 0
//Check for the bounce and how long the press holds
//If pressed, increment the counter
if ((PIND & (1<<PIND2)) == 0){
watch::press_how_long++;
}
//If not pressed, determine how long the press was
else{if (watch::press_how_long >= EX_LONG_PRESS){
//PORTB |= LED_ON;
watch::pressed = EX_LONG;
}else if (watch::press_how_long >= LONG_PRESS){
//PORTB |= LED_ON;
watch::pressed = LONG;
}else if (watch::press_how_long >= SHORT_PRESS){
//PORTB |= LED_ON;
watch::pressed = SHORT;
}else if (watch::press_how_long < SHORT_PRESS){
//PORTB &= LED_OFF;
watch::pressed = NOT;
}
watch::press_how_long = 0;
EIMSK |= (1 << INT0); //Turns on INT0
TIMSK1 &= ~(1 << OCIE1B); //Disables Timer1 on compare match B
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
// ADC, voltage and light measurements
// To measure the light, supply voltage is measured first, then, the light intensity is
// scaled accordingly
// First,
//
// After calling watch::ADC_get_supply_light(), timer0 is started to enable the voltage
// on the internal reference to settle. After ca. 10 ms, the measurement of the battery
// voltage is started. After done, an ADC interrupt is triggered. In order to get more
// precise measurement, the voltage is measured 16 times and averaged before the actual
// light measurement is performed. The battery voltage is stored in a variable
// watch::u_supply. The light intensity is measured 8 times and the average is returned
// and saved in watch:.light .
ISR(ADC_vect){
// Interrupt handler for the ADC
// If measuring battery, save to the watch::u_supply and then measures the light intensity and
// saves to watch::light. If done, disable the ADC
sei(); //Enable the ISR to be interrupted by other interrupts
//If measuring the battery
if (watch::ADC_stat == MEASURE_SUPPLY) {
// Average 8 measurements together to eliminate the ripple
if (watch::adc_counter < 16){
watch::adc_counter++;
watch::u_supply_sum += ADC; // add the ADC readout to the voltage sum
watch::ADC_start();
}else{
watch::adc_counter = 0; // zero the adc_coutner
// AVcc = Vbg/ADC*1023 = 1.1V*1023/ADC - Use the average u_supply_sum instead fo the ADC
watch::u_supply = (1.1*1023/(watch::u_supply_sum >> 4));
watch::u_supply_sum = 0; // zero the average supply voltage sum
watch::ADC_get_light(); // start the light measurement
}
//If measuring the light
}else if(watch::adc_counter < 8){
// Average 8 measurements to eliminate the ripple
watch::adc_counter++;
watch::light_sum += ADC;
watch::ADC_start();
//If done, disable the ADC and clear the variables
}else{
watch::adc_counter = 0;
watch::ADC_disable();
watch::light = watch::light_sum >> 5;
watch::light_sum = 0;
}
}
ISR (TIMER0_COMPA_vect){
// Timer0 interrupt used to wait before the battery voltage is measured
watch::ADC_timer0_stop();
sei(); //Enable the interrupt being interrupted by other interrupts
watch::ADC_start();
}
void watch::ADC_timer0_init(){
// set up timer with CTC mode and prescaler = 256
TCCR0A |= (1 << WGM01); //Set CTC mode
TCCR0B |= (1 << CS02); //Set prescaler to 256
// initialize counter
TCNT0 = 0;
// set the frequency to ca 200 Hz
OCR0A = 39; //set to 200 Hz (78 for 50 Hz)
}
void watch::ADC_timer0_start(){
//Start the timer0 with correct mode
TCNT0 = 0; //Zero the timer counter
TIMSK0 |= (1 << OCIE0A); //Set interrupt on compare A match
}
void watch::ADC_timer0_stop(){
// Stop the timer0
TIMSK0 &= ~(1 << OCIE0A); //Disable interrupt on compare A match
}
void watch::ADC_init(){
// Initializes ADC with correct frequency
ADCSRA = (1 << ADPS2) | (1 << ADIE); //Set prescaler to 16 - 62500 Hz and enable interrupts from ADC
ADMUX |= (1 <<REFS0); //Set reference to AVCC
ADC_timer0_init(); //Initialize timer0 for the delayed measurements of power supply voltage
//ADC
DDRC |= (1 << PC2); // set PC2 as output
PORTC |= (1 << PC2); // set PC2 high - turn the power up for the photo-resistor
}
void watch::ADC_enable(){
ADCSRA |= (1 << ADEN); //Enable ADC
}
void watch::ADC_disable(){
ADCSRA &= ~(1 << ADEN); //Disable ADC
DDRC |= (1 << PC3); //Set PC3 as output
PORTC &= ~(1 << PC3); //Set PC3 low
PORTC &= ~(1 << PC2); //Set PC2 low
}
void watch::ADC_start(){
ADCSRA |= (1 << ADSC);
}
int watch::ADC_get_supply_light(){
ADC_enable();
ADMUX = ADMUX_ADC_VBG; // set to measure the AVCC using VBG
ADC_stat = MEASURE_SUPPLY; // set the ADC status: measure supply voltage
ADC_timer0_start(); // start the timer to introduce the measurement delay
return 0;
}
int watch::ADC_get_light(){
ADC_enable();
ADMUX = ADMUX_ADC_LIGHT; // set to measure the voltage on the opto-resistor
ADC_stat = MEASURE_LIGHT; // set the ADC status: measure the opto-resistor
DDRC &= ~(1 << PC3); // set PC3 as input - for the opto-resistor measurement
PORTC |= (1 << PC2); // set the supply pin PC2 high - turn the power to the opto-resistor
for (int i = 0; i < 10; i++); // wait for a bit to get the voltage settle
ADC_start();
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
void watch::ISR_handler(){
watch::col++; //Increment column count
if (watch::col == 2) watch::col++; //Skip 2 since bit 2 is not assigned to the port
watch::col = watch::col % 8; //modulo 8 to ensure that col <= 7
watch::show(watch::col, watch::time_BCD,state); //show state
watch::blink_counter = (watch::blink_counter + 1)%BLINK_CNTR_MAX; //Increment the blink counter and ensure it is in the limits of BLINK_CNTR_MAX
}
State watch::state_machine(uint8_t state, uint8_t pressed){
/*This function servers as a state automate which determines the state change after a bottom press*/
/*This function also updates current battery voltage*/
u_supply_show = u_supply;
State new_state = GO_SLEEP; //Set the new state temporally to GO_SLEEP
//Check the state and go to an appropriate routine accordingly
switch (state){
//If state == AFTER_SLEEP - default from sleep waking.
case AFTER_SLEEP:
switch (pressed){
case SHORT:
case LONG:
case EX_LONG:
new_state = SHOW_TIME;
break;
}
break;
//If state == SHOW_TIME
case SHOW_TIME:
//Check for how long the bottom was pressed and act accordingly
switch (pressed){
case SHORT:
new_state = SHOW_DATE;
break;
case LONG:
new_state = GO_SLEEP;
break;
case EX_LONG:
new_state = SET_SECONDS;
//Save the current time to a new variable
time_BCD_new = time_BCD;
break;
}
break;
//If state == SHOW_DATE
case SHOW_DATE:
//Check for how long the bottom was pressed and act accordingly
switch (pressed){
case SHORT:
new_state = SHOW_BAT;
break;
case LONG:
new_state = GO_SLEEP;
break;
case EX_LONG:
new_state = SET_SECONDS;
//Save the current time to a new variable
time_BCD_new = time_BCD;
break;
}
break;
//If state == SHOW_BAT
case SHOW_BAT:
//Check for how long the bottom was pressed and act accordingly
switch (pressed){
case SHORT:
new_state = GO_SLEEP;
break;
case LONG:
new_state = GO_SLEEP;
break;
case EX_LONG:
new_state = SET_SECONDS;
//Save the current time to a new variable
time_BCD_new = time_BCD;
break;
}
break;
case SET_SECONDS:
//Check for how long the bottom was pressed and act accordingly
switch (pressed){
case SHORT:
new_state = SET_SECONDS;
increment_seconds();
break;
case LONG:
new_state = SET_MINUTES;
break;
case EX_LONG:
new_state = SHOW_TIME;
time_BCD = time_BCD_new;
set_TimeDate(time_BCD_new);
break;
}
break;
case SET_MINUTES:
//Check for how long the bottom was pressed and act accordingly
switch (pressed){
case SHORT:
new_state = SET_MINUTES;
increment_minutes();
break;
case LONG:
new_state = SET_HOURS;
break;
case EX_LONG:
new_state = SHOW_TIME;
set_TimeDate(time_BCD_new);
break;
}
break;
case SET_HOURS:
//Check for how long the bottom was pressed and act accordingly
switch (pressed){
case SHORT:
new_state = SET_HOURS;
increment_hours();
break;
case LONG:
new_state = SET_DAYS;
break;
case EX_LONG:
new_state = SHOW_TIME;
set_TimeDate(time_BCD_new);
break;
}
break;
case SET_DAYS:
//Check for how long the bottom was pressed and act accordingly
switch (pressed){
case SHORT:
new_state = SET_DAYS;
increment_day();
break;
case LONG:
new_state = SET_MONTHS;
break;
case EX_LONG:
new_state = SHOW_TIME;
set_TimeDate(time_BCD_new);
break;
}
break;
case SET_MONTHS:
//Check for how long the bottom was pressed and act accordingly
switch (pressed){
case SHORT:
new_state = SET_MONTHS;
increment_month();
break;
case LONG:
new_state = SET_YEARS;
break;
case EX_LONG:
new_state = SHOW_TIME;
set_TimeDate(time_BCD_new);
break;
}
break;
case SET_YEARS:
//Check for how long the bottom was pressed and act accordingly
switch (pressed){
case SHORT:
new_state = SET_YEARS;
increment_year();
break;
case LONG:
new_state = SHOW_TIME;
break;
case EX_LONG:
new_state = SHOW_TIME;
set_TimeDate(time_BCD_new);
break;
}
break;
default:
break;
}
return new_state;
}
void watch::update_TimeDate(){
Twi.update_TimeDate();
time_DEC = Twi.get_TimeDate_DEC();
time_BCD = Twi.get_TimeDate_BCD();
}
uint8_t watch::set_TimeDate(Time time){
return Twi.set_TimeDate(time);
}
uint8_t watch::init_TimeDate(){
//Set a required time to the
Time current_time;
current_time.seconds = 0b00000000;
current_time.minutes = 0b00011001;
current_time.hours = 0b00100001;
current_time.days = 0b00000110;
current_time.weekdays = 0b00000000;
current_time.months = 0b00010001;
current_time.years = 0b00011001;
error = Twi.set_TimeDate(current_time);
return error;
}
void watch::sentToArduino(uint8_t data){
//Send to Arduino to check the functionality
Twi.start();
Twi.write(0b00001000);
Twi.write(data);
Twi.stop();
}
void watch::increment_seconds(){
time_BCD_new.seconds = twi::DEC2BCD((twi::BCD2DEC(time_BCD_new.seconds)+1)%60);
}
void watch::increment_minutes(){
time_BCD_new.minutes = twi::DEC2BCD((twi::BCD2DEC(time_BCD_new.minutes)+1)%60);
}
void watch::increment_hours(){
time_BCD_new.hours = twi::DEC2BCD((twi::BCD2DEC(time_BCD_new.hours)+1)%24);
}
void watch::increment_day(){
time_BCD_new.days = twi::DEC2BCD((twi::BCD2DEC(time_BCD_new.days))%31+1);
}
void watch::increment_month(){
time_BCD_new.months = twi::DEC2BCD((twi::BCD2DEC(time_BCD_new.months))%12+1);
}
void watch::increment_year(){
time_BCD_new.years = twi::DEC2BCD((twi::BCD2DEC(time_BCD_new.years)+1)%50);
}
void watch::timer1_init(){
// set the timer with prescaler = 256 and CTC mode
TCCR1B |= (1 << WGM12)|(1 << CS12);
// initialize counter
TCNT1 = 0;
// set the frequency to
// f = F_CPU/(prescaler*(1+OCRA))
// ~200 Hz
OCR1B = 10;
// ~0.125 Hz - fire once in 8s - makes the uC to enter the sleep mode
OCR1A = 31249;
TIMSK1 |= (1<<OCIE1A)|(1<<OCIE1B); //Enable interrupts on compare match
}
void watch::timer2_init(){
//Initialize timer2 for the multiplexing functionality
TCCR2A |= (1 << WGM21); // Set the mode to CTC
TCCR2B |= (1 << CS21); // set prescaler to 8 and start the timer
TIMSK2 |= (1 << OCIE2A); //Set interrupt on compare A match
TIMSK2 |= (1 << OCIE2B); //Set interrupt on compare B match
OCR2A = 250; //set the compare value for the interrupt to 250
OCR2B = 10; //set the compare value for the interrupt to 10
}
void watch::sleep_init(){
//select a Power-down mode
SMCR |= (1<<SM1);
}
void watch::button_init(){
//Button press
DDRD &= ~(1 << PD2); // set PD2 (PCINT0) to input
//PORTD |= (1 << PD2); // turn On the Pull-up
PORTD &= ~(1 << PD2); // Turn Off the Pull-up
EIMSK |= (1 << INT0); // Turns on INT0 in a normal operation mode (active low)
}
void watch::write_port(volatile uint8_t *port, uint8_t val, uint8_t mask){
uint8_t tmpval = *port; //Get the actual PORT value
//Mask bits which should not be changed and set the port accordingly
tmpval &= ~mask;
tmpval |= (val & mask);
*port = tmpval;
}
void watch::init(){
//Set the initial value for column - used for multiplexing
col = 0;
//cli(); //disable all interrupts
DDRB |= 0b00111100; //set PB2-PB5 as outputs
DDRD |= 0b11111011; //set PD0, PD1, PD3-PD7 as outputs
//Turn of not used modules
PRR |= (1 << PRUSART0); // Turn off the USART0
PRR |= (1 << PRSPI); // Turn off SPI
//cli()
clear_LED(); //set the pins so the LEDs are cleared
//Button press
button_init();
//ADC initialization for the battery and light measurement
ADC_init();
//Initialize timer1 for the bottom press and automatic GO_SLEEP after 8s
timer1_init();
//Initialize timer2 for the multiplexing - LED visualization
timer2_init();
//Initialize the sleep mode
sleep_init();
//sei(); // enable global interrupts
//Initialize the watch with time to start the RT clock to operate - Uses TWI - interrupts have to be enabled
init_TimeDate();
}
void watch::set_LED_intensity(uint8_t intensity){
//A function to set an intensity of the LEDs
//Get the values to reasonable levels (1-249)
uint8_t intensity_temp = (intensity/255.0)*249 + 1;
if (intensity_temp > 249) intensity_temp = 249;
//Write to the compare match B register on timer 2
OCR2B = intensity_temp;
}
inline uint8_t watch::multiplex(uint8_t col){
return (0b00000001 << col) & 0b11111011;
}
void watch::clear_LED(){
// Safely clear all LEDs
WRITE_PORT(PORTB, 0b00111100, 0b00111100);
WRITE_PORT(PORTD, 0b00000000, 0b11111011);
}
void watch::set_all_pins_low_LED(){
// Set all output LED pins low - for energy savings
WRITE_PORT(PORTB,0b00000000,0b00111100); // set all LED outputs on PORTB low
WRITE_PORT(PORTD,0b00000000,0b11111011); // set all LED outputs on PORTD low
}
void watch::show(uint8_t col, Time time, State state){
uint8_t MLTX = multiplex(col);
uint8_t row = 0;
uint8_t batt = 0;
//Switch between different states
switch (state){
case SHOW_TIME:
blink = false;
switch (col){
case 0:
row = ~((time_BCD.seconds & 0b00001111)<<2);
clear_LED();
WRITE_PORT(PORTB, row, 0b00111100); //Write the correct values to port B, 0 means LED ON!
WRITE_PORT(PORTD, MLTX, 0b11111011);
break;
case 1:
row = ~((time_BCD.seconds >> 4 & 0b00000111)<<2);
clear_LED();
WRITE_PORT(PORTB, row, 0b00111100);
WRITE_PORT(PORTD, MLTX, 0b11111011);
break;
case 2:
clear_LED();
break;
case 3:
row = ~((time_BCD.minutes & 0b00001111)<<2);
clear_LED();
WRITE_PORT(PORTB, row, 0b00111100); //Write the correct values to port B, 0 means LED ON!
WRITE_PORT(PORTD, MLTX, 0b11111011);
break;
case 4:
row = ~((time_BCD.minutes >> 4 & 0b00000111)<<2);
clear_LED();
WRITE_PORT(PORTB, row, 0b00111100);
WRITE_PORT(PORTD, MLTX, 0b11111011);
break;
case 5:
row = ~((time_BCD.hours & 0b00001111)<<2);
clear_LED();
WRITE_PORT(PORTB, row, 0b00111100);
WRITE_PORT(PORTD, MLTX, 0b11111011);
break;
case 6:
row = ~((time_BCD.hours >> 4 & 0b00000011)<<2);
clear_LED();
WRITE_PORT(PORTB, row , 0b00111100);
WRITE_PORT(PORTD, MLTX, 0b11111011);
break;
case 7:
clear_LED();
WRITE_PORT(PORTB, ~0b00010000, 0b00111100);
WRITE_PORT(PORTD, MLTX, 0b11111011);
break;
}
break;
case SHOW_DATE:
blink = false;
switch (col){
case 0:
row = ~((time_BCD.years & 0b00001111)<<2);
clear_LED();
WRITE_PORT(PORTB, row, 0b00111100);
WRITE_PORT(PORTD, MLTX, 0b11111011);
break;
case 1:
row = ~((time_BCD.years >> 4 & 0b00001111)<<2);
clear_LED();
WRITE_PORT(PORTB, row, 0b00111100);
WRITE_PORT(PORTD, MLTX, 0b11111011);
break;
case 2:
clear_LED();
break;
case 3:
row = ~((time_BCD.months & 0b00001111)<<2);
clear_LED();
WRITE_PORT(PORTB, row, 0b00111100);
WRITE_PORT(PORTD, MLTX, 0b11111011);
break;
case 4:
row = ~((time_BCD.months >> 4 & 0b00000001)<<2);
clear_LED();
WRITE_PORT(PORTB, row, 0b00111100);
WRITE_PORT(PORTD, MLTX, 0b11111011);
break;
case 5:
row = ~((time_BCD.days & 0b00001111)<<2);
clear_LED();
WRITE_PORT(PORTB, row, 0b00111100);
WRITE_PORT(PORTD, MLTX, 0b11111011);
break;
case 6:
row = ~((time_BCD.days >> 4 & 0b00000011)<<2);
clear_LED();
WRITE_PORT(PORTB, row, 0b00111100);
WRITE_PORT(PORTD, MLTX, 0b11111011);
break;
case 7:
clear_LED();
WRITE_PORT(PORTB, ~0b00001000, 0b00111100);
WRITE_PORT(PORTD, MLTX, 0b11111011);
break;
}
break;
case SHOW_BAT:
blink = false;
batt = ((uint8_t(u_supply_show) << 4) + (uint8_t(u_supply_show*10)%10));
switch (col){
case 0:
row = ~((batt & 0b00001111)<<2);
clear_LED();
WRITE_PORT(PORTB, row, 0b00111100); //Write the correct values to port B, 0 means LED ON!
WRITE_PORT(PORTD, MLTX, 0b11111011);
break;
case 1:
row = ~(((batt >> 4) & 0b00000111)<<2);
clear_LED();
WRITE_PORT(PORTB, row, 0b00111100); //Write the correct values to port B, 0 means LED ON!
WRITE_PORT(PORTD, MLTX, 0b11111011);
break;
case 7:
clear_LED();
WRITE_PORT(PORTB, ~0b00000100, 0b00111100);
WRITE_PORT(PORTD, MLTX, 0b11111011);
break;
default:
clear_LED();
break;
}
break;
case SET_SECONDS:
switch (col){
case 0:
blink = true;
row = ~((time_BCD_new.seconds & 0b00001111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 1:
blink = true;
row = ~((time_BCD_new.seconds >> 4 & 0b00000111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 2:
blink = false;
clear_LED();
break;
case 3:
blink = false;
row = ~((time_BCD_new.minutes & 0b00001111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 4:
blink = false;
row = ~((time_BCD_new.minutes >> 4 & 0b00000111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 5:
blink = false;
row = ~((time_BCD_new.hours & 0b00001111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 6:
blink = false;
row = ~((time_BCD_new.hours >> 4 & 0b00000011)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 7:
blink = false;
clear_LED();
set_LED(~0b00010000, MLTX);
break;
}
break;
case SET_MINUTES:
switch (col){
case 0:
blink = false;
row = ~((time_BCD_new.seconds & 0b00001111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 1:
blink = false;
row = ~((time_BCD_new.seconds >> 4 & 0b00000111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 2:
blink = false;
clear_LED();
break;
case 3:
blink = true;
row = ~((time_BCD_new.minutes & 0b00001111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 4:
blink = true;
row = ~((time_BCD_new.minutes >> 4 & 0b00000111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 5:
blink = false;
row = ~((time_BCD_new.hours & 0b00001111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 6:
blink = false;
row = ~((time_BCD_new.hours >> 4 & 0b00000011)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 7:
blink = false;
clear_LED();
set_LED(~0b00010000, MLTX);
break;
}
break;
case SET_HOURS:
switch (col){
case 0:
blink = false;
row = ~((time_BCD_new.seconds & 0b00001111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 1:
blink = false;
row = ~((time_BCD_new.seconds >> 4 & 0b00000111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 2:
blink = false;
clear_LED();
break;
case 3:
blink = false;
row = ~((time_BCD_new.minutes & 0b00001111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 4:
blink = false;
row = ~((time_BCD_new.minutes >> 4 & 0b00000111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 5:
blink = true;
row = ~((time_BCD_new.hours & 0b00001111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 6:
blink = true;
row = ~((time_BCD_new.hours >> 4 & 0b00000011)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 7:
blink = false;
clear_LED();
set_LED(~0b00010000, MLTX);
break;
}
break;
case SET_DAYS:
blink = false;
switch (col){
case 0:
blink = false;
row = ~((time_BCD_new.years & 0b00001111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 1:
blink = false;
row = ~((time_BCD_new.years >> 4 & 0b00001111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 2:
blink = false;
clear_LED();
break;
case 3:
blink = false;
row = ~((time_BCD_new.months & 0b00001111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 4:
blink = false;
row = ~((time_BCD_new.months >> 4 & 0b00000001)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 5:
blink = true;
row = ~((time_BCD_new.days & 0b00001111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 6:
blink = true;
row = ~((time_BCD_new.days >> 4 & 0b00000011)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 7:
blink = false;
clear_LED();
set_LED(~0b00001000, MLTX);
break;
}
break;
case SET_MONTHS:
blink = false;
switch (col){
case 0:
blink = false;
row = ~((time_BCD_new.years & 0b00001111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 1:
blink = false;
row = ~((time_BCD_new.years >> 4 & 0b00001111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 2:
blink = false;
clear_LED();
break;
case 3:
blink = true;
row = ~((time_BCD_new.months & 0b00001111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 4:
blink = true;
row = ~((time_BCD_new.months >> 4 & 0b00000001)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 5:
blink = false;
row = ~((time_BCD_new.days & 0b00001111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 6:
blink = false;
row = ~((time_BCD_new.days >> 4 & 0b00000011)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 7:
blink = false;
clear_LED();
set_LED(~0b00001000, MLTX);
break;
}
break;
case SET_YEARS:
blink = false;
switch (col){
case 0:
blink = true;
row = ~((time_BCD_new.years & 0b00001111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 1:
blink = true;
row = ~((time_BCD_new.years >> 4 & 0b00001111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 2:
blink = false;
clear_LED();
break;
case 3:
blink = false;
row = ~((time_BCD_new.months & 0b00001111)<<2);
clear_LED();
set_LED(row, MLTX);
break;
case 4:
blink = false;
row = ~((time_BCD_new.months >> 4 & 0b00000001)<<2);