STM32 RTC

STM32 的实时时钟(RTC)是一个独立的定时器。STM32 的 RTC 模块拥有一组连续计数的计数器,在相应软件配置下,可提供时钟日历的功能。修改计数器的值可以重新设置系统当前的时间和日期。

共有三个时钟源,其最终目的是产生1Hz的脉冲,周期为1秒。

 

HAL库应用API

要设置时间和日期可使用

/**
  * @brief  Set RTC current time.
  * @param  hrtc RTC handle
  * @param  sTime Pointer to Time structure
  * @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_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)

/**
  * @brief  Set RTC current date.
  * @param  hrtc RTC handle
  * @param  sDate Pointer to date structure
  * @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_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)

其中,stime为写入的时间,sDate为写入的日期。

若想得到时间与日期可使用

/**
  * @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(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)

/**
  * @brief  Get RTC current date.
  * @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.
  * @param  hrtc RTC handle
  * @param  sDate Pointer to Date structure
  * @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_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)

其中,函数将结果存储在sTime和sDate中。

 

应用笔记:

如图所示选择外部高速时钟,经过32分频后为750khz,要想得到1Hz的脉冲,需要配置RTC的异步预分值(Asynchronous Predivider value:0~255)和同步预分值(Synchronous Predivider value:0~32767)。

代码部分:

/*声明变量*/
RTC_TimeTypeDef rtc_time;
RTC_DateTypeDef rtc_date;
/*初始化部分由HAL库完成*/
HAL_RTC_Init(&hrtc);//启动并初始化RTC

/*存储在rtc_time、rtc_date的值以二进制写入时间与日期*/
HAL_RTC_SetTime(&hrtc, &rtc_time, RTC_FORMAT_BIN);
HAL_RTC_SetDate(&hrtc, &rtc_date, RTC_FORMAT_BIN);

/*以二进制得到当前时间和日期并分别存储在rtc_time、rtc_date中*/
HAL_RTC_GetTime(&hrtc, &rtc_time, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &rtc_date, RTC_FORMAT_BIN);

注:须同时对时间与日期写入或读取才能生效。

posted @ 2022-12-16 17:44  大羿  阅读(2)  评论(0)    收藏  举报