1. iwdg.c
#include "stm32f4xx.h"
#include "iwdg.h"
//prer:预分频值
//rlr:自动重装载值
void IWDG_Init(unsigned char prer,unsigned int rlr)//IWDG初始化
{
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);//取消写保护,方便后续调用寄存器
IWDG_SetPrescaler(prer);//设置预分频值
IWDG_SetReload(rlr);//设置重装载值
IWDG_ReloadCounter();//把重装载寄存器rlr的值加载到KR寄存器中,进行看门狗喂狗
IWDG_Enable();//看门狗使能
}
void IWDG_FEED(void)//看门狗喂狗
{
IWDG_ReloadCounter();//往KR寄存器中写0xAAAA;
}
2. iwdg.h
#ifndef _IWDG__H_
#define _IWDG__H_
#include "sys.h"
void IWDG_Init(unsigned char prer,unsigned int rlr);
void IWDG_FEED(void);
#endif
3. main.c
IWDG_Init(4,500);//初始化看门狗喂狗 (4*2^4)*500/32=1000ms=1s 设置看门狗的溢出时间为1秒
IWDG_FEED();//喂狗
其中:
预分频值取值范围:0-7;(3bit)
重加载值的取值范围;0-4095; (12bit)
时间设置范围:
STM32F4_独立看门狗详解(IWDG)_stm32f4 看门狗_light_2025的博客-CSDN博客