STM32CubeMX系列教程15:看门狗(WDG)
在main()函数中开启IWDG.
/* USER CODE BEGIN 2 */ /* Start the IWDG */ HAL_IWDG_Start(&hiwdg); printf("\n\r***** WaveShare Open7XXI-C Board *****\n\r"); /* USER CODE END 2 */
在while循环中每延时800ms刷新一次IWDG,重载计数器。
/* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ /* Set counter reload value to obtain 1000ms IWDG TimeOut. IWDG counter clock Frequency = LsiFreq / 32 Counter Reload Value = 1000 */ printf("\n\r Refreshes the IWDG !!!\n\r"); /* Refresh IWDG: reload counter */ HAL_IWDG_Refresh(&hiwdg); HAL_Delay(800); } /* USER CODE END 3 */
当在while循环中注释掉HAL_IWDG_Refresh(&hiwdg)语句,不刷新计数器。重新编译程序时串口会输出如下信号,由于不刷新计数器,独立看门狗每1000ms复位重启一次。
在main()函数中开启IWDG.
/* USER CODE BEGIN 2 */ /*##-1- Start the WWDG #####################################################*/ HAL_WWDG_Start_IT(&hwwdg); printf("\n\r***** WaveShare Open7XXI-C Board *****\n\r"); /* USER CODE END 2 */
在while循环中每延时1000ms输出一次信息。
/* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ printf("\n\r www.waveshare.net !!!\n\r"); HAL_Delay(1000); } /* USER CODE END 3 */
在main.c文件后面添加WWDG中断回调函数,当递减计数器的值为0x40触发中断,中断处理函数中刷新WWDG。
/* USER CODE BEGIN 4 */ /** * @brief Early Wakeup WWDG callback. * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains * the configuration information for the specified WWDG module. * @retval None */ void HAL_WWDG_WakeupCallback(WWDG_HandleTypeDef* hwwdg) { /* NOTE: This function Should not be modified, when the callback is needed, the HAL_WWDG_WakeupCallback could be implemented in the user file */ /*##-2- Refresh the WWDG #####################################################*/ HAL_WWDG_Refresh(hwwdg, 127); } /* USER CODE END 4 */
当在WWDG中断回调函数中注释掉HAL_WWDG_Refresh(hwwdg, 127)语句,不刷新计数器。重新编译程序时串口会输出如下信号,由于不刷新计数器,独立看门狗每39ms复位重启一次。