Window watchdog (WWDG) is often used to monitor software failures caused by external interference or unforeseen logic conditions that result from an application deviating from a normal running sequence.
In short, the difference between the IWDG and the IWDG is to feed the dog by hand, and WWDG has a built-in interrupt, so you can use the Set Interrupt service function to feed the dog.
First, set the WWDG (remember to add a header file to the project) the approximate setup steps are as follows:
1) enable WWDG clock (System internal clock)
2) Set the window value and divide number
3) turn on WWDG interrupt and group
4) Set the counter initial value and enable the watchdog
5) write Interrupt service function
Wwdg.c
#include "led.h"
#include "wwdg.h"
//Save the setting value of the WWDG counter, the default is maximum.
U8 wwdg_cnt=0x7f;
void Wwdg_init (U8 tr,u8 wr,u32 fprer)
{
rcc_apb1periphclockcmd (rcc_apb1periph_wwdg,enable);
Wwdg_setprescaler (fprer);////set IWDG prescaler value
wwdg_setwindowvalue (WR);//Set window value
wwdg_enable (TR);//enable Watchdog, set Counter.
Wwdg_clearflag ();
Wwdg_nvic_init ();//Initialize window watchdog NVIC
Wwdg_enableit ();//Open window watchdog interrupt
}
void Wwdg_nvic_init (void)
{
nvic_inittypedef nvic_ist;
Nvic_ist. NVIC_IRQCHANNEL=WWDG_IRQN;
Nvic_ist. nvic_irqchannelpreemptionpriority=2;
Nvic_ist. nvic_irqchannelsubpriority=3;
Nvic_ist. nvic_irqchannelcmd=enable;
Nvic_init (&nvic_ist);
}
void Wwdg_irqhandler (void)
{
wwdg_setcounter (0x7f);//Feed Dog
wwdg_clearflag ();//Clear early Wake flag
led1=! LED1;//detects if the interrupt function is working
}
Wwdg.h
#ifndef _wwdg_h
#define _WWDG_H
#include "sys.h"
void Wwdg_init (U8 tr,u8 wr,u32 fprer);
void Wwdg_nvic_init (void);
void Wwdg_irqhandler (void);
#endif
One of the places was written wrong at first. The third parameter of Wwdg_init () is written in U8. As a result, the lights flashed super fast, and after a while they found the mistake. The parameter is estimated to go through.
Main function
#include "led.h"
#include "sys.h"
#include "delay.h"
#include "wwdg.h"
#include "usart.h"
void init (void)
{
delay_init ();
Uart_init (9600);
Led_init ();
Nvic_configuration ();
led0=0;
Delay_ms (+);
Wwdg_init (0x7f,0x5f,wwdg_prescaler_8);
}
int main (void)
{
init ();
while (1)
{
led0=1;
}
}
The main function is the implementation let LED0 first light, and then WWDG will work (do not let the program reset), and then interrupt function will work, the specific phenomenon is LED1 has been flashing, the beginning did not understand how the interruption triggered, turned back to the manual, the book is said: The second register of the window watchdog is the configuration register (WWDG_CFR), in which the EWI in the bit is an early wake-up interrupt, that is, a period of time (T[6:0]=0X40) before the reset is imminent to remind us that the dog needs to be fed, otherwise it will be reset. Therefore, we generally use this bit to set the interrupt, when the window watchdog counter value is reduced to 0X40, if the bit set and turn on the interrupt, then there will be an interrupt, we can in the interrupt to WWDG_CR to write the counter value, to feed the dog's purpose.
Simply speaking (just personal humble opinion) is WWDG is a descending counter, from the maximum to 0, to 0 if not feed the dog (that is, reset the counter, such as resetting it to the maximum value) will be reset, but the interrupt trigger is halfway through, when the decrement to 0x40 will trigger the interrupt, This is going to go to the interrupt service function we wrote, as long as we write the dog function in the interrupt service function to feed the dog all the time.