Independent watchdog experiment of STM32F103ZET6
Article Directory
Preface
The learning of STM32 can be divided into 3 versions.
1. Register version
2. Library function version
3. HAL library version
Due to personal reasons, I choose the library function version to learn STM32.
Tip: Problems such as software installation will not be explained! ! !
One, watchdog related overview
1. Concept
2. Principle
2. Related registers
1. Key value register (IWDG_KR)
2. Prescaler register (IWDG_PR)
3. Reload register (IWDG_RLR)
4. Status register (IWDG_SR)
Three, operation steps
1. Independent watchdog timeout calculation
2. Operation steps
Fourth, the program source code
1.iwdg.h
code show as below:
#ifndef __IWDG_H
#define __IWDG_H
#include "sys.h"
void IWDG_Init(u8 prer,u16 rlr);
#endif
2.iwdg.c
code show as below:
#include "iwdg.h"
void IWDG_Init(u8 prer,u16 rlr)
{
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);//取消寄存器写保护
IWDG_SetPrescaler(prer);//设置独立看门狗的预分频系数
IWDG_SetReload(rlr);//设置看门狗重装载值
IWDG_Enable();//使能看门狗
}
3.main.c
code show as below:
#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "beep.h"
#include "key.h"
#include "usart.h"
#include "exti.h"
#include "iwdg.h"
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init();
LED_Init();
Beep_Init();
KEY_Init();
uart_init(115200);
EXTIX_Init();
IWDG_Init(4,625);//1s 1000ms=4*2^4/40*rlr
delay_ms(200);
LED1=0;
while(1)
{
if(KEY_SCAN(1)==KEY0_PRES)
{
IWDG_ReloadCounter();
}
}
}
5. Experimental results
When there is no operation, LED0 lights up every 200ms, and the reset operation is always performed;
when the KEY0 button is pressed once, it is equivalent to "feeding the dog" once, and the reset operation is not performed at this time;
when the KEY0 button is kept pressed, the LED0 is off. It has been "feeding the dog" at the time, and the reset operation has not been possible;
to sum up
As long as you write 0XAAAA to IWDG_KR once within one second, it will not cause the watchdog to reset (of course, multiple writes are also possible).