Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: Tutorial: Basic Watchdog Timer Setup on: October 22, 2012, 11:38:47 am
Apologies for late reply!

As I mentioned in my previous post, it would be possible to extend the 8s timer to something longer by applying the WDE to 1 on the second iteration of the WDT interrupt but there is still a small risk that the board could potentially freeze or encounter an error when reconfiguring the WDT fuses. I believe the likely hood to be quite low but it still exists and if encountered would cause your board to hang indefinitely or until a hardware reset is performed.

I've modified the example code to include this step. I feel I should stress once again that this does make the WDT quite vulnerable to flaws. If your looking for a more serious solution, look at implementing a second arduino or ATtiny chipset (you'll need a super basic one...) to monitor and hard reset the main board in the event of a fault or error.

*note: I don't have a board at hand to test this code on so your going to have to guinea pig it for me

Code:
#include <avr/wdt.h>

int loop_count = 0;
int wdt_counter = 0;

void setup() {

  Serial.begin(9600);
  Serial.println("Starting up...");
  pinMode(13,OUTPUT);
  digitalWrite(13,HIGH);
  delay (500);
  watchdogStart();
}

void watchdogStart(void)
{
  cli();  // disable all interrupts
  wdt_reset(); // reset the WDT timer
/*
//WDTCSR configuration:
WDIE = 1: //Interrupt Enable
WDE = 1 : //Reset Enable
WDP3 = 0 :
For 2000ms Time-out WDP2 = 1 :
For 2000ms Time-out WDP1 = 1 :
For 2000ms Time-out WDP0 = 1 :
    For 2000ms Time-out
    */
  // Enter Watchdog Configuration mode:
  WDTCSR |= (1<<WDCE) | (1<<WDE); // Set Watchdog settings:
  WDTCSR = (1<<WDIE) | (0<<WDE) | (0<<WDP3) | (1<<WDP2) | (1<<WDP1) | (1<<WDP0);
  sei();
}

void watchdogArm(void)
{
  cli();  // disable all interrupts
  wdt_reset(); // reset the WDT timer
/*
//WDTCSR configuration:
WDIE = 1: //Interrupt Enable
WDE = 1 : //Reset Enable
WDP3 = 0 :
For 2000ms Time-out WDP2 = 1 :
For 2000ms Time-out WDP1 = 1 :
For 2000ms Time-out WDP0 = 1 :
    For 2000ms Time-out
    */
  // Enter Watchdog Configuration mode:
  WDTCSR |= (1<<WDCE) | (1<<WDE); // Set Watchdog settings:
  WDTCSR = (1<<WDIE) | (1<<WDE) | (0<<WDP3) | (1<<WDP2) | (1<<WDP1) | (1<<WDP0);
  sei();
}

void loop()
{
  for (int i = 0; i <= loop_count;i++){
    digitalWrite(13,HIGH);
    delay(100);
    digitalWrite(13,LOW);
    delay(100);
  }
  loop_count++;
  wdt_reset();
  watchdogStart();
  wdt_counter = 0;
  Serial.print(loop_count);
  Serial.print(". Watchdog fed in approx. ");
  Serial.print(loop_count*200);
  Serial.println(" milliseconds.");
}
ISR(WDT_vect) // Watchdog timer interrupt.
{
  if(wdt_counter==0)
  {
  wdt_counter++;
  watchdogArm();
  }
  // Include your code here - be careful not to use functions they may cause the interrupt to hang and // prevent a reset.
}
2  Using Arduino / Programming Questions / Re: Tutorial: Basic Watchdog Timer Setup on: October 18, 2012, 02:47:34 am
Hi Stanley,

I've never tried but I presume it would be fairly easy to just count the number of reset interrupts with the WDE set to 0 (reset off), there may be a conflict if your board freezes and cant alter the count value but its a theoretical guess. I don't have access to a board right now so I cant test it but I presume the code logic would work somewhat like this:

Run the example code.
Change the WDT setup so the that board will not restart (WDE = 0; //reset off)
Now the board will no longer reset and the WDT will keep calling an interrupt at whatever period of time it is set up
Insert a variable (say wdt_counter = 0) that will increment up when the WDT interrupt is called.
At however many loops or increments you decide (minus one) you will need to arm the WDT to reset (WDE = 1, reset on)
Now if the interrupt is called the board will reset.
If the WDT is reset or fed you will need to reset the counter and WDT back to zeros to reset the whole loop cycle.

Out of interest what need do you have for doing this?

Hope this helps! Its morning where I am but if your still having problems in 10+ hours let me know and Ill try write some proper example code.

Nic
3  Using Arduino / Programming Questions / Re: Tutorial: Basic Watchdog Timer Setup on: October 30, 2011, 02:33:02 am
Thanks very much for the feedback draythomp- Ive only got UNOs sitting on my desktop at home which is why I haven't encountered any of the problems you guys are bringing up. I may update the tutorial at some stage or another but for now the forum thread is short enough for someone to be able to skim and make changes at a glance - great timer work-around! smiley

I presume you only added the Serial in the WDT interrupt to debug and get some sort of feedback - Ive never encountered any problems but just be aware if serial happened to be the function that caused the board to malfunction and prompt a WDT interrupt, It may cause the interrupt to hang if you bring up serial in the interrupt and you will never get around to resetting the board.

Thanks again for the feedback! Such contributions are what make forums such great mediums smiley
4  Using Arduino / Programming Questions / Re: Tutorial: Basic Watchdog Timer Setup on: October 29, 2011, 12:00:07 am
Jimxbob correctly pointed out to me a few oversights of mine so FYI:

Quote
I may be wrong here as this is something hew to me, but one thing you might want to consider in your guide is to alter the example code fractionally to move the watchdogSetup(); line to the top of the setup() function.

I think you're actually relying on code in the bootloader to disable the watchdog before you enable it here.

I was having issues because I was not using a bootloader.
Once the WDT fires, it's left enabled at restart, but the duration is set to the minimum time (I think - read it somewhere & it seems to fit my experience). It then fires again within the delay(500), and your locked in to an infinite loop.

With the watchdogSetup() at the top of the setup() the code will work fine with or without a bootloader (though it'll still fail for a bootloader that doesn't handle the WDT properly).

Yep, thats all correct - I was relying in my code on the boot loader to reset back to all the defaults.
5  Using Arduino / Programming Questions / Re: Tutorial: Basic Watchdog Timer Setup on: July 01, 2011, 12:24:12 pm
Thx.
But can I use it with my Mega board or is that not compatible?


Run the example code and then you can tell us:-) the mega 1280 probably has a slightly older boot loader than the UNO so won't be able to say for sure but the physical device itself is capable of operating a watchdog timer.
6  Using Arduino / Programming Questions / Re: Tutorial: Basic Watchdog Timer Setup on: June 11, 2011, 02:33:25 am
Thanks for the corrections - didn't think I would be the one learning from this  smiley-lol

updated copy with a new example now online.
7  Using Arduino / Programming Questions / Re: Tutorial: Basic Watchdog Timer Setup on: June 10, 2011, 03:15:10 pm
Thanks very much for your feedback!

The serial.println was there merely for debugging purposes but you are quite correct - I should have pointed that out or removed it.


The watchdog will never fire in the example code.  The millis overflow is irrelevant.


I obviously stand to be corrected here but does the millis overflow not cause the arduino to freeze? I cant say ive left any arduino on for more than 3 days, I was led to believe it encountered an error after 49 odd not days?

Nope, it wont fire unless you remove the wtd_reset(), the code was really more of a blank template than a functional example.... I guess I could create a series of growing loops that would eventually cause a time-out. At the time of writing I just thought it simplest to leave things relatively blank for people to fill in the dots

I will correct the PDF when I get back to my desktop in the morning.



8  Using Arduino / Programming Questions / Tutorial: Basic Watchdog Timer Setup on: June 10, 2011, 12:28:04 pm
For those of you looking for a basic understanding of the watchdog timer I have written a simple guide on how to go about setting one up...

let me know if there are any errors/typos or improvements to be made.

enjoy
Pages: [1]