1

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.

dan_wut
  • 45
  • 1
  • 10
  • 1
    Possible duplicate of [HAL\_SetDate sets the year to wrong value](https://stackoverflow.com/questions/49264383/hal-setdate-sets-the-year-to-wrong-value) – Cindy Meister Jan 17 '19 at 19:39

3 Answers3

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.

dan_wut
  • 45
  • 1
  • 10
0

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.

A.R.C.
  • 910
  • 11
  • 22
  • 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? – A.R.C. Sep 19 '18 at 09:12
  • 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 – dan_wut Sep 19 '18 at 09:29
  • 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? – A.R.C. Sep 19 '18 at 09:39
  • 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 – dan_wut Sep 19 '18 at 09:43
  • Look at this thread, they seem to have the same problem: [link](https://stackoverflow.com/questions/49264383/hal-setdate-sets-the-year-to-wrong-value) – A.R.C. Sep 19 '18 at 09:57
  • Hi, I will take a look at it. Thanks for your help so far! – dan_wut Sep 20 '18 at 02:12
0

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

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37