single-chip microcomputer system in the external interference will appear in the program to run the phenomenon of flying caused by the death cycle, watchdog circuit is to avoid the occurrence of this situation. the function of the watchdog is to realize the automatic reset of the processor (sending the reset signal) by not receiving the dog feed signal (which means that the MCU is already dead) within a certain time (through the timer counter).
Feeling is the automatic reset function, that is, in a certain time you should go to feed it and if the program midway into the dead loop (is not Fed) this MCU hangs. But if you do not open the IWDG, you do not know that it is not hanging (although the individual think it is also possible to manually press the reset button.) (The above paragraph is copied from a forum)
IWDG Initialization steps:
1 ) Take Register write protection (to iwdg_kr write 0x5555)
2 ) Sets the Prescaler factor and reload value of the independent watchdog
3 ) Reload Count value feed dog ( write 0XAAAA to Iwdg_kr)
4) start watchdog ( write 0XCCCC to Iwdg_kr )
。。 Of course, or away from the Register, library function Dafa is good
Iwdg.c
#include "iwdg.h" #include "sys.h"//Initialize independent watchdog//prer: Divide number: 0~7 (only low 3 bit valid!) Divide factor =4*2^prer. But the maximum value can only be 256!//RLR: Reload Register Value: Low 11 bits valid.//Time calculation (approximate): tout= ((4*2^prer) *RLR)/40 (ms). void Iwdg_init (U8 prer, U16 RLR) { //enable write operations to registers IWDG_PR and IWDG_RLR iwdg_writeaccesscmd (iwdg_writeaccess_enable); Iwdg_setprescaler (prer);//Set the IWDG prescaler value: Set IWDG prescaler value to iwdg_setreload (RLR);//Set IWDG reload value iwdg_reloadcounter (); Reload the IWDG counter by the value of the IWDG reload Register iwdg_enable ();//enable iwdg}void iwdg_feed (void)//feed dog {iwdg->kr=0xaaaa;}
Iwdg.h
#ifndef _iwdg_h#define _iwdg_h#include "sys.h" void Iwdg_init (U8 prer,u16 rlr); void iwdg_feed (void); #endif
The main function part is a bunch of initialization.
#include "led.h" #include "sys.h" #include "delay.h" #include "key.h" #include "iwdg.h" #include "usart.h" void init () {Led_ Init (); Key_init ();d elay_init (); Uart_init (9600); Nvic_configuration (); Iwdg_init (4,625);//Overflow time 1s}int main (void) {Init ();d Elay_ms (300); Led0=0;while (1) {if (Key_scan (0) ==key0_pres) iwdg_feed ();d Elay_ms (10);}}
The final effect of this experiment is: if you keep pressing KEY0 feed the dog, it will not be reset, or it will be reset, the actual performance is always press KEY0 to let the LED has been lit, otherwise the LED will blink
Cortex_m3_stm32 Embedded Learning Note (v): Independent watchdog experiment (IWDG)