STM8S awu及看門狗IWDG WWDG應(yīng)用
AWU的應(yīng)用(用庫函數(shù)完成的)
//切記要開啟中斷 且在中斷函數(shù)中 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)
#else
void AWU_IRQHandler(void) interrupt 1
#endif
{
AWU_GetFlagStatus();
}
IWDG應(yīng)用(直接操作寄存器完成的)
在需要清狗的地方調(diào)用WDT_CLR()即可
void WDT_CLR(void )
{
IWDG->KR = IWDG_KEY_REFRESH;
}
void START_WDT(void )
{
IWDG->KR =0X55;//打開保護(hù)
IWDG->PR = IWDG_Prescaler_256;// 28Khz 256分頻
IWDG->RLR = 254;//重載寄存器設(shè)定 每當(dāng)喂狗時 此值就會重載
IWDG->KR = IWDG_KEY_REFRESH;//先喂一次
IWDG->KR = IWDG_KEY_ENABLE;//啟動看門狗
}
WWDG應(yīng)用
WWDG-CR是向下的計(jì)數(shù)器,數(shù)到0x40就會復(fù)位,窗口的上限可以任你設(shè),最大7F,如果設(shè)成7F,幾乎就成了沒有窗口的看門狗了?!?/p>
窗口的意思是假如你設(shè)是0x60,那么只有在0X60-0X40間可以喂狗,其它時間喂,狗就會復(fù)位你的單片機(jī),所以喂狗前要先判斷狗餓否。
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 0x70
void WWDG_CLR(void)
{//狗餓否
if ((WWDG->CR)&0x7f)< window_max)WWDG_SetCounter(wwdg_reset) ;
}
main()
{
WWDG_Init(wwdg_reset,window_max );
while(1)
{
WWDG_CLR
}
}