数码之家

 找回密码
 立即注册

QQ登录

只需一步,快速开始

微信登录

微信扫一扫,快速登录

搜索
查看: 198|回复: 2

[STM] STM32F103的RTC HAL库有问题吗?

[复制链接]
发表于 2026-1-31 11:13:09 | 显示全部楼层 |阅读模式

爱科技、爱创意、爱折腾、爱极致,我们都是技术控

您需要 登录 才可以下载或查看,没有账号?立即注册 微信登录

x
本帖最后由 lmn2005 于 2026-1-31 13:26 编辑

近两天用STM32F103C6T6A制作一个电子钟,用STM32CubeMX生成工程后,直接用HAL库中的设定日期时间的函数来初始化时间。

初始化时间开始的时候,读取的日期和时间都是正确的,但当系统复位或主电源断电后,读取的时间是正确的,但读取的日期就变成了0年1月1日。VBAT电源是正常的。
HAL中设定日期和读取日期的函数如下:
  1. /**
  2.   * [url=home.php?mod=space&uid=650075]@brief[/url]  Sets RTC current date.
  3.   * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
  4.   *                the configuration information for RTC.
  5.   * @param  sDate: Pointer to date structure
  6.   * @param  Format: specifies the format of the entered parameters.
  7.   *          This parameter can be one of the following values:
  8.   *            @arg RTC_FORMAT_BIN: Binary data format
  9.   *            @arg RTC_FORMAT_BCD: BCD data format
  10.   * @retval HAL status
  11.   */
  12. HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
  13. {
  14.   uint32_t counter_time = 0U, counter_alarm = 0U, hours = 0U;

  15.   /* Check input parameters */
  16.   if ((hrtc == NULL) || (sDate == NULL))
  17.   {
  18.     return HAL_ERROR;
  19.   }

  20.   /* Check the parameters */
  21.   assert_param(IS_RTC_FORMAT(Format));

  22.   /* Process Locked */
  23.   __HAL_LOCK(hrtc);

  24.   hrtc->State = HAL_RTC_STATE_BUSY;

  25.   if (Format == RTC_FORMAT_BIN)
  26.   {
  27.     assert_param(IS_RTC_YEAR(sDate->Year));
  28.     assert_param(IS_RTC_MONTH(sDate->Month));
  29.     assert_param(IS_RTC_DATE(sDate->Date));

  30.     /* Change the current date */
  31.     hrtc->DateToUpdate.Year  = sDate->Year;
  32.     hrtc->DateToUpdate.Month = sDate->Month;
  33.     hrtc->DateToUpdate.Date  = sDate->Date;
  34.   }
  35.   else
  36.   {
  37.     assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
  38.     assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
  39.     assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));

  40.     /* Change the current date */
  41.     hrtc->DateToUpdate.Year  = RTC_Bcd2ToByte(sDate->Year);
  42.     hrtc->DateToUpdate.Month = RTC_Bcd2ToByte(sDate->Month);
  43.     hrtc->DateToUpdate.Date  = RTC_Bcd2ToByte(sDate->Date);
  44.   }

  45.   /* WeekDay set by user can be ignored because automatically calculated */
  46.   hrtc->DateToUpdate.WeekDay = RTC_WeekDayNum(hrtc->DateToUpdate.Year, hrtc->DateToUpdate.Month, hrtc->DateToUpdate.Date);
  47.   sDate->WeekDay = hrtc->DateToUpdate.WeekDay;

  48.   /* Reset time to be aligned on the same day */
  49.   /* Read the time counter*/
  50.   counter_time = RTC_ReadTimeCounter(hrtc);

  51.   /* Fill the structure fields with the read parameters */
  52.   hours = counter_time / 3600U;
  53.   if (hours > 24U)
  54.   {
  55.     /* Set updated time in decreasing counter by number of days elapsed */
  56.     counter_time -= ((hours / 24U) * 24U * 3600U);
  57.     /* Write time counter in RTC registers */
  58.     if (RTC_WriteTimeCounter(hrtc, counter_time) != HAL_OK)
  59.     {
  60.       /* Set RTC state */
  61.       hrtc->State = HAL_RTC_STATE_ERROR;

  62.       /* Process Unlocked */
  63.       __HAL_UNLOCK(hrtc);

  64.       return HAL_ERROR;
  65.     }

  66.     /* Read current Alarm counter in RTC registers */
  67.     counter_alarm = RTC_ReadAlarmCounter(hrtc);

  68.     /* Set again alarm to match with new time if enabled */
  69.     if (counter_alarm != RTC_ALARM_RESETVALUE)
  70.     {
  71.       if (counter_alarm < counter_time)
  72.       {
  73.         /* Add 1 day to alarm counter*/
  74.         counter_alarm += (uint32_t)(24U * 3600U);

  75.         /* Write new Alarm counter in RTC registers */
  76.         if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
  77.         {
  78.           /* Set RTC state */
  79.           hrtc->State = HAL_RTC_STATE_ERROR;

  80.           /* Process Unlocked */
  81.           __HAL_UNLOCK(hrtc);

  82.           return HAL_ERROR;
  83.         }
  84.       }
  85.     }


  86.   }

  87.   hrtc->State = HAL_RTC_STATE_READY ;

  88.   /* Process Unlocked */
  89.   __HAL_UNLOCK(hrtc);

  90.   return HAL_OK;
  91. }

  92. /**
  93.   * @brief  Gets RTC current date.
  94.   * @param  hrtc   pointer to a RTC_HandleTypeDef structure that contains
  95.   *                the configuration information for RTC.
  96.   * @param  sDate: Pointer to Date structure
  97.   * @param  Format: Specifies the format of the entered parameters.
  98.   *          This parameter can be one of the following values:
  99.   *            @arg RTC_FORMAT_BIN:  Binary data format
  100.   *            @arg RTC_FORMAT_BCD:  BCD data format
  101.   * @retval HAL status
  102.   */
  103. HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
  104. {
  105.   RTC_TimeTypeDef stime = {0U};

  106.   /* Check input parameters */
  107.   if ((hrtc == NULL) || (sDate == NULL))
  108.   {
  109.     return HAL_ERROR;
  110.   }

  111.   /* Check the parameters */
  112.   assert_param(IS_RTC_FORMAT(Format));

  113.   /* Call HAL_RTC_GetTime function to update date if counter higher than 24 hours */
  114.   if (HAL_RTC_GetTime(hrtc, &stime, RTC_FORMAT_BIN) != HAL_OK)
  115.   {
  116.     return HAL_ERROR;
  117.   }

  118.   /* Fill the structure fields with the read parameters */
  119.   sDate->WeekDay  = hrtc->DateToUpdate.WeekDay;
  120.   sDate->Year     = hrtc->DateToUpdate.Year;
  121.   sDate->Month    = hrtc->DateToUpdate.Month;
  122.   sDate->Date     = hrtc->DateToUpdate.Date;

  123.   /* Check the input parameters format */
  124.   if (Format != RTC_FORMAT_BIN)
  125.   {
  126.     /* Convert the date structure parameters to BCD format */
  127.     sDate->Year   = (uint8_t)RTC_ByteToBcd2(sDate->Year);
  128.     sDate->Month  = (uint8_t)RTC_ByteToBcd2(sDate->Month);
  129.     sDate->Date   = (uint8_t)RTC_ByteToBcd2(sDate->Date);
  130.   }
  131.   return HAL_OK;
  132. }
复制代码
请各位大神支招,这问题该如何解决?
发表于 2026-2-1 00:48:55 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

 楼主| 发表于 2026-2-1 08:31:22 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 微信登录

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|数码之家-技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2026-5-30 05:52 , Processed in 0.374401 second(s), 8 queries , Gzip On, Redis On.

Powered by Discuz!

© MyDigit.Net Since 2006

快速回复 返回顶部 返回列表