1. 程式人生 > 其它 >STM32 HAL庫讀取RTC時鐘一直不更新時間的問題

STM32 HAL庫讀取RTC時鐘一直不更新時間的問題

技術標籤:STM32

專案場景:

上傳至伺服器的資料裡面帶有時間戳,通過後臺發現RTC的時間一直為一個時間,通過讀取HAL庫的原始碼後,修復了該問題。


問題描述:

呼叫STM32 HAL庫中的RTC日期、時間獲取函式,發現時間一直為同一個時間。

/*******************************************************************************
** 函式原型:RTC_TimeTypeDef RTC_Time_Get(void)
** 函式功能:獲取前RTC時間
** 輸入引數:無
** 輸出引數:時間
** 備  注:
*******************************************************************************/
RTC_TimeTypeDef RTC_Time_Get(void)
{
 RTC_TimeTypeDef stimestructureget;
 HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);

 return stimestructureget;
}
/******************************************************************************* ** 函式原型:RTC_DateTypeDef RTC_Date_Get(void) ** 函式功能:獲取前RTC日期 ** 輸入引數:無 ** 輸出引數:日期 ** 備 注: *******************************************************************************/ RTC_DateTypeDef RTC_Date_Get(void) { RTC_DateTypeDef sdatestructureget; HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN); return sdatestructureget; } //主函式讀取 void main(void) { uint8_t buff[10]; buff[0] = RTC_Date_Get().Month; //當前時間 buff[1] = RTC_Date_Get().Date; buff[2] = RTC_Time_Get().Hours; buff[3] = RTC_Time_Get().Minutes; buff[4] = RTC_Time_Get().Seconds; }

原因分析:

通過閱讀HAL庫的 RTC原始碼,發現HAL_RTC_GetTime()的解釋:

/**
* @brief Get RTC current time.
* @note You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
* value in second fraction ratio with time unit following generic formula:
* Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit

* This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
* @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
* in the higher-order calendar shadow registers to ensure consistency between the time and date values.
* Reading RTC current time locks the values in calendar shadow registers until Current date is read
* to ensure consistency between the time and date values.

* @param hrtc RTC handle
* @param sTime Pointer to Time structure with Hours, Minutes and Seconds fields returned
* with input format (BIN or BCD), also SubSeconds field returning the
* RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
* factor to be used for second fraction ratio computation.
* @param Format Specifies the format of the entered parameters.
* This parameter can be one of the following values:
* @arg RTC_FORMAT_BIN: Binary data format
* @arg RTC_FORMAT_BCD: BCD data format
* @retval HAL status
*/

即必須在HAL_RTC_GetTime()之後呼叫HAL_RTC_GetDate()來解鎖高階日曆陰影暫存器中的值,以確保時間和日期值之間的一致性,否則會被上鎖。這和我們平常讀日期的順序不一樣,先讀年月日,在讀時間,STM32則相反,先讀時間,在讀日期。


解決方案:

先呼叫讀時間函式,再呼叫讀日期函式,即可解決問題。

void main(void)
{
   uint8_t buff[10];
  
 buff[2] = RTC_Time_Get().Hours;
 buff[3] = RTC_Time_Get().Minutes;
 buff[4] = RTC_Time_Get().Seconds;

  buff[0] = RTC_Date_Get().Month;         //當前時間
 buff[1] = RTC_Date_Get().Date;
}