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 */

效果如下

相关推荐
wenchm2 小时前
细说STM32F407单片机SPI基础知识
stm32·单片机·嵌入式硬件
jikuaidi6yuan2 小时前
STM32 ADC 配置
stm32·单片机·嵌入式硬件
Be Legendary-CGK4 小时前
有源模拟滤波器的快速设计
嵌入式硬件·硬件工程
Anin蓝天(北京太速科技-陈)4 小时前
204-基于Xilinx Virtex-6 XC6VLX240T 和TI DSP TMS320C6678的信号处理板
嵌入式硬件·fpga开发·信号处理
Jack电子实验室5 小时前
STM32 出租车计价器系统设计(一) 江科大源码改写
c语言·stm32·单片机·嵌入式硬件·嵌入式
腾飞的信仰5 小时前
51单片机 串口UART
单片机·嵌入式硬件·51单片机
电子科技圈5 小时前
XMOS将在CES 2025上展出多款由边缘AI驱动的创新音效、音频、识别和处理解决方案
人工智能·科技·嵌入式硬件·mcu·物联网·音视频·iot
lantiandianzi6 小时前
基于单片机的输液速度监控系统设计
单片机·嵌入式硬件
紫阡星影7 小时前
【模块系列】STM32&PCF8563
c语言·stm32·单片机·嵌入式硬件
憧憬一下8 小时前
深入解析PCIe设备事务层与配置过程
arm开发·嵌入式硬件·嵌入式·pcie·linux驱动开发