Files
HBshuiwuConcentrator/APP/wdg.c
2025-12-15 16:07:49 +08:00

53 lines
1.5 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "wdg.h"
/*****************************************************************************
* Function : Iwdg_Init
* Description : none
* Input : u8 timeout 为以秒为单位的看门狗超时时间不得低于1s,看门狗定时器的数据不能超过u16的大小65536
* Output : None
* Return :
* Others :
* Record
* 1.Date : 20170412
* Author : barry
* Modification: Created function
*****************************************************************************/
void Iwdg_Init(u8 timeout)
{
#define WDG_PRE 256
#define WDG_PER_COUNT (40000/WDG_PRE)
#define IWDG_Prescaler IWDG_Prescaler_256
u8 max_timeout = 0xFFF / WDG_PER_COUNT;
u16 reloadCount = 0;
timeout = (timeout == 0)? 1:timeout;
timeout = (timeout >= max_timeout)? max_timeout: timeout;
reloadCount = timeout* WDG_PER_COUNT;
/* Check if the system has resumed from IWDG reset */
if (RCC_GetFlagStatus(RCC_FLAG_IWDGRST) != RESET)
{
RCC_ClearFlag();
}
/* IWDG timeout equal to 280 ms (the timeout may varies due to LSI frequency
dispersion) */
/* Enable write access to IWDG_PR and IWDG_RLR registers */
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
/* IWDG counter clock: 40KHz(LSI) / 32 = 1.25 KHz */
IWDG_SetPrescaler(IWDG_Prescaler);
/* Set counter reload value to 1250, 1s timeout */
IWDG_SetReload(reloadCount);
/* Reload IWDG counter */
IWDG_ReloadCounter();
/* Enable IWDG (the LSI oscillator will be enabled by hardware) */
IWDG_Enable();
}