Still, the fix is pointingI want to my original suspicion, that therehelp others to avoid sleepless nights when debugging this - I had to wait repeated hours to debug the hourly alarm.
There is aan apparent bug in the PIC RTCC module, which showssneaks up in a special case you need anany alarm at rounded time of xx:xx:00 seconds.
It It seems that both PIC documentation and Errata are wrong.
Normally you would think to set the ALRMSEC register to 0x00. Errata says to fix the bug, set it to 0x0A. Unfortunately, neither works.
The solution is to set the SECONDS alarm register to 'weird' value of 0x5A. Then the code works, no matter if
Below I want every 1-minute or every 1-hour wake upsgive few examples clarifying this. Remember these basic steps before setting an alarm:
- enable writing to RTC via RTCWREN register
- disable alarms via ALRMEN register
- set how many alarms you need via ALRMRPT register (if infinite, set it to 0xFF and later you must also set the CHIME bit in ALRMCON register)
- set the desired alarm time in such a way, that minutes are 1_second_before_the_alarm, and the seconds are '1_second_after_the_alarm' (see below examples)
- set the alarm desired frequency in ALRMCON register (upper bit must be set to enable the alarms)
- avoid writing to RTC via RTCWREN register
- clear interrupt flag before enabling interrupts
This single line made it workingThe 'secret' of the solution is in setting the value of the ALRMMIN and ALRMSEC registers:
ALRMSEC = 0x5A; // seconds *
//* errorError in both MCU and ERRATA docsdocuments:
(if* alarmIf needsthe toalarm shall occur at :00 seconds -> ErrataERRATA saidasks for 0x0A, but needsit has to be 0x5A!
*
* EXAMPLES:
* For an alarm every minute on the minute, write xx:xx:5A (HRS/MIN/SEC) // wakes at xx:00:00, xx:01:00, xx:02:00, xx:03:00, ...
* For an alarm every 10-minutes, write xx:09:5A (HRS/MIN/SEC) // wakes at xx:00:00, xx:10:00, xx:20:00, xx:30:00, ...
* For an alarm every hour on the hour, write xx:59:5A (HRS/MIN/SEC) // wakes at x0:00:00, x1:00:00, x2:00:00, x3:00:00, ...
* For an alarm every hour at 01-minute, write xx:00:5A (HRS/MIN/SEC) // wakes at x0:01:00, x1:01:00, x2:01:00, x3:01:00, ...
*
* 0x5A is in BCD as five-ten (5-10), replacing the expected 5-9 (decimal "59" on the clock)
* Likely the rollover mechanism in the PIC has this simple bug, that to get the rollover impulse the last digit needs ten instead of nine.
*/
ALRMMIN = 0x59; // minutes
ALRMSEC = 0x5A; // seconds
/*
* Alarm control register
*/
ALRMCON = ALRMCTRL__REPEAT_INDEFINITELY | ALRMCTRL__EVERY_HOUR;
/*
* Enable the alarm
*/
ALRMCONbits.ALRMEN = 1;