▍STM32独立看门狗 独立看门狗需设置四个寄存器如下: 它的初始化过程大致如下 : //时间计算(大概):Tout=((4*2^prer)*rlr)/40 (ms)
void IWDG_Init(u8 prer,u16 rlr)
{
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
IWDG_SetPrescaler(prer);
IWDG_SetReload(rlr);
IWDG_ReloadCounter();
IWDG_Enable();
} ▍STM32窗口看门狗 共三个寄存器 以上描述可参考下图以更清晰的理解:窗口看门狗应用时还要注意算准最小与最大喂狗时间,以便正确地喂狗,如下: //窗口看门狗中断服务设置程序 void WWDG_NVIC_Init()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = WWDG_IRQn; //WWDG 中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //抢占2 子优先级3 组2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //抢占2,子优先级3,组2
NVIC_Init(&NVIC_InitStructure); //NVIC 初始化
} //保存WWDG 计数器的设置值,默认为最大.
u8 WWDG_CNT=0x7f; //初始化窗口看门狗
//tr :T[6:0],计数器值
//wr :W[6:0],窗口值
//fprer:分频系数(WDGTB ),仅最低2 位有效
//Fwwdg=PCLK1/(4096*2^fprer).
void WWDG_Init(u8 tr,u8 wr,u32 fprer)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE); // WWDG 时钟使能
WWDG_CNT=tr&WWDG_CNT; //初始化WWDG_CNT.
WWDG_SetPrescaler(fprer); //设置IWDG 预分频值
WWDG_SetWindowValue(wr); //设置窗口值
WWDG_Enable(WWDG_CNT); //使能看门狗,设置counter
WWDG_ClearFlag(); //清除提前唤醒中断标志位 (注:若没有此句则会在初始化后先进入中断一次)
WWDG_NVIC_Init(); //初始化窗口看门狗NVIC
WWDG_EnableIT(); //开启窗口看门狗中断
}
|