STM32F051 HAL_RTC_SetDate not setting Year correctly

stm32 rtc example code hal
stm32 rtc calendar example
stm32cubemx rtc
stm32f4 rtc example code
stm32f103 rtc alarm example

So I am using a smart phone application to set the Time and Date of the RTC on the micro-controller. The RTC runs on LSE.

The formatting of the string that I sent over to the micro-controller is correct. I can be certain of that when I set the relevant variables on "Watch 1". I am using Keil uVision 5.

/** 
  * @brief  RTC Date structure definition  
  */
typedef struct
{
  uint8_t WeekDay;  /*!< Specifies the RTC Date WeekDay.
                         This parameter can be a value of @ref RTC_WeekDay_Definitions */

  uint8_t Month;    /*!< Specifies the RTC Date Month (in BCD format).
                         This parameter can be a value of @ref RTC_Month_Date_Definitions */

  uint8_t Date;     /*!< Specifies the RTC Date.
                         This parameter must be a number between Min_Data = 1 and Max_Data = 31 */

  uint8_t Year;     /*!< Specifies the RTC Date Year.
                         This parameter must be a number between Min_Data = 0 and Max_Data = 99 */

}RTC_DateTypeDef;

So my command that I sent over actually sets the DD/MM/YY and of course the time as well but I have no issues with time.

After setting it and then calling HAL_RTC_GetDate and HAL_RTC_GetTime, everything is correct except for the Year attribute.

My command that I send over will always be year 2018 however the GetTime function will always return a higher and random value such as 24, 22, 21 and 19. Occasionally, after sending a few times of the same command but with different mins and seconds, the Year will go back to 18...

What could be the issue? Also, must I set the WeekDay parameter since I only set the Day Month and Year.

Thank you!

*I have a function that will help me take the last two digit of the year 2018 which I send over and it will push the value of 18 to the micro-controller.

Look at the definition of your struct. The year is uint8_t only so maximum is 255. If you read the comment behind the definition, the year is limeted to be between 0 and 99.

Best thing is writing just 18 instead of 2018 to your RTC.

stm32, So I am using a smart phone application to set the Time and Date of the RTC on the micro-controller. The RTC runs on LSE. The formatting of  STM32F051 HAL_RTC_SetDate not setting Year correctly. So I am using a smart phone application to set the Time and Date of the RTC on the micro-controller. The RTC runs on LSE. The formatting of the string that I sent over to the micro-controller is correct. I can be certain of that when I set the relevant variables on "Watch 1".

So I've decided to try setting the WeekDay parameter by hardcoding it first. So I sent the exact same command of setting the Date and Time with Date in DD/MM/YY format to the micro-controller and my read back values are correct so far. I followed this link given by A.Rech:

HAL_SetDate sets the year to wrong value

Even with repeated sending of the same command with the same DD/MM/YY but with different time, my read back values from HAL_RTC_GetDate() is correct so far! I hope this would be the case and not just coincidence.

Thanks!

*Just to add on. Prior to hardcoding the WeekDay param. Whenever I send the command with DD/MM/YY to the micrco-controller multiple times with different Time values set while the DD/MM/YY the same. The read back value of the WeekDay param will be a random number, in my case a single digit number not more than the value of 7.

STM32F051 HAL_RTC_SetDate not setting Year correctly, After setting it and then calling HAL_RTC_GetDate and HAL_RTC_GetTime, everything is correct except for the Year attribute. My command that I send over will  However the year is wrong. As seen on the below picture it is showing the value 46 in the Year field. Below is the two methods i am using to get date and time and then print it out. I get the year value from the Year field of the tmElements struct. The tmElements type is residing in the Time library.

I had the same problem. I found that the problem was not setting the WeekDay (when creating a struct RTC_DateTypeDef, the field WeekDay gets a random value). The value WeekDay must be set to a value between 0 to 7.

See my full answer for a similar question with explanation: https://stackoverflow.com/a/54236587/10927863

Hi, is there a problem with the HAL_RTC_SetDate function? If i set , If i set the year to 18, and read it back its never correct, always a year infront HAL_RTC_SetDate(&RtcHandle, &sDate, RTC_FORMAT_BIN);. Have you verified that your alarm mask is actually set correctly? Even with an incorrect alarm mask, you might get the first alarm because the default settings will cause the alarm to trigger (year=0, month=0, day=0, day_of_week_=0, hours=0, minutes=0, seconds=0, mask=all_must_match) because the RTC happens to start with that exact date/time.

HAL RTC SetDate Bug or some another way to do??, when i inspected the code i found that the HAL_RTC_SetDate() function is not setting the RTC_CNTH and RTC_CNTL registers appropriately. Is  month, year). The STM32 RTC calendar offers several features to easily configure and display the calendar data fields: Calendar with: – sub-seconds (not programmable) – seconds – minutes – hours in 12-hour or 24-hour format – day of the week (day) – day of the month (date) –month – year Calendar in binary-coded decimal (BCD) format

Questions for tag real-time-clock, Qwiic Real Time Clock Module not working when changing I2C pinout · Realtime CPU clock vs STM32F051 HAL_RTC_SetDate not setting Year correctly. When you set this new library, you have to set date and time again, or your settings will not be valid! You can not set subseconds, you can just read it When you update your clock, subsecond is set to reload value = SYNC value; Set date and time with string. Sometimes, useful function will be to set date and time using string format.

User A.R.C., 0 STM32 SPI DMA transferring at external interrupt Sep 19 '18. 0 STM32F051 HAL_RTC_SetDate not setting Year correctly Sep 19 '18. View all questions and​  The solution is to call both GetTime and GetDate at the same time, If you call only GetTime it does not update correctly: HAL_RTC_GetTime(&hrtc, &currTime, RTC_FORMAT_BIN); HAL_RTC_GetDate(&hrtc, &currDate, RTC_FORMAT_BIN); I don't know how they are connected, even more so because GetDate() is run after GetTime() and it still makes difference.

Comments
  • Possible duplicate of HAL_SetDate sets the year to wrong value
  • Just saw your command is DD/MM/YY so this would be ok. Later on you have written 2018, what did you write to the RTC?
  • Hello, I did adhere to the format that they needed. //TIME=DD-MM-YY,HH:mm:ss The above is the format which I send my command over
  • Have you set the time using HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)? What format did you pass to the function? Same as with GetTime?
  • Yes, I always run HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) and then HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) for that function that I am using. In the same function I have also assigned values to: sDate.Date sDate.Month sDate.Year sTime.Hours sTime.Minutes sTime.Seconds
  • Look at this thread, they seem to have the same problem: link