当前位置:网站首页>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 ,
边栏推荐
- How can information security engineers prepare for the soft exam in the second half of 2022?
- Brush questions - Luogu -p1161 turn on the light
- Workplace "digital people" don't eat or sleep 007 work system, can you "roll" them?
- 刷题-洛谷-P1085 不高兴的津津
- "Digital security" alert NFT's seven Scams
- 包管理 apt,dpkg
- QGIS加载在线地图:高德、天地图等
- 移动端网站,独立APP,网站排名策略有哪些?
- Azure Devops (XIV) use azure's private nuget warehouse
- Mujoco+spinningup for intensive learning training quick start
猜你喜欢
随机推荐
@Classmethod decorator
Brush questions - Luogu -p1161 turn on the light
应急科普|收好这份暑期安全指南,让孩子安全过暑假!
Hcip seventh day notes
埃拉托斯特尼筛法
Preparing for the soft test for junior programmers in the second half of 2022
6.27 uniapp项目历程
mujoco_ Py Chinese document
Hcip eighth day experiment
The simplest solution of the whole network 1045 access denied for user [email protected] (using password:YES)
MySQL 01: Source command
Uncaught SyntaxError: Octal literals are not allowed in strict mode.
0716RHCSA
Applet H5 get mobile number scheme
Concurrent tool set for concurrent programming
Lesson of C function without brackets
@wrap 装饰器
How happy is the frisbee bureau? 2022 youth trendy Sports Report
The whole process of 6w+ word recording experiment | explore the economical data storage strategy of alluxio
2022全球开发者中,你的收入排多少?









