完整的测试程序:判断出首次启动程序,单片机处于正常状态时看门狗复位,单片机处于待机模式下被闹钟唤醒/看门狗唤醒.
注意:每次看门狗复位都要被重新配置启动
<div class="dp-highlighter bg_cpp">//检查是否正常状态发生看门狗复位
<ol class="dp-cpp"> if (RCC_GetFlagStatus(RCC_FLAG_IWDGRST) != RESET)
<li class="alt">{ //清楚标志位
<li class="alt"> RCC_ClearFlag();
<li class="alt"> //RTC功能开启 //使能外部晶振
<li class="alt"> RCC_LSEConfig(RCC_LSE_ON); //等待外部晶振准备好
<li class="alt"> while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
<li class="alt"> //设置RTC时钟为外部晶振 RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
<li class="alt"> //使能RTC时钟
<li class="alt"> RCC_RTCCLKCmd(ENABLE); //等待RTC_CTL寄存器中的RSF位(寄存器同步标志)被硬件置1
<li class="alt"> RTC_WaitForSynchro(); RTC_WaitForLastTask();
<li class="alt"> //使能闹钟中断
<li class="alt"> RTC_ITConfig(RTC_IT_ALR, ENABLE); RTC_WaitForLastTask();
<li class="alt"> //分频系数为1,即最小时间单位1/2^15 = 30.5us
<li class="alt"> RTC_SetPrescaler(RTC_PRESCALE); RTC_WaitForLastTask();
<li class="alt"> //启动独立看门狗
<li class="alt"> IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); //访问之前要首先使能寄存器写 IWDG_SetPrescaler(IWDG_Prescaler_64); //64分频 一个周期1.6ms
<li class="alt"> IWDG_SetReload(1250); //最长12位 [0,4096] 1250*1.6 = 2s IWDG_ReloadCounter(); //喂狗
<li class="alt"> //使能开门狗 IWDG_Enable();
<li class="alt"> if (BKP_ReadBackupRegister(BKP_DR5) == 0xabcd)
<li class="alt"> { GPIO_SetBits(GPIOA,GPIO_Pin_12) ;
<li class="alt"> BKP_WriteBackupRegister(BKP_DR5,0); }
<li class="alt"> else {
<li class="alt"> GPIO_ResetBits(GPIOA, GPIO_Pin_12) ; BKP_WriteBackupRegister(BKP_DR5,0xabcd);
<li class="alt"> } }
<li class="alt">else {
<li class="alt"> //检查唤醒标志是否设置,判断是否首次启动 if (PWR_GetFlagStatus(PWR_FLAG_WU) == RESET)
<li class="alt"> { //首次启动
<li class="alt"> //初始化备份寄存器 //BKP_DeInit();
<li class="alt"> //RTC功能开启
<li class="alt"> //使能外部晶振 RCC_LSEConfig(RCC_LSE_ON);
<li class="alt"> //等待外部晶振准备好 while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
<li class="alt"> //设置RTC时钟为外部晶振
<li class="alt"> RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
<li class="alt"> //使能RTC时钟 RCC_RTCCLKCmd(ENABLE);
<li class="alt"> //等待RTC_CTL寄存器中的RSF位(寄存器同步标志)被硬件置1 RTC_WaitForSynchro();
<li class="alt"> RTC_WaitForLastTask();
<li class="alt"> //使能闹钟中断 RTC_ITConfig(RTC_IT_ALR, ENABLE);
<li class="alt"> RTC_WaitForLastTask();
<li class="alt"> //分频系数为1,即最小时间单位1/2^15 = 30.5us RTC_SetPrescaler(RTC_PRESCALE);
<li class="alt"> RTC_WaitForLastTask();
<li class="alt"> //启动独立看门狗 IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); //访问之前要首先使能寄存器写
<li class="alt"> IWDG_SetPrescaler(IWDG_Prescaler_64); //64分频 一个周期1.6ms IWDG_SetReload(1250); //最长12位 [0,4096] 1250*1.6 = 2s
<li class="alt"> IWDG_ReloadCounter(); //喂狗 //使能开门狗
<li class="alt"> IWDG_Enable();
<li class="alt"> if (BKP_ReadBackupRegister(BKP_DR6) == 0xabcd) {
<li class="alt"> GPIO_ResetBits(GPIOA, GPIO_Pin_11) ; BKP_WriteBackupRegister(BKP_DR6,0);
<li class="alt"> } else
<li class="alt"> { GPIO_SetBits(GPIOA,GPIO_Pin_11) ;
<li class="alt"> BKP_WriteBackupRegister(BKP_DR6,0xabcd); }
<li class="alt"> } else
<li class="alt"> { //从待机模式中退出,有闹钟唤醒或者看门狗唤醒
<li class="alt"> //清除唤醒标志 RCC_ClearFlag();
<li class="alt"> if (BKP_ReadBackupRegister(BKP_DR7) == 0xabcd)
<li class="alt"> { GPIO_ResetBits(GPIOA, GPIO_Pin_8) ;
<li class="alt"> BKP_WriteBackupRegister(BKP_DR7,0); }
<li class="alt"> else {
<li class="alt"> GPIO_SetBits(GPIOA,GPIO_Pin_8) ; BKP_WriteBackupRegister(BKP_DR7,0xabcd);
<li class="alt"> } }
<li class="alt">}
<li class="alt">//延时1s for (i = 0;i |