STM32 CubeMx HAL库 独立看门狗IWDG配置使用

看门狗这里我就不多介绍了,能搜到这篇文章说明你了解

总之就是一个单片机重启程序,设定好超时时间,在超时时间内没有喂狗,单片机就会复位

主要应用在单片机异常重启方面,比如程序跑飞(注意程序跑飞时你就没有喂狗,然后就会重启单片机,注意看门狗独立于程序之外,看门狗不会随着程序跑飞)

1,cubeMX配置

1.打开看门狗,配置看门狗相关参数

我设置的超时时间为10s

超时时间=(预分频值*重载值)/时钟频率

10s=(256*1250)/32000

至于为什么是这样计算

IWDG时钟32000经过分频后为32000/256=125 也就是每秒计数125次

重载值reload value1250表示计数1250次内不喂狗就重启

所以超时时间=1250/125=10s

至于那个window value (有些芯片是没有这个值的)大家可以去了解了解WWDG窗口看门狗,也就是只能在窗口期喂狗

窗口期=window value/reload value *超时时间~超时时间

在上面例子 窗口期=600/1250*10~10s

也就是4.8s~10s才能喂狗

不喂狗或者在4.8s之前喂狗都会复位单片机

(我用的是stm32l471rgt6芯片)看门狗时钟频率32kHz

2,打开串口1(可选)

为了调试方便

3.生成代码(基本配置默认你们都会)

勾选生成.c .h文件

我用的是clion开发,所以生成是STM32CubeIDE 用keil的就MDK就行

2.代码编写

就写喂狗函数就好了,初始化cubemx已经写好了

cpp 复制代码
HAL_IWDG_Init(&hiwdg)//初始化
HAL_IWDG_Refresh(&hiwdg);//喂狗

main.c代码

cpp 复制代码
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "can.h"
#include "iwdg.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_CAN1_Init();
  MX_USART1_UART_Init();
  MX_USART2_UART_Init();
  MX_IWDG_Init();
  /* USER CODE BEGIN 2 */
    uint8_t rrr[6]="program begin";
    HAL_UART_Transmit(&huart1, rrr, 13, 1000);
    uint32_t time =HAL_GetTick();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */

  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
    //每7秒喂狗    10s内不喂狗会重启
    if(HAL_GetTick()-time>7000){
        HAL_UART_Transmit(&huart1, "feed dog\r\n", 10, 1000);
        HAL_IWDG_Refresh(&hiwdg);
        time=HAL_GetTick();
    }
      HAL_UART_Transmit(&huart1, "loop\r\n", 6, 1000);
      HAL_Delay(1000);
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 1;
  RCC_OscInitStruct.PLL.PLLN = 20;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

效果如下

相关推荐
大风起兮125 小时前
ESP32,uart安装驱动uart_driver_install函数剖析,以及intr_alloc_flags 参数的意义
开发语言·单片机·嵌入式硬件
君琴6 小时前
SOME/IP协议详解 基础解读 涵盖SOME/IP协议解析 SOME/IP通讯机制 协议特点 错误处理机制
网络·单片机·嵌入式硬件·网络协议
富利威-make6 小时前
THB6128安森美步进电机驱动芯片
图像处理·stm32·单片机·嵌入式硬件·视频编解码·相机
小猪写代码8 小时前
STM32 FreeRTOS 的任务挂起与恢复以及查看任务状态
stm32·单片机·嵌入式硬件
小猪写代码8 小时前
STM32 FreeRTOS 基础知识
stm32·单片机·实时操作系统
捕鲸叉9 小时前
STM32 C++编程,怎样使用printf函数从串口输出中文字符
单片机·嵌入式硬件·调试 诊断
电子科技圈9 小时前
成功案例分享 — 芯科科技助力涂鸦智能打造Matter over Thread模块,简化Matter设备开发
人工智能·科技·嵌入式硬件·mcu·物联网·网络安全·iot
嗯? 嗯。10 小时前
STM32特殊功能引脚详解文章·STM32特殊功能引脚能当作GPIO使用嘛详解!!!
嵌入式硬件·stm32特殊功能引脚·stm32特殊功能引脚详解文章·stm32特殊功能引脚服用·stm32 afio 特殊引脚
weixin_4629019711 小时前
用HAL_GetTick()函数实现简单任务调用
单片机·嵌入式硬件
生活很暖很治愈11 小时前
从玩具到工业控制--51单片机的跨界传奇【2】
嵌入式硬件·51单片机