STM8L探索套件學(xué)習(xí)筆記-窗口看門狗WWDG(十七)
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 WWDG reset */
if (RST_GetFlagStatus(RST_FLAG_WWDGF) != RESET)
{
/* IWDGF flag set */
/* Toggle LED3 */
for (Index = 7; Index != 0; Index--)
{
GPIO_ToggleBits(LED3_PORT,LED3_PIN);
Delay(0x7FFF);
}
/* Clear WWDGF Flag */
RST_ClearFlag(RST_FLAG_WWDGF);
}
/* WWDG configuration: WWDG is clocked by SYSCLK = 2MHz */
/* WWDG timeout is equal to 251,9 ms */
/* Watchdog Window = (COUNTER_INIT - 63) * 1 step
= 41 * (12288 / 2Mhz)
= 251,9 ms
*/
/* Non Allowed Window = (COUNTER_INIT - WINDOW_VALUE) * 1 step
= (104 - 97) * 1 step
= 7 * 1 step
= 7 * (12288 / 2Mhz)
= 43.008 ms
*/
/* So the non allowed window starts from 0.0 ms to 43.008 ms
and the alowed window starts from 43.008 ms to 251,9 ms
If refresh is done during non allowed window, a reset will occur.
If refresh is done during allowed window, no reset will occur.
If the WWDG down counter reaches 63, a reset will occur. */
WWDG_Init(COUNTER_INIT, WINDOW_VALUE);
/* enable interrupts by switching to level 0 */
enableInterrupts();
while (1)
{
/* Check if WWDG counter refresh is allowed in Allowed window */
if (AllowedRefresh != 0)
{
/* get WWDG counter value */
/* wait until WWDG counter becomes lower than window value */
while ((WWDG_GetCounter() & 0x7F) > WINDOW_VALUE);
/* Refresh WWDG counter during allowed window so no MCU reset will occur */
WWDG_SetCounter(COUNTER_INIT);
}
/* Check if WWDG counter refresh is allowed in non Allowed window */
if (NonAlowedRefresh != 0)
{
/* wait until WWDG counter becomes higher than window value */
while ((WWDG_GetCounter() & 0x7F) < WINDOW_VALUE);
/* Refresh WWDG counter during non allowed window so MCU reset will occur */
WWDG_SetCounter(COUNTER_INIT);
}
/* Toggle LED4 */
GPIO_ToggleBits(LED4_PORT,LED4_PIN);
Delay(0x6FFF);
}
}
評(píng)論