2017-08-04 07:43 AM
Hi
Im still new to the STM32, and now working on the RTC. The problem I am having is when I read the time from the RTC, it never changes. I Initialise the RTC, and every second I read from the rtc and display its values. I expected to see the time changing every second , but always get the same values back. Anyone please let me know what I am doing wrong?
Many Thanks
Scott
MX_RTC_Init(); //below
RTC_TimeTypeDef sTime;
__HAL_RCC_RTC_ENABLE(); while (1) //main loop {if(Second)
{
Second=0;
HAL_RTC_GetTime( &hrtc, &sTime, RTC_FORMAT_BIN );
//display value of the rtc time
}
}
/////////////////////////////////////////////
static void MX_RTC_Init(void)
{RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate; RTC_DateTypeDef dt;RTC_TimeTypeDef tt; 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); HAL_RTC_SetTime(&hrtc, &tt, RTC_FORMAT_BIN);// or bcd ofcourseHAL_RTC_SetDate(&hrtc,&dt, RTC_FORMAT_BIN);// or bcd ofcourse /**Initialize RTC Only */ 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; if (HAL_RTC_Init(&hrtc) != HAL_OK) { // _Error_Handler(__FILE__, __LINE__); }/**Initialize RTC and set the Time and Date
*/ if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2) { sTime.Hours = 0; sTime.Minutes = 0; sTime.Seconds = 0; sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; sTime.StoreOperation = RTC_STOREOPERATION_SET; if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK) { // _Error_Handler(__FILE__, __LINE__); }sDate.WeekDay = RTC_WEEKDAY_TUESDAY;
sDate.Month = RTC_MONTH_JULY; sDate.Date = 9; sDate.Year = 17;if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
{// _Error_Handler(__FILE__, __LINE__); }HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);
}2017-08-04 08:51 AM
Hello!
You would help us(and you also) if you provide more details about your hardware .
Do you use a crystal for LSE ? VBAT if exists, is connected to a power source?
If you use CubeMX , please provide to the comunity your.IOC file.
2017-08-04 10:03 AM
Hello again !!
__HAL_RCC_RTC_ENABLE(); must be called prior to initialise the rtc .
2017-08-04 10:58 AM
Hi
Sorry, I should have mentioned these things.
I am using CubeMX, and processor STm32L073 on a nucleo L073 board. I only have the 32Kz crystal on board. Dont know why it would matter if VBatt was the source as wouldnt effect the rtc. But anyway, just using the usb cable
regards
Scott
2017-08-04 11:16 AM
>>
Dont know why it would matter if VBatt was the source as wouldnt effect the rtc.
LSE is in the low power domain, of import if core enters sleep or standby.
2017-08-04 11:31 AM
Ah, I see. As I am just trying to get used to all the different modules one at a time, I am just concentrating on being powered all the time, and will later look at sleep,standby at a later stage. Now it makes sense to me why the VBATT was asked, sorry about that.
Thanks
Scott
2017-08-04 12:31 PM
Hi Vangelis
Thanks for your reply.
I moved it to just before the initialisation of the rtc, but still the same. I found some documentation where it says RTC has to be disabled first. Values 0xca then 0x53 has to be written to the RTC_WPR register, then after initialisation clear the INIT bit in the RTC_ISR . I am not sure if any of the commands above does this. Also, Im not sure what the above commands does (I used someones else post of the code to test)
HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2
HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);
I am assuming its the backup registers, but not sure exactly what they do.
Thanks
Scott
2017-08-04 12:56 PM
Hi Scott!
At least, check with your debug functionality the RTC registers.
I think you need
from ST to study a lot of examples. (RTC and others)You can also download
to produce automated initialization code for your projects.Rgrds
2017-08-04 08:58 PM
JW