当前位置:   article > 正文

STM32—看门狗详解入门(独立看门狗、窗口看门狗)

STM32—看门狗详解入门(独立看门狗、窗口看门狗)

目录

一、什么是看门狗?

二、独立看门狗IWDG

1.特点、本质及原理

2.配置

3.main.c代码

三、窗口看门狗WWDG

1.特点、本质及原理

2.配置

3.main.c代码

四、独立看门狗与窗口看门狗的比较


一、什么是看门狗?

        看门狗主要是用来监测单片机运行状态和解决程序引起的故障的模块。

         独立看门狗:

        由于单片机的工作会受到外界的干扰,可能造成程序进入死循环或者程序跑飞,程序的正常运行被打断,单片机控制的系统无法继续工作,会造成整个系统的陷入停滞状态。为了防止进入这个情况便产生了一种专门用于实时监测单片机程序运行状态的模块或者芯片,俗称“看门狗”。

        窗口看门狗:

        用于监测单片机程序运行时效是否精准,主要检测软件异常。

二、独立看门狗IWDG

1.特点、本质及原理

        1.特点:工作在主程序之外,能够完全独立工作,它的时钟是专用的低速时钟(LSI),由 VDD 电压供电, 在停止模式和待机模式下仍能工作,主要检测硬件的正常运行。

        2.本质:1.是一个 12 位的递减计数器,当计数器的值从某个值一直减到0的时候,系统就会产生一个复位信号,即 IWDG_RESET ,CPU收到复位信号,系统复位重新运行。

                   2.在计数没减到0之前,重置了计数器的值的话,那么就不会产生复位信号,CPU收不到复位信号,系统就会正常运行不会复位,这个动作就是我们说的喂狗。

        3.原理框图:

        4.时钟:独立看门狗的时钟由独立的RC振荡器LSI提供,即使主时钟发生故障它依然可以运行。启用 IWDG后,LSI时钟会自动开启,无法停止可以重置。LSI经过一个8位的预分频器得到计数器时钟,LSI时钟频率并不精确,F1用40kHz。

        5.预分频寄存器(IWDG_PR):

              (1)分频系数算法:prer是IWDG_PR 的值。

        6.重装载寄存器

        重装载寄存器是一个12位的寄存器,用于存放重装载值,低12位有效,即最大值为4096,这个值的大小决定着独立看门狗的溢出时间。       

         7.键寄存器

        是一个控制寄存器,主要有三种控制方式。由位15:0决定.

        8. 溢出时间计算公式

                PSC是分频系数、RLR、是LSI时钟频率一般用40KHz。

                                        

2.配置

        实现:溢出时间1s,按钮喂狗,串口查看状态

        1.独立看门狗配置,设置溢出时间1s。PSC=64、RLR=625

        2.按钮PA0 GPIO设置为输入input。

        3.串口USART1打开,异步通讯(非中断) 

        4.时钟72KHz

3.main.c代码

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2023 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "iwdg.h"
  22. #include "usart.h"
  23. #include "gpio.h"
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include <string.h>//strlen所需头文件============================================
  27. /* USER CODE END Includes */
  28. /* Private typedef -----------------------------------------------------------*/
  29. /* USER CODE BEGIN PTD */
  30. /* USER CODE END PTD */
  31. /* Private define ------------------------------------------------------------*/
  32. /* USER CODE BEGIN PD */
  33. /* USER CODE END PD */
  34. /* Private macro -------------------------------------------------------------*/
  35. /* USER CODE BEGIN PM */
  36. /* USER CODE END PM */
  37. /* Private variables ---------------------------------------------------------*/
  38. /* USER CODE BEGIN PV */
  39. /* USER CODE END PV */
  40. /* Private function prototypes -----------------------------------------------*/
  41. void SystemClock_Config(void);
  42. /* USER CODE BEGIN PFP */
  43. /* USER CODE END PFP */
  44. /* Private user code ---------------------------------------------------------*/
  45. /* USER CODE BEGIN 0 */
  46. /* USER CODE END 0 */
  47. /**
  48. * @brief The application entry point.
  49. * @retval int
  50. */
  51. int main(void)
  52. {
  53. /* USER CODE BEGIN 1 */
  54. /* USER CODE END 1 */
  55. /* MCU Configuration--------------------------------------------------------*/
  56. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  57. HAL_Init();
  58. /* USER CODE BEGIN Init */
  59. /* USER CODE END Init */
  60. /* Configure the system clock */
  61. SystemClock_Config();
  62. /* USER CODE BEGIN SysInit */
  63. /* USER CODE END SysInit */
  64. /* Initialize all configured peripherals */
  65. MX_GPIO_Init();
  66. MX_IWDG_Init();
  67. MX_USART1_UART_Init();
  68. /* USER CODE BEGIN 2 */
  69. //程序启动就让串口发送信息=========================================================
  70. HAL_UART_Transmit(&huart1,"程序启动...",strlen("程序启动..."),100);
  71. /* USER CODE END 2 */
  72. /* Infinite loop */
  73. /* USER CODE BEGIN WHILE */
  74. while (1)
  75. {
  76. /* USER CODE END WHILE */
  77. //检测按钮是否被按下==========================================================
  78. if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET)
  79. HAL_IWDG_Refresh(&hiwdg);//重置看门狗计数
  80. HAL_Delay(50);//循环延时50ms
  81. /* USER CODE BEGIN 3 */
  82. }
  83. /* USER CODE END 3 */
  84. }
  85. /**
  86. * @brief System Clock Configuration
  87. * @retval None
  88. */
  89. void SystemClock_Config(void)
  90. {
  91. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  92. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  93. /** Initializes the RCC Oscillators according to the specified parameters
  94. * in the RCC_OscInitTypeDef structure.
  95. */
  96. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
  97. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  98. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  99. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  100. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  101. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  102. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  103. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  104. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  105. {
  106. Error_Handler();
  107. }
  108. /** Initializes the CPU, AHB and APB buses clocks
  109. */
  110. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  111. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  112. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  113. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  114. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  115. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  116. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  117. {
  118. Error_Handler();
  119. }
  120. }
  121. /* USER CODE BEGIN 4 */
  122. /* USER CODE END 4 */
  123. /**
  124. * @brief This function is executed in case of error occurrence.
  125. * @retval None
  126. */
  127. void Error_Handler(void)
  128. {
  129. /* USER CODE BEGIN Error_Handler_Debug */
  130. /* User can add his own implementation to report the HAL error return state */
  131. __disable_irq();
  132. while (1)
  133. {
  134. }
  135. /* USER CODE END Error_Handler_Debug */
  136. }
  137. #ifdef USE_FULL_ASSERT
  138. /**
  139. * @brief Reports the name of the source file and the source line number
  140. * where the assert_param error has occurred.
  141. * @param file: pointer to the source file name
  142. * @param line: assert_param error line source number
  143. * @retval None
  144. */
  145. void assert_failed(uint8_t *file, uint32_t line)
  146. {
  147. /* USER CODE BEGIN 6 */
  148. /* User can add his own implementation to report the file name and line number,
  149. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  150. /* USER CODE END 6 */
  151. }
  152. #endif /* USE_FULL_ASSERT */

三、窗口看门狗WWDG

1.特点、本质及原理

        1.特点:用于监测单片机程序运行时效是否精准,主要检测软件异常,一般用于需要精准检测程序运行时间的场合。

        2.本质:是一个能产生系统复位信号提前唤醒中断的6位计数器。

         (1)产生复位条件: 1.当递减计数器值从 0x40 减到 0x3F 时复位(即T6位跳变到0)

                                             2.计数器的值大于 W[6:0] 值时喂狗会复位。

         (2)产生中断条件: 1.当递减计数器等于 0x40 时可产生提前唤醒中断 (EWI)。

         (3)要在窗口期内重装载计数器的值,才能防止复位,也就是所谓的喂狗。

        3.原理 

          (1)窗口上限值:可以修改

          (2)中断:可以设置是否中断

          (3)提前唤醒: 作用:可以在复位之前保存一些数据。        

         4.框图

                (1)PCLK1最大36M

         5.控制寄存器(WWDG_CR)

                激活看门狗、计数器

         6.配置寄存器(WWDG_CFR)

                提前唤醒中断、预分频器、窗口值

         7.状态寄存器(WWDG_SR)

                提前唤醒中断标志

         8.超时时间计算

                          

            参数:

                (1)Tout是WWDG超时时间(没喂狗)

                (2)Fwwdg是WWDG的时钟源频率(最大36M)

                (3)4096是WWDG固定的预分频系数

                (4)2^WDGTB是WWDG_CFR寄存器设置的预分频系数值 

                (5)T[5:0]是WWDG计数器低6位,最多63 

2.配置

        1.实现:程序启动时点 亮 LED1 ,300ms 后熄灭。在提前唤醒中断服务函数进行喂狗,同时翻转 LED2 状态。

        2.值: T[6.0] 七位计数器设置为 127   

                    W[6:0] 七位窗口值设置 为95

                    2^WDGTB  预分频系数设置 为8

       3.时钟源频率Fwwdg 设置为36(来自RCC时钟控制器)

         4.WWDG配置

 

         5.LDE灯引脚设置  PB8 输出 开始时高电平、PB9 输出 开始时高电平

3.main.c代码

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2023 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "wwdg.h"
  22. #include "gpio.h"
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25. /* USER CODE END Includes */
  26. /* Private typedef -----------------------------------------------------------*/
  27. /* USER CODE BEGIN PTD */
  28. /* USER CODE END PTD */
  29. /* Private define ------------------------------------------------------------*/
  30. /* USER CODE BEGIN PD */
  31. /* USER CODE END PD */
  32. /* Private macro -------------------------------------------------------------*/
  33. /* USER CODE BEGIN PM */
  34. /* USER CODE END PM */
  35. /* Private variables ---------------------------------------------------------*/
  36. /* USER CODE BEGIN PV */
  37. /* USER CODE END PV */
  38. /* Private function prototypes -----------------------------------------------*/
  39. void SystemClock_Config(void);
  40. /* USER CODE BEGIN PFP */
  41. /* USER CODE END PFP */
  42. /* Private user code ---------------------------------------------------------*/
  43. /* USER CODE BEGIN 0 */
  44. //提前唤醒中断=============================================================
  45. void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef *hwwdg)
  46. {
  47. HAL_WWDG_Refresh(hwwdg);//喂狗重置计数器
  48. HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_9);//反转LDE灯
  49. }
  50. /* USER CODE END 0 */
  51. /**
  52. * @brief The application entry point.
  53. * @retval int
  54. */
  55. int main(void)
  56. {
  57. /* USER CODE BEGIN 1 */
  58. /* USER CODE END 1 */
  59. /* MCU Configuration--------------------------------------------------------*/
  60. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  61. HAL_Init();
  62. /* USER CODE BEGIN Init */
  63. /* USER CODE END Init */
  64. /* Configure the system clock */
  65. SystemClock_Config();
  66. /* USER CODE BEGIN SysInit */
  67. /* USER CODE END SysInit */
  68. /* Initialize all configured peripherals */
  69. MX_GPIO_Init();
  70. //在狗启动之前点灯,程序启动亮300ms=============================================
  71. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_RESET);
  72. HAL_Delay(300);
  73. MX_WWDG_Init();
  74. /* USER CODE BEGIN 2 */
  75. /* USER CODE END 2 */
  76. /* Infinite loop */
  77. /* USER CODE BEGIN WHILE */
  78. while (1)
  79. {
  80. /* USER CODE END WHILE */
  81. //关灯===========================-==========================================
  82. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_SET);
  83. HAL_Delay(40);
  84. /* USER CODE BEGIN 3 */
  85. }
  86. /* USER CODE END 3 */
  87. }
  88. /**
  89. * @brief System Clock Configuration
  90. * @retval None
  91. */
  92. void SystemClock_Config(void)
  93. {
  94. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  95. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  96. /** Initializes the RCC Oscillators according to the specified parameters
  97. * in the RCC_OscInitTypeDef structure.
  98. */
  99. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  100. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  101. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  102. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  103. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  104. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  105. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  106. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  107. {
  108. Error_Handler();
  109. }
  110. /** Initializes the CPU, AHB and APB buses clocks
  111. */
  112. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  113. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  114. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  115. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  116. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  117. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  118. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  119. {
  120. Error_Handler();
  121. }
  122. }
  123. /* USER CODE BEGIN 4 */
  124. /* USER CODE END 4 */
  125. /**
  126. * @brief This function is executed in case of error occurrence.
  127. * @retval None
  128. */
  129. void Error_Handler(void)
  130. {
  131. /* USER CODE BEGIN Error_Handler_Debug */
  132. /* User can add his own implementation to report the HAL error return state */
  133. __disable_irq();
  134. while (1)
  135. {
  136. }
  137. /* USER CODE END Error_Handler_Debug */
  138. }
  139. #ifdef USE_FULL_ASSERT
  140. /**
  141. * @brief Reports the name of the source file and the source line number
  142. * where the assert_param error has occurred.
  143. * @param file: pointer to the source file name
  144. * @param line: assert_param error line source number
  145. * @retval None
  146. */
  147. void assert_failed(uint8_t *file, uint32_t line)
  148. {
  149. /* USER CODE BEGIN 6 */
  150. /* User can add his own implementation to report the file name and line number,
  151. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  152. /* USER CODE END 6 */
  153. }
  154. #endif /* USE_FULL_ASSERT */

四、独立看门狗与窗口看门狗的比较

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/478631
推荐阅读
相关标签
  

闽ICP备14008679号