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

news2024/10/7 18:29:48

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 = 2500
	 IWDG_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 = 2500
	 IWDG_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.coloradmin.cn/o/1479468.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

mysql缓存机制面试题,学海无涯

二、我们先来看看这份笔记到底有什么 1、先把kubernetes跑起来(先跑起来创建kubernetes集群部署应用访问应用Scale应用滚动更新) 2、重要概念 3、部署kubernetes Cluster(安装docker安装 kubelet.kubeadm和 kubectll用kubeadm 创建cluster&a…

【力扣hot100】刷题笔记Day18

前言 晚上巩固一下今天的回溯题,基础不牢地动山摇,po一张代码随想录总结的 组合补充 77. 组合 - 力扣(LeetCode) class Solution:def combine(self, n: int, k: int) -> List[List[int]]:path []res []def backtrack(star…

航拍无人机技术,航拍无人机方案详解,无人机摄影技术

航拍无人机是利用遥控技术和摄像设备,在空中进行拍摄和录像的无人机。这种无人机通常具有高清摄像设备、图像传输设备、GPS定位系统、智能控制系统等,可以轻松实现各种拍摄角度和高度,广泛应用于影视制作、旅游景区航拍、城市规划、环保监测等…

CryoEM - 使用 cryoSPARC 基于单颗粒图像从头重构蛋白质三维结构

欢迎关注我的CSDN:https://spike.blog.csdn.net/ 本文地址:https://blog.csdn.net/caroline_wendy/article/details/136384544 基于冷冻电镜单颗粒图像重构蛋白质三维结构,利用冷冻电镜技术测定生物大分子结构的方法。原理是从冷冻电镜获得大量同一种蛋白质分子的二维投影图…

计算机组成原理-第一/二章 概述和数据的表示和运算【期末复习|考研复习】

前言 总结整理不易,希望大家点赞收藏。 给大家整理了一下计算机组成原理中的重点概念,以供大家期末复习和考研复习的时候使用。 参考资料是王道的计算机组成原理和西电的计算机组成原理。 计算机组成原理系列文章传送门: 第一/二章 概述和数据…

适用Java SpringBoot项目的分布式锁

在分布式系统中,常用到分布式锁,它有多中实现方式,如:基于redis,database,zookeeper等。Spring integration组件有这三种服务的分布式锁实现,今天来看看用的比较多的redis和database实现方式。 …

ANTLR4规则解析生成器(三):遍历语法分析树

文章目录 1 词法分析2 语法分析3 遍历语法分析树3.1 Listener3.2 Visitor 4 总结 1 词法分析 词法分析就是对给定的字符串进行分割,提取出其中的单词。 在antlr4中,词法规则的名称的首字母需要大写,右侧必须是终结符,通常将词法…

Tomcat布署及优化

1.Tomcat简介 Tomcat 是 Java 语言开发的,Tomcat 服务器是一个免费的开放源代码的 Web 应用服务器,Tomcat 属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试 JSP 程序的首选。一般来说&…

数据结构:队列 || oj(l两个队列实现栈)

[TOC](数据结构:队列 || oj(l两个队列实现栈)) 一、队列的概念 1.什么是队列? //先说一下,队列跟栈一样都是很重要的数据结构,重要的不是说这个数据结构怎么实现,重要的是结构的优势! //栈:是…

上海亚商投顾:沪指终结月线6连阴 北向资金净买入超160亿

上海亚商投顾前言:无惧大盘涨跌,解密龙虎榜资金,跟踪一线游资和机构资金动向,识别短期热点和强势个股。 一.市场情绪 三大指数昨日低开高走,沪指重新站上3000点,深成指、创业板指大涨超3%。半导体产业链全…

协议和序列化反序列化

“协议”和序列化反序列化 “协议”的概念: “协议”本身是一种约定俗成的东西,由通讯双方必须共同遵从的一组约定,因此我们一定要将这种约定用计算机语言表达出来,此时双方计算机才能识别约定的相关内容 我们把这个规矩叫做“…

仿真科普|CAE技术赋能无人机 低空经济蓄势起飞

喝一杯无人机送来的现磨热咖啡;在拥堵的早高峰打个“空中的士”上班;乘坐水陆两栖飞机来一场“陆海空”立体式观光……曾经只出现在科幻片里的5D城市魔幻场景,正逐渐走进现实。而推动上述场景实现的,就是近年来越来越热的“低空经…

包管理工具之npm也慌了?

起因 因为npm的种种问题,我很早就换成了pnpm和yarn(但是其实npm也在使用),已经很久没有关注npm的功能更新了。最近无意间进入Node18版本的安装目录,发现其除了常规的node,npm等默认安装了一个新的包corepack,这个就是今天我要分享的东西了。 注: 我因为18版本的node上…

Docker技术概论(3):Docker 中的基本概念

Docker技术概论(3) Docker 中的基本概念 - 文章信息 - Author: 李俊才 (jcLee95) Visit me at: https://jclee95.blog.csdn.netMy WebSite:http://thispage.tech/Email: 291148484163.com. Shenzhen ChinaAddress of this article:https://…

MySQL 用户账号迁移

文章目录 前言1. 工具安装1.1 下载安装包1.2 编译安装 2. 用户迁移后记 前言 有一个典型的使用场景,就是 RDS 下云大多数都是通过 DTS 进行数据传输的,用户是不会同步到自建数据库的。需要运维人员在自建数据库重新创建用户,如果用户数量很多…

kyuubi整合spark on yarn

目录 概述实践下载配置启动 结束 概述 目标: 1.实现kyuubi spark on yarn2.实现 kyuubi spark on yarn 资源的动态分配 注意:版本 kyuubi 1.8.0 、 spark 3.4.2 、hadoop 3.3.6 前置准备请看如下文章 文章链接hadoop一主三从安装链接spark on yarn链接 实践 …

【音视频处理】使用ffmpeg实现多个视频合成一个视频(按宫格视图)

先上结果 环境 硬件:通用PC 系统:Windows 测试有效 软件:ffmpeg 解决 0、命令 ffmpeg.exe -i input1.mp4 -i input2.mp4 -i input3.mp4 -i input4.mp4 -filter_complex "[0:v]scaleiw/2:ih/2,pad2*iw:2*ih[a]; [1:v]scaleiw/2:ih/2…

看待事物的层与次 | DBA与架构的一次对话交流

前言 在计算机软件业生涯中,想必行内人或多或少都能感受到系统架构设计与数据库系统工程的重要性,也能够清晰地认识到在计算机软件行业中技术工程师这个职业所需要的专业素养和必备技能! 背景 通过自研的数据库监控管理工具,发现 SQL Server 数据库连接数在1-2K之间,想…

Java进阶-反射

来学习一下Java的反射,通过Class实例获取class信息的方法称为反射(Reflection),内容如下 一、反射机制 1、概述 在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一…

UDP数据报套接字编程入门

目录 1.TCP和UDP的特点及区别 1.1TCP的特点 1.2UDP的特点 1.3区别 2.UDP Socket的api的介绍 2.1DatagramSocket API 2.2DatagramPacket API 3.回显客户端与服务器 3.1回显服务器 3.1.1UdpEchoServer类的创建 3.1.2服务器的运行方法start() 3.1.3main部分 3.1.4.完整…