OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 9508|回复: 12

阿波罗HAL库获取RTC时间的问题?

[复制链接]

80

主题

268

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
441
金钱
441
注册时间
2014-8-11
在线时间
84 小时
发表于 2017-1-7 20:21:14 | 显示全部楼层 |阅读模式
10金钱
情况是这样的:
     我在调试模式下系统获取RTC时间然后显示在显示屏上是OK的,时间是走的,
但是我在非调试模式下给系统上电运行发现屏幕上的时间是停止不动的。
我已开始以为是程序跑飞了,但是测试下来ucos其它任务都是正常运行的,
经测试以下hal库函数:红色部分是为了观察用的(没办法了,调试模式完全是正常的)
HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
{
  uint32_t tmpreg = 0;

  /* Check the parameters */
  assert_param(IS_RTC_FORMAT(Format));
  
  /* Get subseconds structure field 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)) >> 16);
  sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8);
  sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
  sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16);
  
  /* Check the input parameters format */
  if(Format == RTC_FORMAT_BIN)
  {
    /* Convert the time structure parameters to Binary format */
       LED0=!LED0;
    sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
    sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
    sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
        if((sTime->Seconds)%5==0){beep();}
  }
  
  return HAL_OK;
}

系统运行发现LED0亮灭交替,但是beep始终不响,故判定RTC获取的时间始终没变!
纠结的是只要调试,这个时间就是正常走的而且beep 5s会嘀一下。。。。。。

这个问题如何解决,有遇到这种情况的么?

最佳答案

查看完整内容[请看2#楼]

/** * @brief Gets RTC current time. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains * the configuration information for RTC. * @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 dat ...
我是一只菜鸟,但我会大鹏展翅
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

80

主题

268

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
441
金钱
441
注册时间
2014-8-11
在线时间
84 小时
 楼主| 发表于 2017-1-7 20:21:15 | 显示全部楼层
/**
  * @brief  Gets RTC current time.
  * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @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
  * @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.
  * @retval HAL status
  */
我是一只菜鸟,但我会大鹏展翅
回复

使用道具 举报

14

主题

89

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
283
金钱
283
注册时间
2015-4-19
在线时间
137 小时
发表于 2017-2-28 22:53:32 | 显示全部楼层
你问题已经解决了吗,我也碰到此问题,发现  TR 寄存器的时间值一直不会变,但是每次复位会改变时间值,求解答
回复

使用道具 举报

14

主题

89

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
283
金钱
283
注册时间
2015-4-19
在线时间
137 小时
发表于 2017-2-28 23:47:27 | 显示全部楼层
问题已经解决,hal库的时间获取函数有bug,会无法获取时间,现在采用寄存器方法获取时间正常
回复

使用道具 举报

2

主题

65

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
2106
金钱
2106
注册时间
2016-5-21
在线时间
238 小时
发表于 2017-3-1 11:16:05 | 显示全部楼层
@note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
回复

使用道具 举报

20

主题

122

帖子

0

精华

高级会员

Rank: 4

积分
636
金钱
636
注册时间
2014-6-20
在线时间
168 小时
发表于 2017-4-6 15:19:24 | 显示全部楼层
你是不是只调用了HAL_RTC_GetTime获取时间,没有调用HAL_RTC_GetDate获取日期,我F7之前也是无法更新时间,后来,时间日期一起读取,就可以了。
回复

使用道具 举报

80

主题

268

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
441
金钱
441
注册时间
2014-8-11
在线时间
84 小时
 楼主| 发表于 2017-4-19 16:28:56 | 显示全部楼层
Fliger 发表于 2017-4-6 15:19
你是不是只调用了HAL_RTC_GetTime获取时间,没有调用HAL_RTC_GetDate获取日期,我F7之前也是无法更新时间, ...

是的,我后来也找到原因了,库函数的note还是要看看才可以少纠结
我是一只菜鸟,但我会大鹏展翅
回复

使用道具 举报

9

主题

20

帖子

0

精华

初级会员

Rank: 2

积分
96
金钱
96
注册时间
2017-1-14
在线时间
27 小时
发表于 2017-5-4 13:15:05 | 显示全部楼层
战舰水手 发表于 2017-4-19 16:28
是的,我后来也找到原因了,库函数的note还是要看看才可以少纠结

请教,HAL库的NOTE是在哪里获取的啊?
回复

使用道具 举报

8

主题

32

帖子

0

精华

新手上路

积分
37
金钱
37
注册时间
2017-3-23
在线时间
13 小时
发表于 2017-6-26 14:06:19 | 显示全部楼层
战舰水手 发表于 2017-4-19 16:28
是的,我后来也找到原因了,库函数的note还是要看看才可以少纠结

你好楼主,这个库函数的返回值不是状态吗?怎么把RTC的时间值读出来啊?
回复

使用道具 举报

80

主题

268

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
441
金钱
441
注册时间
2014-8-11
在线时间
84 小时
 楼主| 发表于 2017-8-13 11:52:15 | 显示全部楼层
蝙蝠侠 发表于 2017-6-26 14:06
你好楼主,这个库函数的返回值不是状态吗?怎么把RTC的时间值读出来啊?

返回的是状态,他有个全局的结构体是在这个函数里面被赋值的
我是一只菜鸟,但我会大鹏展翅
回复

使用道具 举报

11

主题

66

帖子

0

精华

高级会员

Rank: 4

积分
644
金钱
644
注册时间
2018-4-17
在线时间
152 小时
发表于 2019-5-23 19:57:52 | 显示全部楼层
6楼说得对啊
回复

使用道具 举报

9

主题

162

帖子

0

精华

高级会员

Rank: 4

积分
933
金钱
933
注册时间
2017-3-23
在线时间
255 小时
发表于 2020-9-26 09:08:41 | 显示全部楼层
在函数定义的上方有这么一段解释,我也是看了这个帖子回去找,才看到的

@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.
回复

使用道具 举报

5

主题

45

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
210
金钱
210
注册时间
2015-2-12
在线时间
35 小时
发表于 2022-2-26 17:22:12 | 显示全部楼层
找了两个礼拜的问题,还是在本论坛得到了解决,太感谢了
* @note在hal_rtc_gettime()后必须调用hal_rtc_getdate()来解锁值
回复

使用道具 举报

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

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-2-28 03:23

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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