Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Watchdog timer in PIC

Status
Not open for further replies.

elexhobby

Junior Member level 3
Junior Member level 3
Joined
Sep 9, 2005
Messages
31
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,685
clrwdt example watchdog

Can somebody please explain me the concept of watchdog timer in PIC?
What does it do? It is said that before sleep the watchdog timer should be cleared, & it can ‘wake up’ the uC from sleep. But how does it decide the delay after which it should wake up the uC?
I can’t understand why Watchdog timer is necessary to wake up the uC, if any other interrupt can.
Also, is there any other ingenious application of the Watchdog timer besides during sleep operation.
I have read that it resets the uC whenever it goes in an infinite loop.
But how does the watchdog timer inintiate itself (how does it know that the uC has crashed). If my program consists of some periodic activity, say transferring data serially, the watchdog timer may feel this is an infinite loop - since I am repeating the same task.
In such a case won't the programmer have the headache of continuously resetting the watchdog timer after every few instructions to prevent resetting the uC
Kindly help…
 

pic wdt wake

Hello,

In my understanding and reading, a watchdog timer (not just for PIC in general) is an independent timer that keeps on ticking regardless of what your uC does. Usually it is based on the system master clock and can be scaled to increase its period.

In some cases, watchdog timer is very useful. For example, it can use to trigger on events periodically e.i when the timer overflows. Example, you want your uC to do something in every 1 millisecond or so, you can prescale your watchdog timer to make it overfolw in every millisecond - thus saving you from coding the delay subroutines and memory space. Really Neat.

But i think the more important function of the Watchdog timer is to see if your masterclock is running correctly. Outside interfernces may cause your master clock you slow down or even shut down.

You can simply ignore this watchdog timer if you dont want to use it by two ways:
a.) Disable all interrupts; by default all interrupts are disabled. Watchdog Timer Over Flow (WDTO) is a software interrupt.
b.) by initializing in your code that Watchdog timer is disabled.

Thanks

Added after 7 minutes:

here I pulled this out from one of my PIC ebooks

1.7 Watchdog
One more thing is requiring our attention is a flawless functioning of the microcontroller
during its run-time. Suppose that as a result of some interference (which often does occur in industry) our microcontroller stops executing the program, or worse, it starts working incorrectly.

Of course, when this happens with a computer, we simply reset it and it will keep working. However, there is no reset button we can push on the microcontroller and thus solve our problem. To overcome this obstacle, we need to introduce one more block called watchdog. This block is in fact another free-run counter where our program needs to write a zero in every time it executes correctly. In case that program gets "stuck", zero will not be written in, and counter alone will reset the microcontroller upon achieving its maximum value. This will result in executing the program again, and correctly this time around. That is an important element of every program to be reliable without man's supervision.
 

    elexhobby

    Points: 2
    Helpful Answer Positive Rating
Many Thanks for ur help. I now have a better idea of the purpose of the WDT.
But I have a few doubts... If one program cycle is longer than WDT timer overflow period, are two CLRWDT instructions used?
If the program gets stuck within a loop that contains the CLRWDT instruction, it wouldn't reset the PIC, right?
Also, can I use the WDT as a fourth timer (I know this would be rare) & writing an interrupt service routine at 0000h itself which checks the status of TO(bar) in status register, to know whether a WDT overflow occurred? Since it is a crude oscillator, it can't be used as an accurate timer..
 

In any microcontroller, the watchdog is a timer that is supposed to reset the micro if it times out. The watchdog's function is thus to detect if the micro has gone "berserk" and reset it.
It's similar to your computer doing something funny and your resetting it when you see there is nothing else you can do to bring it back under control.
In an embedded system, since there is no one around, this function belongs to the watchdog timer.

Knowing you are going to use a WDT, you write your program in such a way as to reset the WDT often enough to prevent it from resetting the micro under normal conditions. For the PIC example, the period is 18ms. So make sure you return ot the CLRWDT instruction in less than 18ms. If not, insert another CLRWDT. Although that is not very advisable. Ideally, you should have only one CLRWDT instruction, located in the main loop. That way, if the program gets stuck, the WDT will reset the micro.

In the PIC, the WDT can use the 8-bit prescaler (see PSA bit) to increase the timeout period. The prescaler can be assigned either to TMR0 or to the WDT, so you have to be careful here. If you really need it for TMR0, then you have to take care of the WDT within 18ms (actually, less than that, since the tolerance on the WDT is large, the minimum being about 9ms, if I am not mistaken. But this should be no problem, sinc even at 1MHz the PIC will execute avout 9,000 instructions within those 9ms).

Special attention is to be paid to wait loops. If the wait time cannot be determined, you will have to include a CLRWDT in that loop. Although, as you correctly said, that is dangerous, since if the micro gets stuck in such a loop the WDT will not reset it. So pay special attention to these loops, make sure the program can not get stuck there. Or, use a timeout function, to limit the time spent in the loop, if possible.

What I like about the WDT on the PIC is that it runs off a separate RC oscillator, one that has nothing to do with the main system clock, so the WDT will work no matter what.
What I do not really like about the PIC's WDT is that it works even in sleep mode. This is sometimes desirable, but sometimes it is not. I think they made it really independent, so you just have to live with that.

Anyway, the WDT is not supposed to be used as a timer, do not rely on its period. It should never be allowed to reset the micro, except in sleep mode. And then you test the source of the wakeup and if you find it was the WDT, you can just go back to sleep.
 
hai

wdt is used to maintain the function of the controller without any hangng
the nature of wdt is it will master clear the controller when it gets overflowed.
so in program sequence we will clear the wdt before it gets overflowed.
this is done by using any timer.
in some circumstances the controller will hang, at the same time wdt won't get stopped still it will run and gets overflow because the controller is hanged. so overflowed wdt resets the controller and controller starts to work from origin
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top