/*!
\brief configure the RTC
\param[in] none
\param[out] none
\retval none
*/
void rtc_configuration(void)
{
/* enable BKPI clock */
rcu_periph_clock_enable(RCU_BKPI);
rcu_periph_clock_enable(RCU_PMU);//使能PMU电源管理单元时钟
/* allow access to backup domain */
pmu_backup_write_enable();
/* reset backup domain */
bkp_deinit();
/* enable IRC40K */
rcu_osci_on(RCU_IRC40K);
/* wait till IRC40K is ready */
rcu_osci_stab_wait(RCU_IRC40K);
/* select RCU_IRC40K as RTC clock source */
rcu_rtc_clock_config(RCU_RTCSRC_IRC40K);
/* enable RTC clock */
rcu_periph_clock_enable(RCU_RTC);
/* wait for RTC registers synchronization */
rtc_register_sync_wait();
/* wait until last write operation on RTC registers has finished */
rtc_lwoff_wait();
/* enable the RTC alarm interrupt */
rtc_interrupt_enable(RTC_INT_ALARM);
// rtc_interrupt_enable(RTC_INT_SECOND);
/* wait until last write operation on RTC registers has finished */
rtc_lwoff_wait();
/* set RTC prescaler: set RTC period to 1s */
rtc_prescaler_set(3999);
/*注意:这里必须分频,且分频出来的时钟必须小于APB1时钟的四分之一,
即rtc时钟不能大于15Mhz
这里设置分频3,即PSC=3, 频率SC_CLK = RTCCLK /(PSC[19:0]+1)=40000/(3+1)=10000Hz=10MHZ
如此秒中断为1/10000=0.0001s触发一次
如果要改时基,按照以上公式修改
*/
/* wait until last write operation on RTC registers has finished */
rtc_lwoff_wait();
rtc_counter_set(0);
/* wait until last write operation on RTC registers has finished */
rtc_lwoff_wait();
rtc_alarm_config(4);//这里是经过多少个时基触发闹钟
/* wait until last write operation on RTC registers has finished */
rtc_lwoff_wait();
/* clear the RTC alarm flag */
rtc_flag_clear(RTC_FLAG_ALARM);
/* enable and set EXTI interrupt to the lowest priority */
nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
nvic_irq_enable(RTC_Alarm_IRQn, 0U, 0U);
/* configure EXTI line */
exti_init(EXTI_17, EXTI_INTERRUPT, EXTI_TRIG_RISING);//这里测试过是上升沿触发才行
//exti_interrupt_flag_clear(EXTI_17);
exti_interrupt_enable(EXTI_17);
}
void Enter_Deepsleepmode(void){
pmu_to_deepsleepmode(PMU_LDO_NORMAL, PMU_LOWDRIVER_DISABLE, WFI_CMD);
//SystemInit();//重新初始化时钟
}
然后是中断服务函数
void RTC_Alarm_IRQHandler(void)
{
if(rtc_flag_get(RTC_FLAG_ALARM))
{
SystemInit();//重新初始化时钟 直接调用system_clock_120m_hxtal也行
//Set_led_off();
exti_flag_clear(EXTI_17);
exti_interrupt_flag_clear(EXTI_17);
rtc_flag_clear(RTC_FLAG_ALARM);
rtc_register_sync_wait();
rtc_lwoff_wait();
rtc_alarm_config(4);//如果修改了闹钟时间,这里记得修改
rtc_lwoff_wait();
rtc_counter_set(0);
rtc_lwoff_wait();
// fwdgt_counter_reload();如果加了看门狗,这一步写喂狗
}
}