0
Offline
Jr. Member
Karma: 0
Posts: 74
Arduino rocks
|
 |
« on: December 15, 2008, 12:02:47 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #1 on: December 15, 2008, 03:06:20 am » |
you could try it with something like this: #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
|
|
|
|
« Last Edit: December 15, 2008, 03:13:02 am by mem »
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 74
Arduino rocks
|
 |
« Reply #2 on: December 15, 2008, 11:46:33 am » |
#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
|
|
|
|
« Last Edit: December 15, 2008, 11:48:23 am by Rico_Bravo »
|
Logged
|
|
|
|
|
"The old Europe"
Offline
Edison Member
Karma: 0
Posts: 2003
Bootloaders suck!
|
 |
« Reply #3 on: December 15, 2008, 02:06:26 pm » |
|
|
|
|
« Last Edit: December 15, 2008, 02:07:37 pm by madworm »
|
Logged
|
• 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!
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #4 on: December 15, 2008, 02:19:23 pm » |
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.pdfI have not used the watchdog on the Arduino so don't have any more info than that.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 74
Arduino rocks
|
 |
« Reply #5 on: December 15, 2008, 10:23:27 pm » |
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(); }
|
|
|
|
« Last Edit: December 15, 2008, 10:24:54 pm by Rico_Bravo »
|
Logged
|
|
|
|
|
"The old Europe"
Offline
Edison Member
Karma: 0
Posts: 2003
Bootloaders suck!
|
 |
« Reply #6 on: December 16, 2008, 02:03:13 am » |
The dog is impatient !
BTW, if you just want to make the led blink, you don't need the watchdog at all.
ruff ruff
|
|
|
|
« Last Edit: December 16, 2008, 02:06:23 am by madworm »
|
Logged
|
• 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!
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 74
Arduino rocks
|
 |
« Reply #7 on: December 17, 2008, 02:43:26 pm » |
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(); }
|
|
|
|
|
Logged
|
|
|
|
|
"The old Europe"
Offline
Edison Member
Karma: 0
Posts: 2003
Bootloaders suck!
|
 |
« Reply #8 on: December 18, 2008, 01:24:49 am » |
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).
|
|
|
|
|
Logged
|
• 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!
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 74
Arduino rocks
|
 |
« Reply #9 on: December 18, 2008, 09:16:53 am » |
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!
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 74
Arduino rocks
|
 |
« Reply #10 on: December 20, 2008, 05:52:43 pm » |
Hey, Just want everybody to know that the watchdog timer works perfect with ladyada's bootloader from adafruit.com
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 27
Arduino rocks
|
 |
« Reply #11 on: June 16, 2010, 01:23:05 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Online
Shannon Member
Karma: 129
Posts: 10383
|
 |
« Reply #12 on: June 16, 2010, 01:48:08 am » |
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 27
Arduino rocks
|
 |
« Reply #13 on: June 16, 2010, 02:26:52 am » |
I make the watchdog timer 30MS.....Darn it so hard to try what u said. ANY other option? 
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 27
Arduino rocks
|
 |
« Reply #14 on: June 16, 2010, 03:13:10 am » |
Miracles does happens, some delay happened and i can upload blink. fiuh...thx.
|
|
|
|
|
Logged
|
|
|
|
|
|