一.IWDG
1.原理
启用IWDG后,LSI时钟会自动开启
2.IWDG溢出时间计算
3.IWDG配置步骤
4.HAL库相关函数介绍
HAL_IWDG_Init //使能IWDG,设置预分频系数和重装载值等
HAL_IWDG_Refresh //把重装载寄存器的值重载到计数器中,喂狗typedef struct
{ IWDG_TypeDef *Instance; /* IWDG 寄存器基地址 */IWDG_InitTypeDef Init; /* IWDG 初始化参数 */
}IWDG_HandleTypeDef;typedef struct
{ uint32_t Prescaler; /* 预分频系数 */ uint32_t Reload; /* 重装载值 */
} IWDG_InitTypeDef;
5.自己编写的初始化函数
IWDG_HandleTypeDef g_iwdg_handle;/* IWDG初始化函数 */
void iwdg_init(uint8_t prer, uint16_t rlr)
{g_iwdg_handle.Instance = IWDG;g_iwdg_handle.Init.Prescaler = prer;g_iwdg_handle.Init.Reload = rlr;HAL_IWDG_Init(&g_iwdg_handle);
}/* 喂狗函数 */
void iwdg_feed(void)
{HAL_IWDG_Refresh(&g_iwdg_handle);
}
int main(void)
{HAL_Init(); /* 初始化HAL库 */sys_stm32_clock_init(RCC_PLL_MUL9); /* 设置时钟为72Mhz */delay_init(72); /* 延时初始化 */usart_init(115200); /* 串口初始化为115200 */printf("您还没喂狗,请及时喂狗!!!\r\n");iwdg_init(IWDG_PRESCALER_32, 1250); /* 预分频系数为32,重装载值为1250,溢出时间约为1s */while (1){delay_ms(1000);iwdg_feed();printf("已经喂狗\r\n");}
}
二.WWDG
1.WWDG超时时间计算
2.WWDG配置步骤
3.相关寄存器介绍
HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg);//设置预分频系数和窗口值等
HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg);//重装载计数器,即喂狗typedef struct
{WWDG_TypeDef *Instance; /* WWDG 寄存器基地址 */ WWDG_InitTypeDef Init; /* WWDG 初始化参数 */
}WWDG_HandleTypeDef;typedef struct
{uint32_t Prescaler; /* 预分频系数 */uint32_t Window; /* 窗口值 */uint32_t Counter; /* 计数器值 */uint32_t EWIMode; /* 提前唤醒中断使能 */
}WWDG_InitTypeDef;
4.自己编写的初始化函数
设置计数器值为0x7F,窗口值0x5F,预分频系数是8
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_NVIC_EnableIRQ(WWDG_IRQn);
}/* WWDG中断服务函数 */
void WWDG_IRQHandler(void)
{HAL_WWDG_IRQHandler(&g_wwdg_handle);
}/* WWDG提前唤醒回调函数 */
void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef *hwwdg)
{HAL_WWDG_Refresh(&g_wwdg_handle);LED1_TOGGLE();
}
int main(void)
{HAL_Init(); /* 初始化HAL库 */sys_stm32_clock_init(RCC_PLL_MUL9); /* 设置时钟,72M */delay_init(72); /* 初始化延时函数 */usart_init(115200); /* 波特率设置为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(500);printf("请在窗口期内喂狗\r\n\r\n");wwdg_init(0x7f, 0x5f, WWDG_PRESCALER_8);while(1){delay_ms(90);//延迟90msHAL_WWDG_Refresh(&g_wwdg_handle);LED0_TOGGLE();}
}