I would like to set a Watchdog timer to reset my Arduino Atmega168 microcontroller, if I do not pat the dog( prevent the Watchdog timer from expiring and reseting my Atmega168) every 2 minutes. However, I am not sure how to do this, all I found is the starting point of code below from a google search:
Example Code (GCC) Resetting the AVR every 30mS:
#include <avr/io.h> #include <avr/wdt.h>
int main(void)
{
wdt_enable(WDTO_30MS);
while(1) {};
}
If you want to have a look, hardware\tools\avr\avr\include\avr\wdt.h has the defines for different watchdog delays. The longest delay defined is 2 seconds. I think the ATmega168 chip used in the arduino can support up to 8 seconds
MEM, so the above code takes care of everything meaning I do not need to pat the dog every 30 milliseconds? It is all done by the calling function.
If so, that is great.
Thanks
For the help
Rico
the code posted sets the watchtog for the given timeout period, you still need to pat the dog.
To do that, you must call wdt_reset();
When the watchdog timer is enabled, a call to this instruction is required before the timer expires, otherwise a watchdog-initiated device reset will occur.
I tried the following code below, trying to trigger the watchdog timer using a delay of 10 seconds and the microcontroller got stuck in an infinite reset loop. I thought it should reset and turn the LED on for another 10 seconds but not the case?? Not sure why???? Can anyone help?
Thanks
RB
void loop()
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(10000); // waits for a 10 seconds
digitalWrite(ledPin, LOW); // sets the LED off
delay(250); // waits
wdt_reset();
}
void loop()
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(10000); // waits for a 10 seconds
digitalWrite(ledPin, LOW); // sets the LED off
delay(250); // waits
wdt_reset();
}
Yes, I know this, the problem is the watchdog timer never comes out of the reset loop. I spoke with another fellow and he said the problem is with the bootloader. However, I think ladyada's bootloader will sovle this problem. I will post the results next week!