WatchDog Timer in Microcontroller

Thread Starter

Allen_Lusy

Joined Dec 10, 2014
12
Hello,
I working on microcontroller project. I want to use Watchdog Timer. I have a basic Question on it, which is
Independent of particular controller. If i am enabling the watchdog Timer. I have to clear the Counter of Watchdog after some instruction in order to avoid the Warm Reset by Watchdog Timer.

Q1) If the maximum count of Watchdog is 2 sec, then what should i do in the situation of below Code,
for(I=0, I<20, I++)
{
//Piece of Code which will take more than 2 Sec.
}

Q2) In multithreading, for simple example If I am having two thread, Each Thread will Switch in 500ms.

Thread 1:

while(1)
{
if(Cond)
{
instruction 1
}
//Clear WDT Counter
if(Cond)
{
instruction 1
}
while(wait for Cond);
//Clear WDT Counter
}

Thread 2:
while(1)
{
if(Cond)
{
instruction 1
}
//Clear WDT Counter
if(Cond)
{
instruction 1
}
//Clear WDT Counter
}

In thread 1, there is a while condition which will wait for some Condition. My aim to reset the Controller, if that Condition is not meeting for 2 sec. But when Context Switch is happen, other thread will Clear the Counter. How this Condition can be manage.
 

djsfantasi

Joined Apr 11, 2010
7,870
Why do you want to use the Watchdog Timer? Are any of your instructions blocking? I.e., one instruction may take 2 seconds or more to execute and nothing else can run? Or is it like this example. You send a message and want to wait up to 2 seconds for a response?

The latter case can be done without a WDT. Add a timeout condition to your wait While.

While(wait for condition && ! Timeout);

Or in English, "while condition has not occurred and not timeout". Calculate timeout by noting the start time and current time using a function like millis().
 

ErnieM

Joined Apr 24, 2011
8,183
Q1: You simple need to increase the “granularity” of the routine, meaning you need to look into what chunks of processing time so during normal processing the WDT gets a clear well within the timeout time. In other words, just sprinkle lots and lots of WDT clears thru the code.

Q2: For this I would not depend on the WDT to reset the processor. Set some timer off to the side and if that reads over your time limit then execute a reset instruction.

Keep in mind tight loops (such as “ while (!switch_down) {ResetWDT();} ” are either great choices (as the processor is essentially doing nothing waiting on the user) or really poor choices (if some hardware device is not working and THAT is the hold up).
 
Top