欧阳小麟 发表于 2020-1-19 17:07:14

关于STM32L071的RTC时间读取问题

在开启RTC后,频繁读取RTC时间,出现在跨天的时候天数无法累加,不知道有没有小伙伴遇到同样的问题?
(例如时间19-01-01 23:59:50->19-01-02 00:00:02的过程成,频繁读取RTC时间,基本为1S读一次,在跨天后读取出来的时间值为19-01-01 00:00:01)
不知道这由什么问题导致的?
RTC_Wait_For_Synchro();//Waiting for synchronization
RTC_Date_TYPE *pDate = (RTC_Date_TYPE *)&RTC->DR;
RTC_Time_TYPE *pTime = (RTC_Time_TYPE *)&RTC->TR;
UINT16 tmpreg = RTC->SSR;
rtc->SSeconds = ((RTC_SYNCHRONOUS_PRESCALER-tmpreg)*125)>>5;
rtc->Year.ALL = pDate->Year;
rtc->Month.ALL = pDate->Month&0x1f;
rtc->Date.ALL = pDate->Date;
rtc->Hours.ALL = pTime->Hours&0x3f;
rtc->Minutes.ALL = pTime->Minutes;
rtc->Seconds.ALL = pTime->Seconds;

ljtzsd 发表于 2020-1-20 09:39:12

先读时间再读日期,可以试试hal库

欧阳小麟 发表于 2020-1-21 11:47:32

ljtzsd 发表于 2020-1-20 09:39
先读时间再读日期,可以试试hal库

先读日期再读时间就会有这种问题吗?
hal的库也都只是独立封装出来,效果应该都是一样的吧

ljtzsd 发表于 2020-1-22 20:32:20

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.
官方note

ljtzsd 发表于 2020-1-22 20:32:48

欧阳小麟 发表于 2020-1-21 11:47
先读日期再读时间就会有这种问题吗?
hal的库也都只是独立封装出来,效果应该都是一样的吧 ...

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.

butterflyspring 发表于 2020-2-7 14:46:00

官方的手册上关于RTC寄存器的读取及原因有详细的说明,软件库中的注释就是依据RTC原理。详细的可以仔细读一下相关章节

五哥1 发表于 2020-4-2 00:20:38

这个看看原子哥的程序就明白了
页: [1]
查看完整版本: 关于STM32L071的RTC时间读取问题