当前位置:网站首页>用Arduino写个ESP32看门狗
用Arduino写个ESP32看门狗
2022-07-25 09:24:00 【悟渔】
参考了很多网上的关于ESP32看门狗文章,很多设置不正确,看门狗不起作用!无意间在Arduino软件的示例程序中翻到个Watchdog例程,打工一看,恍然大悟,原来就是个定时中断嘛!只不过中断程序执行一条复位指令就行了!我去,我以前认识中都觉得看门狗应该是独立的一个硬件,就只能看门,根本就没想他也可以是一个定时器!
但软件中那个例程不够让我满意,干脆做个看门狗封装,方便以后使用.程序非常简单,有注释,大家一看就明白了.下面贴出代码,欢迎批评指正.
模块文件:Watchdog.c
/*
* ESP32看门狗对象使用方法:
* 1、引用对象定义文件(本文件):#include "Watchdog.C"
* 2、申明看门狗对象变量:WATCHDOG Watchdog;
* 3、初始化对象:Watchdog.begin();//默认使用定时器0,10秒超时。//也可以Watchdog.begin(1,1000);//指定定时器和超时时间。
* 4、喂狗:Watchdog.feed();
*/
#ifndef ESP_RT_WDT_H //避免重复定义.
#define ESP_RT_WDT_H
#include "esp_system.h"
class WATCHDOG {
private:
hw_timer_t *timer = NULL;//定时器对象.
uint8_t Timerindex=0;//硬件定时器编号.默认定时器0.
uint16_t Timeout=10000;//定时器计数.默认10秒超时.
protected:
static void Callback(){
//定时器溢出回调函数,直接复位.
esp_restart();
}
void Init(){
if(timer!=NULL){timerEnd(timer);}//如果之前有设置过定时器,则关闭相应的定时器.
timer=timerBegin(Timerindex,80,true);//使用硬件定时器,预分频80,向上计数。
timerAttachInterrupt(timer,Callback,true);//设置回调函数,边延触发。
timerAlarmWrite(timer,Timeout*1000,true);//设定中断计数器值,自动重启计数(循环定时)。CPU主频80MHz/80/1000=1毫秒。
timerAlarmEnable(timer);//开启定时器。
}
public:
WATCHDOG(){Timerindex=0;Timeout=10000;}
void begin(){Init();}
void begin(uint8_t Esp_Timerindex,uint16_t Esp_Timerout){Timerindex=Esp_Timerindex,Timeout=Esp_Timerout;begin();}
void feed(void){timerWrite(timer,0);}//喂狗.
};
#endif验证程序文件Watchdog.ino
#include "Watchdog.C"
uint8_t button=0;//按键IO--D0(GPIO0)
WATCHDOG Watchdog;//看门狗对象.
void setup() {
Serial.begin(115200);//串口波特率.
Serial.println("系统复位!");//打印启动信息,以指示模块已经复位.
pinMode(button,INPUT_PULLUP);//按键IO输入模式.
Watchdog.begin();//默认定时器0,10秒超时.
//Watchdog.begin(1,1000);//看门狗初始化,使用定时器1,1秒超时.
}
void loop() {
Watchdog.feed();//喂狗.
long loopTime = millis();
while (!digitalRead(button)) {;}//如果按键超过设定时间,则会因来不及喂狗而超时.
loopTime=millis()-loopTime;
if(loopTime>1){Serial.printf("\r\n按键时间:%d毫秒.",loopTime);}//按键未超时则显示按键所用时间.
}如果按键超时,就全重启,
边栏推荐
- App lifecycle and appledelegate, scenedelegate
- Mlx90640 infrared thermal imager temperature measurement module development notes (4)
- @5-1 CCF 2019-12-1 reporting
- 无线振弦采集仪应用工程安全监测
- Android & kotlin: puzzle solution
- Principle analysis of self supervised depth estimation of fish eye image and interpretation of omnidet core code
- 无向连通图邻接矩阵的创建输出广度深度遍历
- Get to know opencv4.x for the first time --- add salt and pepper noise to the image
- [data mining] Chapter 3 basis of data analysis
- CCF 201509-2 日期计算
猜你喜欢

数据分析业务核心

服务器cuda toolkit多版本切换

First knowledge of opencv4.x --- image histogram equalization

CCF 201503-4 网络延时

Linked list -- basic operation

SystemVerilog语法

深入理解pytorch分布式并行处理工具DDP——从工程实战中的bug说起

从鱼眼到环视到多任务王炸——盘点Valeo视觉深度估计经典文章(从FisheyeDistanceNet到OmniDet)(下)

【深度学习模型部署】使用TensorFlow Serving + Tornado部署深度学习模型

Creation of adjacency table of undirected connected graph output breadth depth traversal
随机推荐
~2 CCF 2022-03-1 uninitialized warning
深度估计自监督模型monodepth2论文总结和源码分析【理论部分】
First knowledge of opencv4.x --- image histogram matching
无线振弦采集仪的使用常见问题
AI模型风险评估 第1部分:动机
单目深度估计自监督模型Featdepth解读(下)——openMMLab框架使用
无线振弦采集仪应用工程安全监测
[data mining] nearest neighbor and Bayesian classifier
~3 CCF 2022-03-2 travel plan
MLX90640 红外热成像仪测温模块开发说明
MLOps专栏介绍
OC -- packaging class and processing object
Customize dialog to realize the pop-up box of privacy clause statement imitating Netease cloud music
Solve the problem that esp8266 cannot connect mobile phones and computer hotspots
NLM5系列无线振弦传感采集仪的工作模式及休眠模式下状态
CCF 201509-3 模板生成系统
CCF 201509-2 日期计算
CCF 201512-4 送货
¥ 1-1 SWUST OJ 941: implementation of consolidation operation of ordered sequence table
Mlx90640 infrared thermal imager temperature measurement module development notes (I)