This page looks best with JavaScript enabled

WD.Easy

 ·  ☕ 3 min read

WD.Easy

This library is intended to control watchdog modes of operation, so far only series microcontrollers AVR.

Features

I did not write it in the style of a singleton class (the watchdog is one). No matter how many objects are created, they will all work with the same register. I set myself the task to make the library as easy as possible and can be used for any Arduino and not only.

The main requirement is that the TIMER SHOULD START COUNTING EVEN BEFORE THE FUNCTION setup STARTS , and even better before the start of main.

It seems to have worked

Methods are available in the timer

MethodDESCRIPTIONresult
setTimeOut(uint8_t)Sets the timer trigger intervalvoid
setMode(uint8_t)Sets the action when the timer is triggeredvoid
setTask(ptr*)Set the interrupt handler functionvoid
getTimeOut()Get the frequency of activationsuint8_t
getMode()Get the timer modeuint8_t
isEnable()Is the timer active?bool
reset()Start a new countdownvoid

Intervals

The table shows the parameters accepted by the method setTimeOut(uint8_t) and returns getTimeOut().

definetime s
WD_15MS0.015
WD_30MS0.030
WD_60MS0.060
WD_120MS0.120
WD_250MS0.250
WD_500MS0.500
WD_1S1
WD_2S2
WD_4S4
WD_8S8

Note The time is approximate and not suitable for measuring exact intervals, since the watchdog timer is clocked by an internal low-latency oscillator that “floats” when the supply voltage and temperature change

Actions

defineDESCRIPTION
DISABLEDThe timer is stopped
INTERRUPTWhen triggered, the interrupt handler will be called
SYSTEMRESETWhen triggered, the microcontroller will be rebooted
INTERUPTANDRESETWhen triggered, the interrupt handler will be called
after the same intervals, the controller will be rebooted

Обробник переривань

By default, no interrupt handler is specified (nullptr).

Examples

Example one

1
2
3
4
5
6
7
8
9
void WD_tick() {
Serial.print("WD_tick()");
Serial.println(millis());
}

WatchDogEasy WD(WatchDogEasy::WD_1S, WatchDogEasy::INTERRUPT, WD_tick);

void setup() { Serial.begin(115200); }
void loop() {}

Here we trigger the timer to fire every second, when it fires, an interrupt will be generated, which will be handled by the WD_tick() function.

In this example, the timer starts counting even before the setup() function starts. This is convenient if there is a risk of the controller freezing during initiation.

Example two

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
void WD_tick() {
Serial.print("WD_tick()");
Serial.println(millis());
}

WatchDogEasy WD;

void setup() {
Serial.begin(115200);
WD.setTimeOut(WatchDogEasy::WD_1S);
WD.setMode(WatchDogEasy::INTERRUPT);
WD.setTask(WD_tick);
}
void loop() {}

Everything is the same here, but the timer starts counting after the last setter.

All setters reset the timer to a new count. Getters do not (Be careful). During the execution of the getter, the time may coincide and the action programmed in the timer may be executed.

Example three

1
2
3
4
5
6
7
WatchDogEasy WD(WatchDogEasy::WD_1S, WatchDogEasy::SYSTEMRESET, nullptr);

int main(){
for(;;){

}
}

Here, the timer is initialized before entering main(). In my opinion, it’s super convenient

“System Requirements”

The timer will work on any Arduino and AVR series microcontrollers

An additional 2 bytes in the RAM will take up space in the flash 198 bytes

The most important thing

Installation

  1. You can download a zip archive WD.Easy v1.0 And extract the contents to the desired directory
  2. The library is available for installation through the Arduino IDE library manager.
  3. You can download a zip archive WD.Easy prerelise. This is a test branch with the most recent code, not debugged. It is set as the first item of this list

Need to connect

1
 #include <WD.Easy.hpp>

and do not forget to set the timer so that it does not reset the program

Share on
Support the author with

Sam4uk
WRITTEN BY
Sam4uk
Embedded Software Engineare C/C++