@@ -57,10 +57,11 @@ void RTCZero::begin(bool timeRep)
57
57
tmp_reg |= RTC_MODE2_CTRL_PRESCALER_DIV1024; // set prescaler to 1024 for MODE2
58
58
tmp_reg &= ~RTC_MODE2_CTRL_MATCHCLR; // disable clear on match
59
59
60
+ // According to the datasheet RTC_MODE2_CTRL_CLKREP = 0 for 24h
60
61
if (timeRep)
61
- tmp_reg |= RTC_MODE2_CTRL_CLKREP; // 24h time representation
62
+ tmp_reg &= ~ RTC_MODE2_CTRL_CLKREP; // 24h time representation
62
63
else
63
- tmp_reg &= ~ RTC_MODE2_CTRL_CLKREP; // 12h time representation
64
+ tmp_reg |= RTC_MODE2_CTRL_CLKREP; // 12h time representation
64
65
65
66
RTC->MODE2 .READREQ .reg &= ~RTC_READREQ_RCONT; // disable continuously mode
66
67
@@ -133,7 +134,31 @@ uint8_t RTCZero::getMinutes()
133
134
134
135
uint8_t RTCZero::getHours ()
135
136
{
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;
137
162
}
138
163
139
164
uint8_t RTCZero::getDay ()
@@ -163,7 +188,31 @@ uint8_t RTCZero::getAlarmMinutes()
163
188
164
189
uint8_t RTCZero::getAlarmHours ()
165
190
{
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;
167
216
}
168
217
169
218
uint8_t RTCZero::getAlarmDay ()
@@ -199,22 +248,29 @@ void RTCZero::setMinutes(uint8_t minutes)
199
248
;
200
249
}
201
250
202
- void RTCZero::setHours (uint8_t hours)
251
+ void RTCZero::setHours (uint8_t hours, uint8_t am_pm )
203
252
{
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
+ }
208
262
}
263
+
264
+ RTC->MODE2 .CLOCK .bit .HOUR = hours;
209
265
while (RTCisSyncing ())
210
266
;
211
267
}
212
268
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 )
214
270
{
215
271
setSeconds (seconds);
216
272
setMinutes (minutes);
217
- setHours (hours);
273
+ setHours (hours, am_pm );
218
274
}
219
275
220
276
void RTCZero::setDay (uint8_t day)
@@ -259,23 +315,29 @@ void RTCZero::setAlarmMinutes(uint8_t minutes)
259
315
;
260
316
}
261
317
262
- void RTCZero::setAlarmHours (uint8_t hours)
318
+ void RTCZero::setAlarmHours (uint8_t hours, uint8_t am_pm )
263
319
{
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
+ }
269
329
}
330
+
331
+ RTC->MODE2 .Mode2Alarm [0 ].ALARM .bit .HOUR = hours;
270
332
while (RTCisSyncing ())
271
333
;
272
334
}
273
335
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 )
275
337
{
276
338
setAlarmSeconds (seconds);
277
339
setAlarmMinutes (minutes);
278
- setAlarmHours (hours);
340
+ setAlarmHours (hours, am_pm );
279
341
}
280
342
281
343
void RTCZero::setAlarmDay (uint8_t day)
@@ -327,7 +389,21 @@ uint32_t RTCZero::getY2kEpoch()
327
389
328
390
days += 365 * years + (years + 3 ) / 4 - 1 ;
329
391
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 +
331
407
RTC->MODE2 .CLOCK .bit .MINUTE ) * 60 + RTC->MODE2 .CLOCK .bit .SECOND ;
332
408
}
333
409
0 commit comments