2

I'm trying to set up a watchdog timer interrupt to execute a routine in the WDT_vec ISR at some recurring interval. I don't want the processor to reset, just to execute the code in the interrupt and return to "normal" operation. I have the following test jig appropriated from the 'net that claims to set that up:

#include <avr/wdt.h>

 void setup_WDT() {
      noInterrupts();  // disable all the interrupts
      MCUSR = 0;       // ensure that the reset vectors are off

      /* Disable and clear all Watchdog settings. */

      _WD_CONTROL_REG = (1 << WDCE) | (1 << WDE);
      _WD_CONTROL_REG = 0;

      /* Setup Watchdog for interrupt and not reset, and a approximately 500ms timeout */
      _WD_CONTROL_REG = (1 << WDIE) | (1 << WDP2) | (1 << WDP0);

      interrupts();  // re-enable interrupts
    }

void setup() {
  setup_WDT();
  Serial.begin(9600);
  Serial.println("Startup");
}

void loop() {
}

ISR(WDT_vec) {
  Serial.println("Watchdog Interrupt");
}

The output indicates that the processor is just resetting repeatedly and never reaching the ISR. Also I get the following warning on compilation from the IDE (Arduino 1.8.2):

warning: 'WDT_vec' appears to be a misspelled signal handler, missing __vector prefix [-Wmisspelled-isr] ISR(WDT_vec) {

2
  • 1
    Be careful using a serial.print line inside of your ISR. Serial communication requires interrupts to function.
    – lemontwist
    Sep 27, 2017 at 0:19
  • @lemontwist Thanks, seems to work well enough just to test whether the ISR is firing, or not. I wouldn't do that in actual work...
    – MattyZ
    Sep 27, 2017 at 0:54

2 Answers 2

1

Two errors:

  1. The watchdog interrupt vector is named WDT_vect, not WDT_vec.
  2. The statement _WD_CONTROL_REG = 0; serves no useful purpose, and will likely mess up with the timed sequence for changing the watchdog setting. Just remove it.
1
  • Those changes got it working!
    – MattyZ
    Sep 27, 2017 at 0:53
0

_WD_CONTROL_REG = 0; serves an important purpose. If the Watchdog is accidentally enabled, for example by a runaway pointer or brown-out condition, the device will be reset and the Watchdog Timer will stay enabled. If the code is not set up to handle the Watchdog, this might lead to an eternal loop of timeout resets. To avoid this situation, the application software should always clear the Watchdog System Reset Flag (WDRF) and the WDE control bit in the initialization routine, even if the Watchdog is not in use. if you are using it in developing a product whose programme should never freeze than this is important to remember.

9
  • this is not a good answer. in Arduino the bootloader controls the MCU some seconds after powerup or reset. correct AVR bootloaders turn off the watchdog. in setup() it is too late for most of the wdt intervals
    – Juraj
    Jun 26, 2018 at 12:44
  • thanks, @juraj for your kind comment. The RESET what we percieve normally & reset occured after WDT reset are different. the hard reset runs the bootloader again but WDT reset does not even Reinitialize the RAM, running bootloader is a different issue. Read article: playground.arduino.cc/Main/ArduinoReset & There it is written that: Unfortunately, once the Arduino has started running a sketch, there is apparently no way to write a software function that has exactly the same effect as pushing the reset button. I will appreciate & feel good if proved wrong & learn something new :).
    – bandejiya
    Jun 27, 2018 at 12:55
  • the watchdog resets to bootloader
    – Juraj
    Jun 27, 2018 at 18:17
  • then, how can we prove that watchdog resets to the bootloader. please share the code or something authentic..!
    – bandejiya
    Jun 28, 2018 at 9:51
  • there are projects of bootloaders for flashing over Ethernet. but to activate it, the board must be remote reset to bootloader. watchdog reset is used.github.com/per1234/Ariadne-Bootloader/tree/master/libraries/…
    – Juraj
    Jun 28, 2018 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.