STM8L探索套件学习笔记-窗口看门狗WWDG(十七)
时间:11-24
来源:互联网
点击:
窗口看门狗(WWDG)通常被用来监测由外部干扰或不可预见的逻辑条件造成的应用程序背离正常的运行序列而产生的软件故障。除非递减计数器的值在T6位变成0前被刷新,看门狗电路在达到预置的时间周期时,会产生一个MCU复位。在递减计数器达到窗口寄存器数值之前,如果7位的递减计数器数值(在控制寄存器中)被刷新,那么也将产生一个MCU复位。这表明递减计数器需要在一个有限的时间窗口中被刷新。上面提到的T6即是窗口看门狗的自减计数器的第六位(最高位)。在该计数器的T6位变为0后(小于0X40),就会引起一复位。这是窗口的下限。而当计数器的值在大雨窗口配置寄存器的窗口值之前就被修改的话,也会引起一次复位,这里窗口值是上限。窗口值是由用户自己设定的,根据实际要求来设计窗口值,但是一定要确保窗口值大于0X40,否则窗口就不存在了
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 下降沿低电平触发*/
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);
}
}
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 下降沿低电平触发*/
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);
}
}
STM8L探学习笔记窗口看门狗WWD 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)