/*
================================================================================
描述 :RTC初始化
输入 :
输出 :
================================================================================
*/
void drv_rtc_init(void)
{
#ifdef USE_RTC
__IO uint32_t prescaler_a = 0, prescaler_s = 0;
rcu_periph_clock_enable(RCU_PMU);
pmu_backup_write_enable();
#if defined (RTC_CLOCK_SOURCE_IRC40K)
rcu_osci_on(RCU_IRC40K);
rcu_osci_stab_wait(RCU_IRC40K);
rcu_rtc_clock_config(RCU_RTCSRC_IRC40K);
prescaler_s = 0x18F;
prescaler_a = 0x63;
#elif defined (RTC_CLOCK_SOURCE_LXTAL)
rcu_osci_on(RCU_LXTAL);
rcu_osci_stab_wait(RCU_LXTAL);
rcu_rtc_clock_config(RCU_RTCSRC_LXTAL);
prescaler_s = 0xFF;
prescaler_a = 0x7F;
#else
#error RTC clock source should be defined.
#endif /* RTC_CLOCK_SOURCE_IRC40K */
rcu_periph_clock_enable(RCU_RTC);
rtc_register_sync_wait();
if(BKP_VALUE != RTC_BKP0)
{
rtc_parameter_struct rtc_initpara;
rtc_initpara.rtc_factor_asyn = prescaler_a;
rtc_initpara.rtc_factor_syn = prescaler_s;
rtc_initpara.rtc_year = 0x16;
rtc_initpara.rtc_day_of_week = RTC_SATURDAY;
rtc_initpara.rtc_month = RTC_APR;
rtc_initpara.rtc_date = 0x30;
rtc_initpara.rtc_display_format = RTC_24HOUR;
rtc_initpara.rtc_am_pm = RTC_AM;
rtc_initpara.rtc_hour = 0;
rtc_initpara.rtc_minute = 0;
rtc_initpara.rtc_second = 0;
if(ERROR == rtc_init(&rtc_initpara))
{
printf("\n\r** RTC time configuration failed! **\n\r");
}
else
{
printf("\n\r** RTC time configuration success! **\n\r");
RTC_BKP0 = BKP_VALUE;
}
}
else
{
printf("RTC continue!\n");
}
#endif
}
以上是初始化代码,我设置了一个时间后,然后每隔5S打印一次获取到的RTC时间,发现RTC时间的间隔是10S,也就是以正常两倍的速度在运行。
以下是串口打印输出的内容。
[15:02:26.941] app sec counter=175s
[15:02:26.941] hour=12, min=55, sec=24
[15:02:26.941] ***2021-07-05 12:55:24
[15:02:26.941] rtc_time=1625460924s
[15:02:26.947]
[15:02:31.940] app sec counter=180s
[15:02:31.950] hour=12, min=55, sec=34
[15:02:31.950] ***2021-07-05 12:55:34
[15:02:31.950] rtc_time=1625460934s
|