0
votes

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 field.

My command that I send over will always be year 2018 however the HAL_RTC_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.

3

3 Answers

0
votes

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.

0
votes

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

0
votes

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.