STM32 HAL库读取RTC时钟一直不更新时间的问题

项目场景:

上传至服务器的数据里面带有时间戳,通过后台发现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;
}

 

  • 18
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
STM32HAL库中的RTC是指实时时钟(Real-Time Clock),用于提供精确的时间和日期信息。在STM32HAL库中,可以使用一些函数来设置和获取RTC的参数。 引用\[1\]中提到了一些设置和获取RTC参数的函数。例如,使用HAL_RTC_GetDate函数可以获取RTC的日期参数,使用HAL_RTC_GetTime函数可以获取RTC时间参数。而使用HAL_RTC_SetTime函数可以设置RTC时间参数,使用HAL_RTC_SetDate函数可以设置RTC的日期参数。 引用\[2\]中提到了RTC寄存器的一些信息。RTC寄存器包含了秒、分钟、小时、星期几、日期、月份和年份等信息。此外,RTC还可以提供亚秒值,并且可以自动补偿月份的天数。 引用\[3\]中提到了RTC的复位过程。除了一些特定的寄存器外,其他系统寄存器都会在系统复位或电源复位时进行异步复位。而RTC的特定寄存器(RTC_PRL、RTC_ALR、RTC_CNT和RTC_DIV)只能通过备份域复位信号进行复位。 综上所述,STM32HAL库中的RTC提供了设置和获取时间日期参数的函数,并且有相应的寄存器来存储这些参数。在复位过程中,RTC的特定寄存器可以通过备份域复位信号进行复位。 #### 引用[.reference_title] - *1* [【STM32HAL库开发教程(五)—RTC使用](https://blog.csdn.net/weixin_44064233/article/details/108326975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [STM32日历读取,设置和输出](https://blog.csdn.net/qq_45874317/article/details/121697122)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值