硬汉嵌入式论坛

 找回密码
 立即注册
查看: 822|回复: 4
收起左侧

求教!STM32F4 的RTC库函数问题

[复制链接]

3

主题

20

回帖

29

积分

新手上路

积分
29
发表于 2022-4-24 17:12:11 | 显示全部楼层 |阅读模式
在HAL stm32f4xx_hal_rtc.c 文件中RTC时间读取和写入函数中为何 sTime->TimeFormat 是偏移16U? 查看寄存器位22才是TimeFormat 对应的位,难道不是应该偏移22u吗?求教大佬!

  /* Fill the structure fields with the read parameters */
  sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16U);
  sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> 8U);
  sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
  sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16U);



image.png

image.png
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107452
QQ
发表于 2022-4-24 21:32:32 | 显示全部楼层
#define RTC_TR_PM_Pos                 (22U)                                    
#define RTC_TR_PM_Msk                 (0x1UL << RTC_TR_PM_Pos)                  /*!< 0x00400000 */

[C] 纯文本查看 复制代码
HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
{
  uint32_t tmpreg = 0U;

  /* Check the parameters */
  assert_param(IS_RTC_FORMAT(Format));

  /* Get subseconds value from the corresponding register */
  sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);

  /* Get SecondFraction structure field from the corresponding register field*/
  sTime->SecondFraction = (uint32_t)(hrtc->Instance->PRER & RTC_PRER_PREDIV_S);

  /* Get the TR register */
  tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);

  /* Fill the structure fields with the read parameters */
  sTime->Hours      = (uint8_t)((tmpreg & (RTC_TR_HT  | RTC_TR_HU))  >> RTC_TR_HU_Pos);
  sTime->Minutes    = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> RTC_TR_MNU_Pos);
  sTime->Seconds    = (uint8_t)( tmpreg & (RTC_TR_ST  | RTC_TR_SU));
  sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM))               >> RTC_TR_PM_Pos);

  /* Check the input parameters format */
  if (Format == RTC_FORMAT_BIN)
  {
    /* Convert the time structure parameters to Binary format */
    sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
    sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
    sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
  }

  return HAL_OK;
}


回复

使用道具 举报

3

主题

20

回帖

29

积分

新手上路

积分
29
 楼主| 发表于 2022-4-25 11:04:24 | 显示全部楼层
eric2013 发表于 2022-4-24 21:32
#define RTC_TR_PM_Pos                 (22U)                                    
#define RTC_TR_PM_M ...

硬汉哥你这贴的是H7的hal库函数吗?我看h7生成出来的是对的,但是F4的hal库函数里面好像就是错的
回复

使用道具 举报

212

主题

1051

回帖

1697

积分

至尊会员

More we do, more we can do.

积分
1697
发表于 2022-4-25 13:41:19 | 显示全部楼层
它这写法缺点是位不在bit0,如果用 TimeFormat != 0来判断格式,也不会有问题。
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107452
QQ
发表于 2022-4-25 17:19:44 | 显示全部楼层
Shawnx 发表于 2022-4-25 11:04
硬汉哥你这贴的是H7的hal库函数吗?我看h7生成出来的是对的,但是F4的hal库函数里面好像就是错的

我这是最新版的F4 HAL库
回复

使用道具 举报

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

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-6-1 19:01 , Processed in 0.169880 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

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