RTC时间更新

首先,需要对RTC进行初始化,初始化的详解介绍见链接:RTC

static void MX_RTC_Init(void)
{
  RTC_TimeTypeDef sTime;
  RTC_DateTypeDef sDate;
  RTC_AlarmTypeDef sAlarm;

    /**Initialize RTC Only 
    */
  hrtc.Instance = RTC;
  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  hrtc.Init.AsynchPrediv = 39;
  hrtc.Init.SynchPrediv = 799;//本芯片采用内部32K时钟源,32k/(39+1)(799+1)=1HZ
  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) != 0x32F6)
    {
        sTime.Hours = 0x01;
        sTime.Minutes = 0x01;
        sTime.Seconds = 0x01;
        sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
        sTime.StoreOperation = RTC_STOREOPERATION_RESET;
        if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
        {
            _Error_Handler(__FILE__, __LINE__);
        }

        sDate.WeekDay = RTC_WEEKDAY_FRIDAY;
        sDate.Month = RTC_MONTH_JANUARY;
        sDate.Date = 0x01;
        sDate.Year = 0x18;

        if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
        {
            _Error_Handler(__FILE__, __LINE__);
        }

    HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F6);
  }
 }

下面是在获取了网络时间后,对RTC进行更新的代码。

//  RTC更新
void RTC_Update(void)
{
  RTC_DateTypeDef sdatestructure;
  RTC_TimeTypeDef stimestructure;

    /*##-1- Configure the Date #################################################*/
    sdatestructure.Year =  Net_time->tm_year+1900-2000;
    sdatestructure.Month =  Net_time->tm_mon+1;
    sdatestructure.Date = Net_time->tm_mday;
    sdatestructure.WeekDay = Net_time->tm_wday;

    if (HAL_RTC_SetDate(&hrtc, &sdatestructure, RTC_FORMAT_BIN) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }

    /*##-2- Configure the Time #################################################*/
    stimestructure.Hours =  Net_time->tm_hour;
    stimestructure.Minutes =  Net_time->tm_min;
    stimestructure.Seconds = Net_time->tm_sec;
  stimestructure.TimeFormat = RTC_HOURFORMAT12_AM;
  stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ;
  stimestructure.StoreOperation = RTC_STOREOPERATION_RESET;

    if (HAL_RTC_SetTime(&hrtc, &stimestructure, RTC_FORMAT_BIN) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }
    HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F6);
}

接着是获取RTC时间并打印出来代码。

void RTC_Show(void)
{
  RTC_DateTypeDef sdate_get;
  RTC_TimeTypeDef stime_get;

    /* Get the RTC current Date */
  HAL_RTC_GetDate(&hrtc, &sdate_get, RTC_FORMAT_BIN);
  /* Get the RTC current Time */
  HAL_RTC_GetTime(&hrtc, &stime_get, RTC_FORMAT_BIN);

    printf("%d-%02d-%02d   %02d:%02d:%02d\r\n",2000 + sdate_get.Year, sdate_get.Month, sdate_get.Date,\
                                      stime_get.Hours,stime_get.Minutes,stime_get.Seconds);

}

主程序中调用NTP函数获取时间、然后更新RTC,接着打印出更新之后的RTC时间即可。下图是从NTP获取时间后间隔5秒打印出RTC时间的效果。
RTC时间更新_第1张图片

NTP实例的源码:https://download.csdn.net/download/u014470361/10234803

你可能感兴趣的:(NTP协议)