STM8L探索套件學(xué)習(xí)筆記-獨(dú)立看門(mén)狗IWDG(十六)
例程展示如何重載IWDG計(jì)數(shù)值,IWDG的時(shí)間設(shè)置為214.7ms。程序采用“Refresh”變量來(lái)使能IWDG重載計(jì)數(shù)值仿真IWDG復(fù)位,并且板上LED4翻轉(zhuǎn)。當(dāng)按鍵按下,模擬一次軟件錯(cuò)誤造成程序沒(méi)有喂狗,從而發(fā)生復(fù)位。如果IWDG復(fù)位,系統(tǒng)啟動(dòng)后LED3會(huì)翻轉(zhuǎn)8次,主代碼如下:
void main(void)
{
uint8_t Index;
GPIO_Init(LED3_PORT,LED3_PIN,GPIO_Mode_Out_PP_Low_Fast);
//輸出低電平-高速10M
GPIO_Init(LED4_PORT,LED4_PIN,GPIO_Mode_Out_PP_Low_Fast);
//輸出低電平-高速10M
GPIO_Init(KEY_PORT,KEY_PIN,GPIO_Mode_In_FL_IT);
//輸入浮空-中斷
/* Set PC1 sensitivity to falling edge and low level 下降沿低電平觸發(fā)*/
EXTI_SetPinSensitivity(EXTI_Pin_1, EXTI_Trigger_Falling_Low);
/* Check if the MCU has resumed from IWDG reset */
if (RST_GetFlagStatus(RST_FLAG_IWDGF) != RESET)
{
/* IWDGF flag set */
/* Toggle LED3 */
for (Index = 7; Index != 0; Index--)
{
GPIO_ToggleBits(LED3_PORT,LED3_PIN);
Delay(0x7FFF);
}
/* Clear IWDGF Flag */
RST_ClearFlag(RST_FLAG_IWDGF);
}
/* IWDG configuration: IWDG is clocked by LSI = 38KHz */
/* IWDG timeout equal to 214.7 ms (the timeout may varies due to LSI frequency dispersion) */
/* IWDG timeout = (RELOAD_VALUE + 1) * Prescaler / LSI = (254 + 1) * 32 / 38 000 = 214.7 ms */
/* Enable the IWDG */
IWDG_Enable();
/* Enable write access to IWDG_PR and IWDG_RLR registers */
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
/* Set IWDG timeout */
IWDG_SetPrescaler(IWDG_Prescaler_32);
IWDG_SetReload(RELOAD_VALUE);
/* Refresh IWDG */
IWDG_ReloadCounter();
/* enable interrupts by switching to level 0 */
enableInterrupts();
while (1)
{
/* As Refresh is equal to ENABLE no IWDG reset will occur. When Refresh is
equal to DISABLE, the IWDG counter isnt refreshed and IWDG reset will occur. */
if (Refresh != 0)
{
/* Reload IWDG counter */
IWDG_ReloadCounter();
}
/* Toggle LED4 */
GPIO_ToggleBits(LED4_PORT,LED4_PIN);
Delay(0x6FFF);
}
}
評(píng)論