当前位置:网站首页>Write an esp32 Watchdog with Arduino
Write an esp32 Watchdog with Arduino
2022-07-25 13:50:00 【Wuyu】
I refer to a lot of online information about ESP32 Watchdog article , Many settings are incorrect , The watchdog doesn't work ! By accident Arduino In the sample program of the software, I turn to Watchdog routine , Take a look at working , See light suddenly , It turned out to be a timed interrupt ! Just interrupt the program to execute a reset instruction ! I went to , I used to know that the watchdog should be an independent hardware , You can only watch the door , I didn't think he could also be a timer !
But the routine in the software is not enough to satisfy me , Simply make a watchdog package , Convenient for future use . The procedure is very simple , There are notes , You can see it at a glance . Post the code below , Welcome criticism and correction .
Module file :Watchdog.c
/*
* ESP32 How to use the watchdog object :
* 1、 Reference object definition file ( This document ):#include "Watchdog.C"
* 2、 Declare the watchdog object variable :WATCHDOG Watchdog;
* 3、 Initialize object :Watchdog.begin();// Timer is used by default 0,10 Second timeout .// It's fine too Watchdog.begin(1,1000);// Specify timer and timeout .
* 4、 feed a dog :Watchdog.feed();
*/
#ifndef ESP_RT_WDT_H // Avoid repeating definitions .
#define ESP_RT_WDT_H
#include "esp_system.h"
class WATCHDOG {
private:
hw_timer_t *timer = NULL;// Timer Objects .
uint8_t Timerindex=0;// Hardware timer number . Default timer 0.
uint16_t Timeout=10000;// Timer count . Default 10 Second timeout .
protected:
static void Callback(){
// Timer overflow callback function , Direct reset .
esp_restart();
}
void Init(){
if(timer!=NULL){timerEnd(timer);}// If the timer has been set before , Then turn off the corresponding timer .
timer=timerBegin(Timerindex,80,true);// Use hardware timer , prescale 80, Count up .
timerAttachInterrupt(timer,Callback,true);// Set the callback function , Edge delay trigger .
timerAlarmWrite(timer,Timeout*1000,true);// Set the interrupt counter value , Automatic restart count ( Cycle timing ).CPU Main frequency 80MHz/80/1000=1 millisecond .
timerAlarmEnable(timer);// Turn on 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);}// feed a dog .
};
#endifValidation program file Watchdog.ino
#include "Watchdog.C"
uint8_t button=0;// Key IO--D0(GPIO0)
WATCHDOG Watchdog;// Watchdog object .
void setup() {
Serial.begin(115200);// Serial port baud rate .
Serial.println(" System reset !");// Print start information , To indicate that the module has been reset .
pinMode(button,INPUT_PULLUP);// Key IO The input mode .
Watchdog.begin();// Default timer 0,10 Second timeout .
//Watchdog.begin(1,1000);// Watchdog initialization , Use timer 1,1 Second timeout .
}
void loop() {
Watchdog.feed();// feed a dog .
long loopTime = millis();
while (!digitalRead(button)) {;}// If the key exceeds the set time , Will be too late to feed the dog and overtime .
loopTime=millis()-loopTime;
if(loopTime>1){Serial.printf("\r\n Key time :%d millisecond .",loopTime);}// If the key does not time out, the time spent on pressing the key will be displayed .
}If the key times out , Just restart it all ,
边栏推荐
- 0715RHCSA
- hcip第八天笔记
- 刷题-洛谷-P1150 Peter的烟
- Serious [main] org.apache.catalina.util.lifecyclebase Handlesubclassexception initialization component
- 埃拉托斯特尼筛法
- Leetcode202 --- Happy number
- 0720RHCSA
- stable_ Baselines quick start
- Brush questions - Luogu -p1046 Tao Tao picking apples
- ADB connects to Xiaomi mobile phone via Wi Fi
猜你喜欢
![[platform IO compile hifive1 revb] * * * [.pio\build\hifive1 revb\src\setupgpio.o] solution to error 1](/img/a1/de197464e2a735d7ef012cea780da5.png)
[platform IO compile hifive1 revb] * * * [.pio\build\hifive1 revb\src\setupgpio.o] solution to error 1

【力扣】645.错误的集合

Applet sharing function

window unbutu20 LTS apt,wget 安装时 DNS 解析错误

Package management apt, dpkg

redis集群的三种方式

QGIS loading online map: Gaode, Tiandi map, etc

"Digital security" alert NFT's seven Scams

刷题-洛谷-P1150 Peter的烟

嵌入式代码如何进行重构?
随机推荐
Turn off automatic update when brew executes commands
Brush questions - Luogu -p1075 prime factor decomposition
mysql 01: source命令
刷题-洛谷-P1151 子数整数
互斥锁、自旋锁、读写锁……理清它们的区别和应用
运动豪华还是安全豪华?亚洲龙与沃尔沃S60该入手哪款?
hcip第九天笔记
How to refactor embedded code?
Package management apt, dpkg
Congestion control of TCP
Alibaba mqtt IOT platform "cloud product circulation" practice - the two esp32 achieve remote interoperability through the IOT platform
IM系统-消息流化一些常见问题
伯克利博士『机器学习工程』大实话;AI副总裁『2022 ML就业市场』分析;半导体创业公司大列表;大规模视频人脸属性数据集;前沿论文 | ShowMeAI资讯日报
MySQL 01: Source command
应急科普|收好这份暑期安全指南,让孩子安全过暑假!
Applet sharing function
Canvas判断内容为空
What should I do if the high-level MySQL server cannot be installed and I forget the password (MySQL 8.0.29)?
window unbutu20 LTS apt,wget 安装时 DNS 解析错误
Brush questions - luogu-p1089 Jinjin savings plan