源:
AWU的应用(用库函数完成的)
//切记要开启中断 且在中断函数中 AWU_GetFlagStatus(); 来清除中断void AWU_SET(void){ CLK_PeripheralClockConfig(CLK_PERIPHERAL_AWU, ENABLE); AWU_Init(AWU_TIMEBASE_12S);}#ifdef _COSMIC_@far @interrupt void AWU_IRQHandler(void)#elsevoid AWU_IRQHandler(void) interrupt 1#endif { AWU_GetFlagStatus(); }
IWDG应用(直接操作寄存器完成的)
在需要清狗的地方调用WDT_CLR()即可
void WDT_CLR(void ){ IWDG->KR = IWDG_KEY_REFRESH;}void START_WDT(void ){ IWDG->KR =0X55;//打开保护 IWDG->PR = IWDG_Prescaler_256;// 28Khz 256分频 IWDG->RLR = 254;//重载寄存器设定 每当喂狗时 此值就会重载 IWDG->KR = IWDG_KEY_REFRESH;//先喂一次 IWDG->KR = IWDG_KEY_ENABLE;//启动看门狗}
WWDG应用
WWDG-CR是向下的计数器,数到0x40就会复位,窗口的上限可以任你设,最大7F,如果设成7F,几乎就成了没有窗口的看门狗了。
窗口的意思是假如你设是0x60,那么只有在0X60-0X40间可以喂狗,其它时间喂,狗就会复位你的单片机,所以喂狗前要先判断狗饿否。
typedef unsigned char uint8_t;void WWDG_Init(uint8_t Counter, uint8_t WindowValue){ //assert_param(IS_WWDG_WINDOWLIMITVALUE_OK(WindowValue)); WWDG->WR = WWDG_WR_RESET_VALUE; WWDG->CR = (uint8_t)((uint8_t)(WWDG_CR_WDGA | WWDG_CR_T6) | (uint8_t)Counter); WWDG->WR = (uint8_t)((uint8_t)(~WWDG_CR_WDGA) & (uint8_t)(WWDG_CR_T6 | WindowValue));}#define BIT_MASK ((uint8_t)0x7F)void WWDG_SetCounter(uint8_t Counter){ WWDG->CR = (uint8_t)(Counter & (uint8_t)BIT_MASK);}#define window_max 0x60#define wwdg_reset 0x70void WWDG_CLR(void){ //狗饿否 if ((WWDG->CR)&0x7f)< window_max)WWDG_SetCounter(wwdg_reset) ;}main(){ WWDG_Init(wwdg_reset,window_max ); while(1) { WWDG_CLR }}