cancel
Showing results for 
Search instead for 
Did you mean: 

RTC not rolling over at midnight

Dabbo
Associate III

Using the RTC on a NUCLEO-G071RB (HAL) I have a program the prints out the time, date and temperature in degrees Centigrade. I left it running overnight and discovered in the morning that the hours had not reset to 00:00 at midnight and neither had the date rolled forward by a day.
I'm not experienced in embedded systems, in fact this is my first attempt at programming any MCU, but I thought this rollover was implemented in the HAL library.
I'm using functions HAL_RTC_SetTime & HAL_RTC_SetDate to initialise the clock to 10 seconds before midnight) and then HAL_RTC_GetTime and HAL_RTC_GetDate before printing to the serial monitor.

I'm not asking to fix my code (attached) but would appreciate help in knowing where to look. Thanks

1 ACCEPTED SOLUTION

Accepted Solutions

Always start debugging with reading out and checking the relevant peripheral registers, here, RTC registers.

You'll find that you have RTC_CR.FMT set, i.e. RTC is in AM/PM (12-hour) mode, as a consequence of using uninitialized local variable as an initialization struct when calling Cube/HAL functions. Here, it's unitialized fields of hrtc in calling HAL_RTC_Init() (it's a weird call, as you've already called it in MX_RTC_Init()).

JW

View solution in original post

3 REPLIES 3

Always start debugging with reading out and checking the relevant peripheral registers, here, RTC registers.

You'll find that you have RTC_CR.FMT set, i.e. RTC is in AM/PM (12-hour) mode, as a consequence of using uninitialized local variable as an initialization struct when calling Cube/HAL functions. Here, it's unitialized fields of hrtc in calling HAL_RTC_Init() (it's a weird call, as you've already called it in MX_RTC_Init()).

JW

Dabbo
Associate III

Thanks Jan, that seems to have solved the problem. I initialised my gtime and gdate structs to {0} and now it works.

Hi Dabbo,

I was talking about the hrtc struct, defined locally in main().

JW