|
- #include "stm32l4xx_hal.h"
- #include "periph.h"
- static WWDG_HandleTypeDef wwdgHandle;
- static uint32_t TimeoutCalculation(uint32_t timevalue);
- int main()
- {
- uint32_t delay;
- HAL_Init();
- SystemClock_Config();
- __HAL_RCC_WWDG_CLK_ENABLE(); //使能wwdg时钟
- Gpio_Inits();
-
- HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET);
- HAL_Delay(1300);
- /*
- 设置分频系数8
- WWDG counter clock = (PCLK1/4096)/8
- 设置窗口值 最大0x7f 到窗口0x50
- 唤醒中断关闭
- */
- wwdgHandle.Instance =WWDG; //窗口看门狗
- wwdgHandle.Init.Prescaler = WWDG_PRESCALER_8; //设置8分频
- wwdgHandle.Init.Window = 0X50;
- wwdgHandle.Init.Counter=0x7f;
- wwdgHandle.Init.EWIMode=WWDG_EWI_DISABLE; //唤醒中断
- HAL_WWDG_Init(&wwdgHandle);
- delay = TimeoutCalculation((wwdgHandle.Init.Counter-wwdgHandle.Init.Window) + 1) + 1;
- while(1)
- {
- HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);
- HAL_Delay(delay); //延时到窗口时间
- HAL_WWDG_Refresh(&wwdgHandle); //喂狗
- }
- }
- static uint32_t TimeoutCalculation(uint32_t timevalue)
- {
- uint32_t timeoutvalue = 0;
- uint32_t pclk1 = 0;
- uint32_t wdgtb = 0;
- /* considering APB divider is still 1, use HCLK value */
- pclk1 = HAL_RCC_GetPCLK1Freq();
- /* get prescaler */
- wdgtb = (1 << ((wwdgHandle.Init.Prescaler) >> 7)); /* 2^WDGTB[1:0] */
-
- /* calculate timeout */
- timeoutvalue = ((4096 * wdgtb * timevalue) / (pclk1 / 1000));
- return timeoutvalue;
- }
复制代码 |
|