Skip to content

Commit 50d9b8b

Browse files
committed
Added H12 AM/PM support.
Note in set hours the am_pm parameter is ignored if the hours > 12.
1 parent 7b3215d commit 50d9b8b

File tree

2 files changed

+109
-25
lines changed

2 files changed

+109
-25
lines changed

src/RTCZero.cpp

Lines changed: 96 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ void RTCZero::begin(bool timeRep)
5757
tmp_reg |= RTC_MODE2_CTRL_PRESCALER_DIV1024; // set prescaler to 1024 for MODE2
5858
tmp_reg &= ~RTC_MODE2_CTRL_MATCHCLR; // disable clear on match
5959

60+
//According to the datasheet RTC_MODE2_CTRL_CLKREP = 0 for 24h
6061
if (timeRep)
61-
tmp_reg |= RTC_MODE2_CTRL_CLKREP; // 24h time representation
62+
tmp_reg &= ~RTC_MODE2_CTRL_CLKREP; // 24h time representation
6263
else
63-
tmp_reg &= ~RTC_MODE2_CTRL_CLKREP; // 12h time representation
64+
tmp_reg |= RTC_MODE2_CTRL_CLKREP; // 12h time representation
6465

6566
RTC->MODE2.READREQ.reg &= ~RTC_READREQ_RCONT; // disable continuously mode
6667

@@ -133,7 +134,31 @@ uint8_t RTCZero::getMinutes()
133134

134135
uint8_t RTCZero::getHours()
135136
{
136-
return RTC->MODE2.CLOCK.bit.HOUR;
137+
uint8_t hours = RTC->MODE2.CLOCK.bit.HOUR;
138+
139+
if (!__time24) {
140+
hours &= ~RTC_MODE2_CLOCK_HOUR_PM_Val;
141+
}
142+
143+
return hours;
144+
}
145+
146+
uint8_t RTCZero::getAM_PM()
147+
{
148+
uint8_t result = RTC_AM_PM::RTC_AM;
149+
150+
if (__time24) {
151+
if (RTC->MODE2.CLOCK.bit.HOUR > 11) {
152+
result = RTC_AM_PM::RTC_PM;
153+
}
154+
}
155+
else {
156+
if (RTC->MODE2.CLOCK.bit.HOUR & RTC_MODE2_CLOCK_HOUR_PM_Val) {
157+
result = RTC_AM_PM::RTC_PM;
158+
}
159+
}
160+
161+
return result;
137162
}
138163

139164
uint8_t RTCZero::getDay()
@@ -163,7 +188,31 @@ uint8_t RTCZero::getAlarmMinutes()
163188

164189
uint8_t RTCZero::getAlarmHours()
165190
{
166-
return RTC->MODE2.Mode2Alarm[0].ALARM.bit.HOUR;
191+
uint8_t hours = RTC->MODE2.Mode2Alarm[0].ALARM.bit.HOUR;
192+
193+
if (!__time24) {
194+
hours &= ~RTC_MODE2_CLOCK_HOUR_PM_Val;
195+
}
196+
197+
return hours;
198+
}
199+
200+
uint8_t RTCZero::getAlarmAM_PM()
201+
{
202+
uint8_t result = RTC_AM_PM::RTC_AM;
203+
204+
if (__time24) {
205+
if (RTC->MODE2.Mode2Alarm[0].ALARM.bit.HOUR > 11) {
206+
result = RTC_AM_PM::RTC_PM;
207+
}
208+
}
209+
else {
210+
if (RTC->MODE2.Mode2Alarm[0].ALARM.bit.HOUR & RTC_MODE2_CLOCK_HOUR_PM_Val) {
211+
result = RTC_AM_PM::RTC_PM;
212+
}
213+
}
214+
215+
return result;
167216
}
168217

169218
uint8_t RTCZero::getAlarmDay()
@@ -199,22 +248,29 @@ void RTCZero::setMinutes(uint8_t minutes)
199248
;
200249
}
201250

202-
void RTCZero::setHours(uint8_t hours)
251+
void RTCZero::setHours(uint8_t hours, uint8_t am_pm)
203252
{
204-
if ((__time24) || (hours < 13)) {
205-
RTC->MODE2.CLOCK.bit.HOUR = hours;
206-
} else {
207-
RTC->MODE2.CLOCK.bit.HOUR = hours - 12;
253+
if (!__time24)
254+
{
255+
if (hours > 12) {
256+
hours -= 12;
257+
hours |= RTC_MODE2_CLOCK_HOUR_PM_Val;
258+
}
259+
else {
260+
hours |= (am_pm << 4);
261+
}
208262
}
263+
264+
RTC->MODE2.CLOCK.bit.HOUR = hours;
209265
while (RTCisSyncing())
210266
;
211267
}
212268

213-
void RTCZero::setTime(uint8_t hours, uint8_t minutes, uint8_t seconds)
269+
void RTCZero::setTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint8_t am_pm)
214270
{
215271
setSeconds(seconds);
216272
setMinutes(minutes);
217-
setHours(hours);
273+
setHours(hours, am_pm);
218274
}
219275

220276
void RTCZero::setDay(uint8_t day)
@@ -259,23 +315,29 @@ void RTCZero::setAlarmMinutes(uint8_t minutes)
259315
;
260316
}
261317

262-
void RTCZero::setAlarmHours(uint8_t hours)
318+
void RTCZero::setAlarmHours(uint8_t hours, uint8_t am_pm)
263319
{
264-
if ((__time24) || (hours < 13)) {
265-
RTC->MODE2.Mode2Alarm[0].ALARM.bit.HOUR = hours;
266-
}
267-
else {
268-
RTC->MODE2.Mode2Alarm[0].ALARM.bit.HOUR = hours - 12;
320+
if (!__time24)
321+
{
322+
if (hours > 12) {
323+
hours -= 12;
324+
hours |= RTC_MODE2_CLOCK_HOUR_PM_Val;
325+
}
326+
else {
327+
hours |= (am_pm << 4);
328+
}
269329
}
330+
331+
RTC->MODE2.Mode2Alarm[0].ALARM.bit.HOUR = hours;
270332
while (RTCisSyncing())
271333
;
272334
}
273335

274-
void RTCZero::setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds)
336+
void RTCZero::setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint8_t am_pm)
275337
{
276338
setAlarmSeconds(seconds);
277339
setAlarmMinutes(minutes);
278-
setAlarmHours(hours);
340+
setAlarmHours(hours, am_pm);
279341
}
280342

281343
void RTCZero::setAlarmDay(uint8_t day)
@@ -327,7 +389,21 @@ uint32_t RTCZero::getY2kEpoch()
327389

328390
days += 365 * years + (years + 3) / 4 - 1;
329391

330-
return ((days * 24 + RTC->MODE2.CLOCK.bit.HOUR) * 60 +
392+
uint8_t hours = RTC->MODE2.CLOCK.bit.HOUR;
393+
394+
if (!__time24) {
395+
uint8_t pm = hours & RTC_MODE2_CLOCK_HOUR_PM_Val;
396+
hours &= ~RTC_MODE2_CLOCK_HOUR_PM_Val;
397+
398+
if ((!pm) && (hours == 12)) {
399+
hours = 0;
400+
}
401+
else if ((pm) && (hours != 12)) {
402+
hours += 12;
403+
}
404+
}
405+
406+
return ((days * 24 + hours) * 60 +
331407
RTC->MODE2.CLOCK.bit.MINUTE) * 60 + RTC->MODE2.CLOCK.bit.SECOND;
332408
}
333409

src/RTCZero.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ typedef void(*voidFuncPtr)(void);
3030
class RTCZero {
3131
public:
3232

33+
enum RTC_AM_PM : uint8_t
34+
{
35+
RTC_AM = 0,
36+
RTC_PM = 1
37+
};
38+
3339
enum Alarm_Match: uint8_t // Should we have this enum or just use the identifiers from /component/rtc.h ?
3440
{
3541
MATCH_OFF = RTC_MODE2_MASK_SEL_OFF_Val, // Never
@@ -55,14 +61,16 @@ class RTCZero {
5561
uint8_t getSeconds();
5662
uint8_t getMinutes();
5763
uint8_t getHours();
64+
uint8_t getAM_PM();
5865

5966
uint8_t getDay();
6067
uint8_t getMonth();
6168
uint8_t getYear();
62-
69+
6370
uint8_t getAlarmSeconds();
6471
uint8_t getAlarmMinutes();
6572
uint8_t getAlarmHours();
73+
uint8_t getAlarmAM_PM();
6674

6775
uint8_t getAlarmDay();
6876
uint8_t getAlarmMonth();
@@ -72,8 +80,8 @@ class RTCZero {
7280

7381
void setSeconds(uint8_t seconds);
7482
void setMinutes(uint8_t minutes);
75-
void setHours(uint8_t hours);
76-
void setTime(uint8_t hours, uint8_t minutes, uint8_t seconds);
83+
void setHours(uint8_t hours, uint8_t am_pm = RTC_AM);
84+
void setTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint8_t am_pm = RTC_AM);
7785

7886
void setDay(uint8_t day);
7987
void setMonth(uint8_t month);
@@ -82,8 +90,8 @@ class RTCZero {
8290

8391
void setAlarmSeconds(uint8_t seconds);
8492
void setAlarmMinutes(uint8_t minutes);
85-
void setAlarmHours(uint8_t hours);
86-
void setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds);
93+
void setAlarmHours(uint8_t hours, uint8_t am_pm = RTC_AM);
94+
void setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint8_t am_pm = RTC_AM);
8795

8896
void setAlarmDay(uint8_t day);
8997
void setAlarmMonth(uint8_t month);

0 commit comments

Comments
 (0)