Go Down

Topic: Watchdog timer Question??? (Read 7975 times) previous topic - next topic

Rico_Bravo

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) {};
}


Alternatively you can create a macro:

Code:

#include <avr/io.h>
#include <avr/wdt.h>

#define Reset_AVR() wdt_enable(WDTO_30MS); while(1) {}

int main(void)
{
Reset_AVR();
}



Thanks for any help
Rico

mem

#1
Dec 15, 2008, 09:06 am Last Edit: Dec 15, 2008, 09:13 am by mem Reason: 1
you could try it with something like this:

Code: [Select]
#include <avr/io.h>
#include <avr/wdt.h>

void setup(){
 wdt_enable(WDTO_30MS);
}


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

Rico_Bravo

#2
Dec 15, 2008, 05:46 pm Last Edit: Dec 15, 2008, 05:48 pm by Rico_Bravo Reason: 1
#include <avr/io.h>
#include <avr/wdt.h>

void setup(){
 wdt_enable(WDTO_30MS);
}

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

madworm

#3
Dec 15, 2008, 08:06 pm Last Edit: Dec 15, 2008, 08:07 pm by madworm Reason: 1
http://linux.die.net/man/3/wdt_enable
http://www.nongnu.org/avr-libc/user-manual/group__avr__watchdog.html

--> wdt_reset()

p.s. google ... and so on
• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

mem

I answered rico via pm, this was my response:

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.

There is not yet an arduino friendly version of this code so you will need to do a little research, there looks to be quite a good document here: http://www.atmel.com/dyn/resources/prod_documents/doc2551.pdf

I have not used the watchdog on the Arduino so don't have any more info than that.

Rico_Bravo

#5
Dec 16, 2008, 04:23 am Last Edit: Dec 16, 2008, 04:24 am by Rico_Bravo Reason: 1
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 :(

#include <avr/io.h>
#include <avr/wdt.h>



int ledPin = 5;


void setup()
{
wdt_enable(WDTO_8S);
pinMode(ledPin, OUTPUT);
}

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();
}

madworm

#6
Dec 16, 2008, 08:03 am Last Edit: Dec 16, 2008, 08:06 am by madworm Reason: 1
The dog is impatient !

BTW, if you just want to make the led blink, you don't need the watchdog at all.

ruff ruff
• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

Rico_Bravo

I tried adding the MCUSR=0; like the data sheet said and still not working???  :(

#include <avr/io.h>
#include <avr/wdt.h>



int ledPin = 5;


void setup()
{
MCUSR=0;
wdt_enable(WDTO_8S);
pinMode(ledPin, OUTPUT);
}

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();
}

madworm

You set the watchdog timer timeout to 8 seconds, then you wait for more than 10 seconds, and then you pat the dog with wdt_reset().

As the 8 < 10 your code never gets beyond delay(10000).

• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

Rico_Bravo

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!

Rico_Bravo

Hey, Just want everybody to know that the watchdog timer works perfect with ladyada's bootloader from adafruit.com

dbo

I tried the code up there, and now i can`t upload coz the dog keeps barking. LED 13 keeps blinking all the time. Panic mode: ON.
Any help plz. THX

Coding Badly


http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1271245150/2#2

dbo

I make the watchdog timer 30MS.....Darn it so hard to try what u said.
ANY other option? =(

dbo

Miracles does happens, some delay happened and i can upload blink. fiuh...thx.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy