【STM32】STM32学习笔记-独立看门狗和窗口看门狗(47)

00. 目录

文章目录

    • 00. 目录
    • 01. WDG概述
    • 02. 独立看门狗相关API
      • 2.1 IWDG_WriteAccessCmd
      • 2.2 IWDG_SetPrescaler
      • 2.3 IWDG_SetReload
      • 2.4 IWDG_ReloadCounter
      • 2.5 IWDG_Enable
      • 2.6 IWDG_GetFlagStatus
      • 2.7 RCC_GetFlagStatus
    • 03. 独立看门狗接线图
    • 04. 独立看门狗程序示例1
    • 05. 独立看门狗程序示例2
    • 06. 窗口看门狗相关API
      • 6.1 WWDG_DeInit
      • 6.2 WWDG_SetPrescaler
      • 6.3 WWDG_SetWindowValue
      • 6.4 WWDG_EnableIT
      • 6.5 WWDG_SetCounter
      • 6.6 WWDG_Enable
      • 6.7 WWDG_GetFlagStatus
      • 6.8 WWDG_ClearFlag
    • 07. 窗口看门狗接线图
    • 08. 窗口看门狗程序示例
    • 09. 程序示例代码下载
    • 10. 附录

01. WDG概述

  • WDG(Watchdog)看门狗

  • 看门狗可以监控程序的运行状态,当程序因为设计漏洞、硬件故障、电磁干扰等原因,出现卡死或跑飞现象时,看门狗能及时复位程序,避免程序陷入长时间的罢工状态,保证系统的可靠性和安全性

  • 看门狗本质上是一个定时器,当指定时间范围内,程序没有执行喂狗(重置计数器)操作时,看门狗硬件电路就自动产生复位信号

  • STM32内置两个看门狗

独立看门狗(IWDG):独立工作,对时间精度要求较低

窗口看门狗(WWDG):要求看门狗在精确计时窗口起作用

02. 独立看门狗相关API

2.1 IWDG_WriteAccessCmd

/*** @brief  Enables or disables write access to IWDG_PR and IWDG_RLR registers.* @param  IWDG_WriteAccess: new state of write access to IWDG_PR and IWDG_RLR registers.*   This parameter can be one of the following values:*     @arg IWDG_WriteAccess_Enable: Enable write access to IWDG_PR and IWDG_RLR registers*     @arg IWDG_WriteAccess_Disable: Disable write access to IWDG_PR and IWDG_RLR registers* @retval None*/
void IWDG_WriteAccessCmd(uint16_t IWDG_WriteAccess)
功能:使能或者失能对寄存器 IWDG_PR 和 IWDG_RLR 的写操作
参数:IWDG_WriteAccess:对寄存器 IWDG_PR 和 IWDG_RLR 的写操作的新状态
返回值:

2.2 IWDG_SetPrescaler

/*** @brief  Sets IWDG Prescaler value.* @param  IWDG_Prescaler: specifies the IWDG Prescaler value.*   This parameter can be one of the following values:*     @arg IWDG_Prescaler_4: IWDG prescaler set to 4*     @arg IWDG_Prescaler_8: IWDG prescaler set to 8*     @arg IWDG_Prescaler_16: IWDG prescaler set to 16*     @arg IWDG_Prescaler_32: IWDG prescaler set to 32*     @arg IWDG_Prescaler_64: IWDG prescaler set to 64*     @arg IWDG_Prescaler_128: IWDG prescaler set to 128*     @arg IWDG_Prescaler_256: IWDG prescaler set to 256* @retval None*/
void IWDG_SetPrescaler(uint8_t IWDG_Prescaler)
功能:设置 IWDG 预分频值
参数:IWDG_Prescaler:IWDG 预分频值
返回值:

2.3 IWDG_SetReload

/*** @brief  Sets IWDG Reload value.* @param  Reload: specifies the IWDG Reload value.*   This parameter must be a number between 0 and 0x0FFF.* @retval None*/
void IWDG_SetReload(uint16_t Reload)
功能:设置 IWDG 重装载值
参数:IWDG_Reload:IWDG 重装载值
返回值:

2.4 IWDG_ReloadCounter

/*** @brief  Reloads IWDG counter with value defined in the reload register*   (write access to IWDG_PR and IWDG_RLR registers disabled).* @param  None* @retval None*/
void IWDG_ReloadCounter(void)
功能:按照 IWDG 重装载寄存器的值重装载 IWDG 计数器
参数:无
返回值:

2.5 IWDG_Enable

/*** @brief  Enables IWDG (write access to IWDG_PR and IWDG_RLR registers disabled).* @param  None* @retval None*/
void IWDG_Enable(void)
功能:使能 IWDG
参数:无
返回值:

2.6 IWDG_GetFlagStatus

/*** @brief  Checks whether the specified IWDG flag is set or not.* @param  IWDG_FLAG: specifies the flag to check.*   This parameter can be one of the following values:*     @arg IWDG_FLAG_PVU: Prescaler Value Update on going*     @arg IWDG_FLAG_RVU: Reload Value Update on going* @retval The new state of IWDG_FLAG (SET or RESET).*/
FlagStatus IWDG_GetFlagStatus(uint16_t IWDG_FLAG)
功能:检查指定的 IWDG 标志位被设置与否
参数:IWDG_FLAG:待检查的 I2C 标志位
返回值:IWDG_FLAG 的新状态(SET 或者 RESET)       

2.7 RCC_GetFlagStatus

/*** @brief  Checks whether the specified RCC flag is set or not.* @param  RCC_FLAG: specifies the flag to check.*   *   For @b STM32_Connectivity_line_devices, this parameter can be one of the*   following values:*     @arg RCC_FLAG_HSIRDY: HSI oscillator clock ready*     @arg RCC_FLAG_HSERDY: HSE oscillator clock ready*     @arg RCC_FLAG_PLLRDY: PLL clock ready*     @arg RCC_FLAG_PLL2RDY: PLL2 clock ready      *     @arg RCC_FLAG_PLL3RDY: PLL3 clock ready                           *     @arg RCC_FLAG_LSERDY: LSE oscillator clock ready*     @arg RCC_FLAG_LSIRDY: LSI oscillator clock ready*     @arg RCC_FLAG_PINRST: Pin reset*     @arg RCC_FLAG_PORRST: POR/PDR reset*     @arg RCC_FLAG_SFTRST: Software reset*     @arg RCC_FLAG_IWDGRST: Independent Watchdog reset*     @arg RCC_FLAG_WWDGRST: Window Watchdog reset*     @arg RCC_FLAG_LPWRRST: Low Power reset* *   For @b other_STM32_devices, this parameter can be one of the following values:        *     @arg RCC_FLAG_HSIRDY: HSI oscillator clock ready*     @arg RCC_FLAG_HSERDY: HSE oscillator clock ready*     @arg RCC_FLAG_PLLRDY: PLL clock ready*     @arg RCC_FLAG_LSERDY: LSE oscillator clock ready*     @arg RCC_FLAG_LSIRDY: LSI oscillator clock ready*     @arg RCC_FLAG_PINRST: Pin reset*     @arg RCC_FLAG_PORRST: POR/PDR reset*     @arg RCC_FLAG_SFTRST: Software reset*     @arg RCC_FLAG_IWDGRST: Independent Watchdog reset*     @arg RCC_FLAG_WWDGRST: Window Watchdog reset*     @arg RCC_FLAG_LPWRRST: Low Power reset*   * @retval The new state of RCC_FLAG (SET or RESET).*/
FlagStatus RCC_GetFlagStatus(uint8_t RCC_FLAG)
功能:检查指定的 RCC 标志位设置与否
参数:RCC_FLAG:待检查的 RCC 标志位
返回值:RCC_FLAG 的新状态(SET 或者 RESET)   

03. 独立看门狗接线图

在这里插入图片描述

04. 独立看门狗程序示例1

main.c

#include "stm32f10x.h"#include "delay.h"
#include "oled.h"int main(void){	//初始化OLED_Init();OLED_ShowString(1, 1, "IWDG TEST");if (RCC_GetFlagStatus(RCC_FLAG_IWDGRST) == SET){OLED_ShowString(2, 1, "IWDGRST");delay_ms(500);OLED_ShowString(2, 1, "       ");delay_ms(100); RCC_ClearFlag();}else{OLED_ShowString(3, 1, "RST");delay_ms(500);OLED_ShowString(3, 1, "       ");delay_ms(100); }//写使能IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);//设置预分频IWDG_SetPrescaler(IWDG_Prescaler_16);//设置重装载值  1000 = 1 / 40K * 16 * x   x = 2500IWDG_SetReload(2499);//喂狗IWDG_ReloadCounter();//看门狗使能IWDG_Enable();while(1){//喂狗IWDG_ReloadCounter();delay_ms(1800);}return 0;}

05. 独立看门狗程序示例2

main.c

#include "stm32f10x.h"#include "delay.h"
#include "oled.h"
#include "key.h"int main(void){	//初始化OLED_Init();//按键初始化key_init();OLED_ShowString(1, 1, "IWDG TEST");if (RCC_GetFlagStatus(RCC_FLAG_IWDGRST) == SET){OLED_ShowString(2, 1, "IWDGRST");delay_ms(500);OLED_ShowString(2, 1, "       ");delay_ms(100); RCC_ClearFlag();}else{OLED_ShowString(3, 1, "RST");delay_ms(500);OLED_ShowString(3, 1, "   ");delay_ms(100); }//写使能IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);//设置预分频IWDG_SetPrescaler(IWDG_Prescaler_16);//设置重装载值  1000 = 1 / 40K * 16 * x   x = 2500IWDG_SetReload(2499);//喂狗IWDG_ReloadCounter();//看门狗使能IWDG_Enable();while(1){key_scan();//喂狗IWDG_ReloadCounter();OLED_ShowString(4, 1, "FEED");delay_ms(200);OLED_ShowString(4, 1, "    ");delay_ms(600); }return 0;}

06. 窗口看门狗相关API

6.1 WWDG_DeInit

/*** @brief  Deinitializes the WWDG peripheral registers to their default reset values.* @param  None* @retval None*/
void WWDG_DeInit(void)
功能:将外设 WWDG 寄存器重设为缺省值
参数:无
返回值:

6.2 WWDG_SetPrescaler

/*** @brief  Sets the WWDG Prescaler.* @param  WWDG_Prescaler: specifies the WWDG Prescaler.*   This parameter can be one of the following values:*     @arg WWDG_Prescaler_1: WWDG counter clock = (PCLK1/4096)/1*     @arg WWDG_Prescaler_2: WWDG counter clock = (PCLK1/4096)/2*     @arg WWDG_Prescaler_4: WWDG counter clock = (PCLK1/4096)/4*     @arg WWDG_Prescaler_8: WWDG counter clock = (PCLK1/4096)/8* @retval None*/
void WWDG_SetPrescaler(uint32_t WWDG_Prescaler)
功能:设置 WWDG 预分频值值
参数:WWDG_Prescaler:指定 WWDG 预分频
返回值:

6.3 WWDG_SetWindowValue

/*** @brief  Sets the WWDG window value.* @param  WindowValue: specifies the window value to be compared to the downcounter.*   This parameter value must be lower than 0x80.* @retval None*/
void WWDG_SetWindowValue(uint8_t WindowValue)
功能:设置 WWDG 窗口值
参数:WindowValue r:指定的窗口值。该参数取值必须在 0x400x7F 之间。
返回值:

6.4 WWDG_EnableIT

/*** @brief  Enables the WWDG Early Wakeup interrupt(EWI).* @param  None* @retval None*/
void WWDG_EnableIT(void)
功能:使能 WWDG 早期唤醒中断(EWI)
参数:无
返回值:

6.5 WWDG_SetCounter

/*** @brief  Sets the WWDG counter value.* @param  Counter: specifies the watchdog counter value.*   This parameter must be a number between 0x40 and 0x7F.* @retval None*/
void WWDG_SetCounter(uint8_t Counter)
功能:设置 WWDG 计数器值
参数:Counter:指定看门狗计数器值。该参数取值必须在 0x400x7F 之间。
返回值:

6.6 WWDG_Enable

/*** @brief  Enables WWDG and load the counter value.                  * @param  Counter: specifies the watchdog counter value.*   This parameter must be a number between 0x40 and 0x7F.* @retval None*/
void WWDG_Enable(uint8_t Counter)
功能:使能 WWDG 并装入计数器值
参数:Counter:指定看门狗计数器值。该参数取值必须在 0x400x7F 之间。
返回值:

6.7 WWDG_GetFlagStatus

/*** @brief  Checks whether the Early Wakeup interrupt flag is set or not.* @param  None* @retval The new state of the Early Wakeup interrupt flag (SET or RESET)*/
FlagStatus WWDG_GetFlagStatus(void)
功能:检查 WWDG 早期唤醒中断标志位被设置与否
参数:无
返回值:早期唤醒中断标志位的新状态(SET 或者 RESET)      

6.8 WWDG_ClearFlag

/*** @brief  Clears Early Wakeup interrupt flag.* @param  None* @retval None*/
void WWDG_ClearFlag(void)
功能:清除早期唤醒中断标志位
参数:无
返回值:

07. 窗口看门狗接线图

在这里插入图片描述

08. 窗口看门狗程序示例

main.c

#include "stm32f10x.h"#include "delay.h"
#include "oled.h"
#include "key.h"int main(void){	//初始化OLED_Init();//按键初始化key_init();//开启WWDG时钟RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);OLED_ShowString(1, 1, "WWDG TEST");if (RCC_GetFlagStatus(RCC_FLAG_WWDGRST) == SET){OLED_ShowString(2, 1, "WWDGRST");delay_ms(500);OLED_ShowString(2, 1, "       ");delay_ms(100); RCC_ClearFlag();}else{OLED_ShowString(3, 1, "RST");delay_ms(500);OLED_ShowString(3, 1, "   ");delay_ms(100); }//设置预分频WWDG_SetPrescaler(WWDG_Prescaler_8);//设置窗口值WWDG_SetWindowValue(21 | 0x40);//使能看门狗WWDG_Enable(0x40 | 54);while(1){key_scan();OLED_ShowString(4, 1, "FEED");delay_ms(10);OLED_ShowString(4, 1, "    ");delay_ms(10); //喂狗WWDG_SetCounter(0x40 | 54);}return 0;}

09. 程序示例代码下载

38-独立看门狗.rar

39-窗口看门狗.rar

10. 附录

参考: 【STM32】江科大STM32学习笔记汇总

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/499294.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

宝塔FTP服务设置并结合cpolar内网穿透实现远程传输文件

文章目录 1. Linux安装Cpolar2. 创建FTP公网地址3. 宝塔FTP服务设置4. FTP服务远程连接小结 5. 固定FTP公网地址6. 固定FTP地址连接 宝塔FTP是宝塔面板中的一项功能,用于设置和管理FTP服务。通过宝塔FTP,用户可以创建FTP账号,配置FTP用户权限…

高级语言期末2011级B卷(计算机学院)

1.编写函数&#xff0c;实现按照如下公式计算的功能&#xff0c;其中n为自然数 #include <stdio.h>int fac(int n) {if(n0)return 1;elsereturn n*fac(n-1); }float fun(int n) {float flag;float sum0;for(int i0; i<n; i) {flagi/((i1)*fac(i2));sumflag;}return su…

无法调试MFC源码

VS无法调试MFC源码 起初 有时候就是这么无奈&#xff0c;MFC源码各种问题没有办法调试&#xff0c;可是又想看下代码如何调用&#xff0c;里面做了些什么&#xff0c;从哪儿调出&#xff0c;学习一下大神的思路什么的。整理一下有可能的原因。 检查生成代码设置 需要设置正…

Anroid开发过程中常见的异常及解决方法

本文总结了在android开发中一些常见的错误及异常&#xff0c;特此分享&#xff01; The import android cannot be resolved 错误 将android源码通过eclipse的import功能导入的话&#xff0c;如果出现了“The import android cannot be resolved”错误。不用着急&#xff0c;…

petalinux烧写image.ub报错

xinlinx SDK烧写petalinux生成的BOOT.BIN和image.ub时&#xff0c;BOOT.BIN烧写正常&#xff0c;image.ub烧写报错如下 Erase Operation failed. INFO: [Xicom 50-44] Elapsed time 0 sec.ERROR: Flash Operation Failed串口助手操作擦除flash如图&#xff1a; 解决方法&am…

jenkins插件下载失败bug

如果遇到安装jenkins插件经常失败并报以下类似错误&#xff0c;很可能是因为jenkins国外官方插件地址下载速度非常慢&#xff0c;我们可以修改为国内插件地址。 java.io.IOException: Failed to load: SCM API Plugin (scm-api 676.v886669a_199a_a_) - Jenkins (2.361.4) or h…

linux操作docker

docker地址 官方地址 centos7安装docker 卸载旧版本docker sudo //在前面表示以管理员权限操作yum remove docker \docker-client \docker-client-latest \docker-common \docker-latest \docker-latest-logrotate \docker-logrotate \docker-engine安装docker //安装所需资…

每天十条linux知识点-24-0226(1)

文章目录 1.在哪下载linux内核源码&#xff1f;2.linux文件夹都有哪些文件&#xff1f;arch&#xff1a;包含和硬件体系结构相关的代码&#xff0c;每种平台占一个相应的目录&#xff0c;如i386、arm、arm64、powerpc、mips等。block&#xff1a;块设备驱动程序I/O调度。certs&…

07-Linux部署Nginx

Linux部署Nginx 简介 NGINX是一款高性能的HTTP和反向代理服务器&#xff0c;也是一个IMAP/POP3/SMTP代理服务器。它的特点包括占用内存少、并发能力强&#xff0c;因此在处理高负载和高并发的场景时表现优秀。NGINX由俄罗斯的程序设计师Igor Sysoev开发&#xff0c;最初是为俄…

面向对象编程入门:掌握C++类的基础(2/3):深入理解C++中的类成员函数

在C编程中&#xff0c;类是构建程序的基石&#xff0c;而理解类的默认成员函数对于高效使用C至关重要。本文将深入探讨这六个默认成员函数及其他相关概念&#xff0c;提供给读者一个全面的视角。 类的6个默认成员函数&#xff1a; 如果一个类中什么成员都没有&#xff0c;简称为…

如何在Win系统从零开始搭建Z-blog网站,并将本地博客发布到公网可访问

文章目录 1. 前言2. Z-blog网站搭建2.1 XAMPP环境设置2.2 Z-blog安装2.3 Z-blog网页测试2.4 Cpolar安装和注册 3. 本地网页发布3.1. Cpolar云端设置3.2 Cpolar本地设置 4. 公网访问测试5. 结语 1. 前言 想要成为一个合格的技术宅或程序员&#xff0c;自己搭建网站制作网页是绕…

1990-2022年世界各国基于消费的人均CO2排放/世界各国基于人均消费的二氧化碳排放数据

1990-2022年世界各国基于消费的人均CO2排放/世界各国基于人均消费的二氧化碳排放数据 1、时间&#xff1a;1990-2022年 2、指标&#xff1a;Entity、Code、Year、Annual CO2 emissions (per capita)、Per capita consumption-based CO2 emissions 3、来源&#xff1a;GCP 4…