How to use the WatchDog

How to use the WatchDog

In this post we will see how to use the watchdog and what is the limit for our equipment.

What is the watchdog?

Watchdog is an electronic timer that allows to reset the system in case of computer malfunctions and recover from it.

Watchdog consists on a decrementing timer, when the timer reaches to zero the system resets. Then you always have to refresh the watchdog.

Watchdog library “<#include <avr/wdt.h>”

Functions:

#define 

wdt_reset()   __asm__ __volatile__ (“wdr”)

#define 

WDTO_500MS   5

#define 

WDTO_1S   6

#define 

WDTO_2S   7

#define 

WDTO_4S   8

#define 

WDTO_8S   9

 

¡¡¡Important!!! The Ardbox PLC watchdog cannot work under 500ms. There are not limitations for M-Duino PLC.

Next there is an example of how to use it:

#include <avr/wdt.h>

///////////////////////////////////////////////////////////////////////////////////////////

void setup() {
  wdt_disable();

	Serial.begin(9600L);
#ifdef ARDBOX
  while (!Serial); 
#endif

  // Enable watchdog
  // In this example:
  // - WDTO_500MS, WDTO_1S: the watchdog resets the device
  // - WDTO_2S, WDTO_4S, WDTO_8S: the watchdog does not reset the device
  wdt_enable(WDTO_500MS);
}

////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  // Refresh the watchdog timer
  wdt_reset();

  // WORK HERE, but faster than 500ms

  // Force watchdog
  Serial.println("Wait 1 second...");
  delay(1000);

  // Be careful: the line below will not be executed, and
  // the serial port connection will be lost.
  // You should select the serial port in the Arduino IDE again.

  Serial.println("Watchdog timeout greater than 1s");
}

See also:

HOW TO USE STEPPER MOTOR WITH ARDBOX ANALOGHOW TO CONNECT AND CONFIGURE A LOAD CELL USING HX711 TRANSMITTER WITH OUR EQUIPMENT’SHOW TO GENERATE A DIFFERENT PWM’S SIGNALS WITH OURS EQUIPMENT’SRS-485 COMMUNICATION EXAMPLE USING OUR LIBRARIESHOW TO CREATE A WEB SERVER TO READ A SD CARD WITH OUR PLC’SHOW TO CONNECT A SD CARD TO A PLCHOW TO UPLOAD A BOOTLOADER ON A ARDBOX/M-DUINO PLC

Comments are closed.