AnsweredAssumed Answered

Cant seem to get RTC operating.

Question asked by Scott Dev on Aug 4, 2017
Latest reply on Aug 5, 2017 by waclawek.jan

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 ofcourse
HAL_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);
  }

Outcomes