Skip to main content
dscdsc
Associate III
March 20, 2015
Question

Using the RTC

  • March 20, 2015
  • 16 replies
  • 3367 views
Posted on March 20, 2015 at 14:12

I'm having issues making the RTC work as expected.

This is the code which is called periodically:

RTC_TimeTypeDef currentTime;
HAL_RTC_GetTime(&hrtc, &currentTime, FORMAT_BIN);
char
uartMsg[32] = 
''Time: ''
;
char
time[16] = {0};
sprintf(time, 
''%d:%d:%d %d\n''
, currentTime.Hours, currentTime.Minutes, currentTime.Seconds, currentTime.SubSeconds);
strcat(uartMsg, time);
HAL_UART_Transmit(&huart1, (uint8_t *)uartMsg, strlen(uartMsg), 10000);

The output is always something like this:

Time: 0:0:0 29
Time: 0:0:0 27
Time: 0:0:0 25
Time: 0:0:0 24

With hours, minutes and seconds staying at 0, while sub-seconds oscillate periodically between 0 and RTC initialization code generated by CubeMX:

void
MX_RTC_Init(
void
)
{
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
RTC_AlarmTypeDef sAlarm;
/* Initialize RTC and set the Time and Date */
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 127;
hrtc.Init.SynchPrediv = 255;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
HAL_RTC_Init(&hrtc);
sTime.Hours = 0;
sTime.Minutes = 0;
sTime.Seconds = 0;
sTime.SubSeconds = 0;
sTime.TimeFormat = RTC_HOURFORMAT12_AM;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BIN);
sDate.WeekDay = RTC_WEEKDAY_MONDAY;
sDate.Month = RTC_MONTH_JANUARY;
sDate.Date = 1;
sDate.Year = 0;
HAL_RTC_SetDate(&hrtc, &sDate, FORMAT_BIN);
/* Enable the Alarm A */
sAlarm.AlarmTime.Hours = 0;
sAlarm.AlarmTime.Minutes = 0;
sAlarm.AlarmTime.Seconds = 0;
sAlarm.AlarmTime.SubSeconds = 0;
sAlarm.AlarmTime.TimeFormat = RTC_HOURFORMAT12_AM;
sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
sAlarm.AlarmMask = RTC_ALARMMASK_NONE;
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
sAlarm.AlarmDateWeekDay = 1;
sAlarm.Alarm = RTC_ALARM_A;
HAL_RTC_SetAlarm(&hrtc, &sAlarm, FORMAT_BIN);
}

With different values of

AsynchPrediv

and

SynchPrediv, hours, minutes and seconds are set to different initial values (not all 0's as by default), but they never change with time not matter what I try.
 
 I'm using STM32F051 and RTC is clocked by 40kHz LSI.
 
 What's the problem?
 

#rtc #stm32
This topic has been closed for replies.

16 replies

Tesla DeLorean
Guru
April 17, 2015
Posted on April 17, 2015 at 13:37

STM32F0xx_StdPeriph_Lib_V1.4.0\Projects\STM32F0xx_StdPeriph_Examples\RTC\RTC_StopWatch\main.c

There are a bunch of RTC examples. There use might be impaired on the DISCO boards as they typically don't have the LSE crystal and support capacitors placed.

http://www.st.com/web/en/catalog/tools/PF257884

http://www.st.com/web/en/catalog/tools/PF260157

http://www.st.com/web/en/catalog/tools/PF257885

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
schubertjw
Associate
April 17, 2015
Posted on April 17, 2015 at 21:09

Thanks...

I downloded the all examples and look over them. Further I tryed to write over for my ruitn of LCD (HD44780). Unfortunatelly I have some problems....

So I would like to ask what command can I use without 

RTC_GetCounter().

Best regards,

JS

Tesla DeLorean
Guru
April 17, 2015
Posted on April 17, 2015 at 21:25

It's a calendering RTC, you don't get the 32-bit counter like the F1 series. You have to read the date/time.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
schubertjw
Associate
April 18, 2015
Posted on April 18, 2015 at 11:05

Hello,

Thanks so much for your helping. My RTC is working. I have written over the example from 

http://www.st.com/web/en/catalog/tools/PF260157#

Have a good day....

JS

hendry putra rahadi
Associate II
November 10, 2017
Posted on November 10, 2017 at 09:21

@note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values

* in the higher-order calendar shadow registers to ensure consistency between the time and date values.

* Reading RTC current time locks the values in calendar shadow registers until current date is read.

copy from stm32f4xx_hal_rtc.c , line number : 570

ERRAOUI Abdelhakim
Associate II
November 20, 2017
Posted on November 20, 2017 at 11:25

hendryputrarahadi

, is right in order to update the time you have to call the GetDate function ! then call the GetTime function

Hope this helps