2015-03-20 06:12 AM
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, ¤tTime, 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
2015-04-17 04:19 AM
Dear JW,
Thanks veray much for your answer. I thought that this conversation is dead. But as I wrote I am a beginner in stm32f0 programing. This is unlike as PIC programming. The CORTEX-M0 has more hhance to set it. So I tryed to write over stm32f1 RTC program to stm32f0.( ) It not was succes. For example I do not know what is theRTC_GetCounter()
in stm32f0 programming.
S
o this was why I asked a running rtc drive for stm32f0. Please help me if it is possible.Best regards,JS2015-04-17 04:37 AM
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
2015-04-17 12:09 PM
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 withoutRTC_GetCounter().
Best regards,JS2015-04-17 12:25 PM
It's a calendering RTC, you don't get the 32-bit counter like the F1 series. You have to read the date/time.
2015-04-18 02:05 AM
Hello,
Thanks so much for your helping. My RTC is working. I have written over the example from . Have a good day....JS2017-11-10 12:21 AM
@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
2017-11-20 02:25 AM
hendryputrarahadi
, is right in order to update the time you have to call the GetDate function ! then call the GetTime function
Hope this helps