|
|
爱科技、爱创意、爱折腾、爱极致,我们都是技术控
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 lmn2005 于 2026-1-31 13:26 编辑
近两天用STM32F103C6T6A制作一个电子钟,用STM32CubeMX生成工程后,直接用HAL库中的设定日期时间的函数来初始化时间。
初始化时间开始的时候,读取的日期和时间都是正确的,但当系统复位或主电源断电后,读取的时间是正确的,但读取的日期就变成了0年1月1日。VBAT电源是正常的。
HAL中设定日期和读取日期的函数如下:
- /**
- * [url=home.php?mod=space&uid=650075]@brief[/url] Sets RTC current date.
- * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
- * the configuration information for RTC.
- * @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_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
- {
- uint32_t counter_time = 0U, counter_alarm = 0U, hours = 0U;
- /* Check input parameters */
- if ((hrtc == NULL) || (sDate == NULL))
- {
- return HAL_ERROR;
- }
- /* Check the parameters */
- assert_param(IS_RTC_FORMAT(Format));
- /* Process Locked */
- __HAL_LOCK(hrtc);
- hrtc->State = HAL_RTC_STATE_BUSY;
- if (Format == RTC_FORMAT_BIN)
- {
- assert_param(IS_RTC_YEAR(sDate->Year));
- assert_param(IS_RTC_MONTH(sDate->Month));
- assert_param(IS_RTC_DATE(sDate->Date));
- /* Change the current date */
- hrtc->DateToUpdate.Year = sDate->Year;
- hrtc->DateToUpdate.Month = sDate->Month;
- hrtc->DateToUpdate.Date = sDate->Date;
- }
- else
- {
- assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
- assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
- assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
- /* Change the current date */
- hrtc->DateToUpdate.Year = RTC_Bcd2ToByte(sDate->Year);
- hrtc->DateToUpdate.Month = RTC_Bcd2ToByte(sDate->Month);
- hrtc->DateToUpdate.Date = RTC_Bcd2ToByte(sDate->Date);
- }
- /* WeekDay set by user can be ignored because automatically calculated */
- hrtc->DateToUpdate.WeekDay = RTC_WeekDayNum(hrtc->DateToUpdate.Year, hrtc->DateToUpdate.Month, hrtc->DateToUpdate.Date);
- sDate->WeekDay = hrtc->DateToUpdate.WeekDay;
- /* Reset time to be aligned on the same day */
- /* Read the time counter*/
- counter_time = RTC_ReadTimeCounter(hrtc);
- /* Fill the structure fields with the read parameters */
- hours = counter_time / 3600U;
- if (hours > 24U)
- {
- /* Set updated time in decreasing counter by number of days elapsed */
- counter_time -= ((hours / 24U) * 24U * 3600U);
- /* Write time counter in RTC registers */
- if (RTC_WriteTimeCounter(hrtc, counter_time) != HAL_OK)
- {
- /* Set RTC state */
- hrtc->State = HAL_RTC_STATE_ERROR;
- /* Process Unlocked */
- __HAL_UNLOCK(hrtc);
- return HAL_ERROR;
- }
- /* Read current Alarm counter in RTC registers */
- counter_alarm = RTC_ReadAlarmCounter(hrtc);
- /* Set again alarm to match with new time if enabled */
- if (counter_alarm != RTC_ALARM_RESETVALUE)
- {
- if (counter_alarm < counter_time)
- {
- /* Add 1 day to alarm counter*/
- counter_alarm += (uint32_t)(24U * 3600U);
- /* Write new Alarm counter in RTC registers */
- if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
- {
- /* Set RTC state */
- hrtc->State = HAL_RTC_STATE_ERROR;
- /* Process Unlocked */
- __HAL_UNLOCK(hrtc);
- return HAL_ERROR;
- }
- }
- }
- }
- hrtc->State = HAL_RTC_STATE_READY ;
- /* Process Unlocked */
- __HAL_UNLOCK(hrtc);
- return HAL_OK;
- }
- /**
- * @brief Gets RTC current date.
- * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
- * the configuration information for RTC.
- * @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_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
- {
- RTC_TimeTypeDef stime = {0U};
- /* Check input parameters */
- if ((hrtc == NULL) || (sDate == NULL))
- {
- return HAL_ERROR;
- }
- /* Check the parameters */
- assert_param(IS_RTC_FORMAT(Format));
- /* Call HAL_RTC_GetTime function to update date if counter higher than 24 hours */
- if (HAL_RTC_GetTime(hrtc, &stime, RTC_FORMAT_BIN) != HAL_OK)
- {
- return HAL_ERROR;
- }
- /* Fill the structure fields with the read parameters */
- sDate->WeekDay = hrtc->DateToUpdate.WeekDay;
- sDate->Year = hrtc->DateToUpdate.Year;
- sDate->Month = hrtc->DateToUpdate.Month;
- sDate->Date = hrtc->DateToUpdate.Date;
- /* Check the input parameters format */
- if (Format != RTC_FORMAT_BIN)
- {
- /* Convert the date structure parameters to BCD format */
- sDate->Year = (uint8_t)RTC_ByteToBcd2(sDate->Year);
- sDate->Month = (uint8_t)RTC_ByteToBcd2(sDate->Month);
- sDate->Date = (uint8_t)RTC_ByteToBcd2(sDate->Date);
- }
- return HAL_OK;
- }
复制代码 请各位大神支招,这问题该如何解决?
|
|