解答多年疑惑。。。
接下来还需要使能中断号。。。
还有对应时钟别忘了使能。。。
接着搞main.c
实验成功。。。
窗口期内喂狗和中断期内喂狗同步进行(关键注意理解其计数值递减和delay()是相互独立的,即想应中断完成后其会恢复延时函数的现场)。。。
main.c
#include "./SYSTEM/sys/sys.h"
#include "./SYSTEM/usart/usart.h"
#include "./SYSTEM/delay/delay.h"
#include "./BSP/LED/led.h"
#include "./BSP/WWDG/wwdg.h"
//int temp[10] = {15,14,13};
//int temp;
int main(void)
{
HAL_Init(); /* 初始化HAL库 */
sys_stm32_clock_init(336, 8, 2, 7); /* 设置时钟,168Mhz */
delay_init(168); /* 延时初始化 */
usart_init(115200);
led_init(); /* 初始化LED */
if(__HAL_RCC_GET_FLAG(RCC_FLAG_WWDGRST)!= RESET)
{
printf("窗口看门狗复位\r\n");
__HAL_RCC_CLEAR_RESET_FLAGS();
}
else{
printf("外部复位\r\n");
}
delay_ms(300);
printf("请在窗口期喂狗\r\n\r\n");
wwdg_init(0x7f,0x5f,WWDG_PRESCALER_8);
while(1)
{
delay_ms(75);
HAL_WWDG_Refresh(&g_wwdg_handle);
//LED1_TOGGLE();
}
}
wwdg.h
#ifndef __WWDG_H
#define __WWDG_H
#include "./SYSTEM/sys/sys.h"
extern WWDG_HandleTypeDef g_wwdg_handle;
void wwdg_init(uint8_t tr,uint8_t wr,uint32_t fprer);
#endif
wwdg.c
#include "./BSP/WWDG/wwdg.h"
#include "./BSP/LED/led.h"
#include "./SYSTEM/delay/delay.h"
WWDG_HandleTypeDef g_wwdg_handle;
//窗口看门狗初始化函数
void wwdg_init(uint8_t tr,uint8_t wr,uint32_t fprer){
g_wwdg_handle.Instance = WWDG;
g_wwdg_handle.Init.Counter = tr;
g_wwdg_handle.Init.Window = wr;
g_wwdg_handle.Init.Prescaler = fprer;
g_wwdg_handle.Init.EWIMode = WWDG_EWI_ENABLE;
HAL_WWDG_Init(&g_wwdg_handle);//注意其实参和形参对应的变量要统一。。。
}
//WWDG MSP回调函数
void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg){//注意这里无需改动
__HAL_RCC_WWDG_CLK_ENABLE();//时钟使能
HAL_NVIC_SetPriority(WWDG_IRQn,2,3);//设置对应中断优先级(相应优先级分组
//已在对应的HAL库函数中设置过了)
HAL_NVIC_EnableIRQ(WWDG_IRQn);//使能中断
}
//WWDG中断服务函数
void WWDG_IRQHandler(void){
HAL_WWDG_IRQHandler(&g_wwdg_handle);
}
//WWDG提前唤醒回调函数
void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef *hwwdg){
//delay_ms(1);
HAL_WWDG_Refresh(&g_wwdg_handle);
//LED0_TOGGLE();
}