-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathds3232.h
125 lines (106 loc) · 2.89 KB
/
ds3232.h
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
#ifndef TIMER_RTC_H
#define TIMER_RTC_H
//http://github.com/JChristensen/DS3232RTC
#include <DS3232RTC.h>
static uint8_t __attribute__ ((noinline)) bcd2dec(uint8_t n) {
return n - 6 * (n >> 4);
}
const char* alarm2s(ALARM_TYPES_t alarm) {
switch (alarm) {
case ALM2_EVERY_MINUTE:
return "Every minute";
case ALM2_MATCH_MINUTES:
return "Match minute";
case ALM2_MATCH_HOURS:
return "Hour and minute";
}
return "Unknown";
}
uint8_t is_alarm_active(uint8_t alarm_number) {
uint8_t controlReg, mask;
controlReg = RTC.readRTC(RTC_CONTROL);
mask = _BV(A1IE) << (alarm_number - 1);
if (controlReg & mask) {
return 1;
}
return 0;
}
void setAlarm(uint8_t alarm_number, alarm_t* alarm) {
RTC.setAlarm(alarm->type, alarm->seconds, alarm->minutes, alarm->hours, 0);
RTC.alarmInterrupt(alarm_number, true);
}
/*
assuming 24 hour clock.
does not fetch day/date
*/
void readAlarm(uint8_t alarm_number, alarm_t* alarm) {
uint8_t addr;
if (alarm_number == ALARM_1) {
addr = ALM1_SECONDS;
} else {
addr = ALM2_MINUTES;
}
uint8_t seconds = 0, minutes, hour, date, alarm_type = 0;
if (alarm_number == ALARM_1) {
RTC.readRTC(addr++, &seconds, 1);
}
RTC.readRTC(addr, &minutes, 1);
RTC.readRTC(addr + 1, &hour, 1);
RTC.readRTC(addr + 2, &date, 1);
/* get the 8th bit and shift it right to 2^4 */
alarm_type = (date & (1 << 7)) >> 4;
/* get the 8th bit and shift it right to 2^3 */
alarm_type = alarm_type | ((hour & (1 << 7)) >> 5);
/* get the 8th bit and shift it right to 2^2 */
alarm_type = alarm_type | ((minutes & (1 << 7)) >> 6);
/* get the 8th bit */
alarm_type = alarm_type | ((seconds & (1 << 7)) >> 7);
if (alarm_number == ALARM_2) {
/* shift one bit to the right, removing seconds */
alarm_type = alarm_type >> 1;
}
/* does not fetch DY/DT, thus ignoring ALM2_MATCH_DAY and ALM2_MATCH_DATE */
switch (alarm_type) {
case 16:
alarm->type = ALM1_MATCH_DAY;
case 15:
alarm->type = ALM1_EVERY_SECOND;
break;
case 14:
alarm->type = ALM1_MATCH_SECONDS;
break;
case 12:
alarm->type = ALM1_MATCH_MINUTES;
break;
case 8:
if (alarm_number == ALARM_1) {
alarm->type = ALM1_MATCH_HOURS;
} else {
alarm->type = ALM2_MATCH_DAY;
}
break;
case 7:
alarm->type = ALM2_EVERY_MINUTE;
break;
case 6:
alarm->type = ALM2_MATCH_MINUTES;
break;
case 4:
alarm->type = ALM2_MATCH_HOURS;
break;
case 0:
if (alarm_number == ALARM_1) {
alarm->type = ALM1_MATCH_DATE;
} else {
alarm->type = ALM2_MATCH_DATE;
}
break;
}
alarm->alarm = alarm_number;
/* ignoring 8th bit */
alarm->seconds = bcd2dec(seconds & ~(1 << 7));
alarm->minutes = bcd2dec(minutes & ~(1 << 7));
/* ignoring 8th and 7th bit */
alarm->hours = bcd2dec(hour & ((1 << 6) - 1));
}
#endif