View previous topic :: View next topic |
Author |
Message |
global
Joined: 01 Feb 2005 Posts: 21 Location: Paris
|
Watch Dog on PIC 18F |
Posted: Wed May 18, 2005 9:34 am |
|
|
Hello,
i use a PIC 18F and i wondered how i could use its Watch Dog.
Do i have to reset the watch dog before the end of the watch dog timer, if i don't want to reset the PIC ?
Could the group explain me how i can use the watch dog effieciently or just what are the basics to start with it ?
I need a description of how it works and how it should be used.
Many thanks. |
|
|
Ttelmah Guest
|
|
Posted: Wed May 18, 2005 10:02 am |
|
|
You basically 'have it'. The key is that if your program is a typical application for an embedded processor, there will be certain events/functions, that ought to be occurring. So (for instance), you may have a program loop that checks a number of ADC inputs, and outputs various values. If you implement code so that each of these operations increments/decrements a 'flag' value by a known amount, at the end of the loop, you have a value that reflects the loop having successfully completed the required tasks. Then have the code that clears the watchdog, itself dependant on this 'success' condition occurring.
As an example, I use a timer in a an interrupt loop to clear the watchdog, provided that a 'counter' in here does not reach zero. The counter is incremented by succesful RS232 reception from the controlling chip, and by succesful interrogation of a couple of I2C peripherals together with an SPI interface to another chip, and decremented by the timer event. Normally the counter should remain +ve if the events are occurring, but if an event is missed, it will drop. If a single event is missed, it'll take several loops to get to zero, and stop clearing the watchdog. However if several parts stop working there will be an almost immediate reset. In normal use the trigger is rare (some of the systems, never reset in months of use), but if something stops working, the chip resets, and normally recovers.
Just having the watchdog, routinely reset, only covers against a condition, where the program flow itself stops working. Adding tests for particular parts of the program operating, allows these to be included in what is being tested.
Best Wishes |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1909
|
|
Posted: Wed May 18, 2005 10:04 am |
|
|
Just enable the watchdog, and periodically "pet" it.
For example, all my programs are structured with an infinite while() loop at the end of main.
e.g.
Code: | while (TRUE) {
restart_wdt();
if (button_pressed) {
button_pressed = FALSE;
react_to_button();
}
} |
It's that simple. Just keep restarting the watchdog (petting it), and it won't wake up. If you have a function that may take a particularly long time to execute, you can add additional restart_wdt() anywhere. |
|
|
Guest
|
|
Posted: Thu May 19, 2005 2:12 am |
|
|
For PIC18F, you can also place the watchdog under software control.
SETUP_WDT(WDT_ON) will turn on the watchdog
SETUP_WDT(WDT_OFF) will turn watchdog off.
But you have to specify NOWDT in your #FUSE statement, follows by the Watchdog divider.
e.g
#fuses HS, NOWDT, WDT128
will place place the PIC under software control.
I use this in an appliation whereby the PIC is put to sleep, and either periodically wakeup by the watchdog or if it receives RS232 command. If the pic is awaken by RS232 cmd, I turn off the watchdog, and do the necessary action. After completing the action, I turn on the watchdog, and go into sleep again! |
|
|
Christophe
Joined: 10 May 2005 Posts: 323 Location: Belgium
|
|
Posted: Thu May 19, 2005 2:28 am |
|
|
So the watchdog can wake up the uP when it's in sleep. But how? If you don't want it to wake up by the watchdog, but only by an interrupt?
How do you enable the WD in CCS-C? |
|
|
global
Joined: 01 Feb 2005 Posts: 21 Location: Paris
|
|
Posted: Thu May 19, 2005 2:40 am |
|
|
Ok many thanks for your help.
One last thing, how is working the watch dog when for example i put it on ?
When does it make a reset of the pic ? After how long ?
Could i modify this time ?
I need to restart my pic for example if the pic has a problem and does not its job. So i should wait for example 2 minutes ans then restart the pic.
May i use timer, or is there a setup option that allows to change the time ?
What is WDT_128 ?
Thanks a lot. |
|
|
Ttelmah Guest
|
|
Posted: Thu May 19, 2005 3:56 am |
|
|
The time is determined by an internal RC oscillator. On most chips, it is nominally 18mSec (but can vary _widely_ from this). Check the data sheet for your chip. This oscillator has an adjustable 'postscaler', giving potential delays up to a couple of seconds. WDT_128, means use the /128 postscaler. Read the chips data sheet, and also look at the MicroChip application notes. There are a couple dealing with the watchdog.
Best Wishes |
|
|
Guest
|
|
Posted: Thu May 19, 2005 4:48 am |
|
|
Hi. My application is such that if the PIC is awaken by WDT, it will blink an LED to show it is alive (very short duration, 100ms), then immediately go to sleep again. The PIC is therefore ON for 100ms, OFF for about 2 sec (according to WDT divider setting).
It can also be awaken by an ext interrupt. I tied the RS232 RX pin to INT0 pin, so that if my PC host sends some command, the PIC will be awaken immediately. |
|
|
global
Joined: 01 Feb 2005 Posts: 21 Location: Paris
|
|
Posted: Thu May 19, 2005 6:07 am |
|
|
Thank you all for quick response but it still don't know how to set the time of the watchdog, i read the datasheet, but i need a proper example to understand really how to do it.
Does someone could answer me to my post with some code example and answering all my questions please ?
Many thanks. |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Thu May 19, 2005 6:15 am |
|
|
For the PIC18Fs the "easiest" way is in the #FUSES statement. You can change it on the fly however. You have looked in the manual for the compiler, right? _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
global
Joined: 01 Feb 2005 Posts: 21 Location: Paris
|
|
Posted: Thu May 19, 2005 8:21 am |
|
|
I looked in the compiler and in the datasheet but i still need some details...
if possible ! ;p
Thank you ! |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu May 19, 2005 9:17 am |
|
|
First of all, details of the watchdog are quiet different in each model so you'll have to study the datasheet.
For example I'll discuss the older PIC18F458. In Chapter 27.1 DC Characteristics there are figure 27-8 and table 27-9 dealing with the reset and watchdog timing. As someone before already mentioned, the watchdog is an internal R/C-timer with a fixed period, for the 18F458 this is typical 18ms but with a very wide spread from minimum 7 to maximum 33ms. More than a factor 4 difference!
In order to achieve longer timeouts there is a postscaler added to the watchdog. On the 18F458 this postscaler has a maximum count of 128 resulting in a:
- minimum time-out of 1 * 18ms = 18ms
- maximum time-out of 128 * 18ms = 2,304s (thanks to wide spread actually 0,896 to 4,224sec)
Newer PIC designs have a postscaler of up to 32768, resulting in time-out values of several minutes.
The PIC16 and PIC18 devices differ in the way you configure the watchdog. In the PIC16 the watchdog is enabled using the #fuses command and the postscaler is set by calling the setup_wdt() function. In the PIC18 the postscaler must be set with the #fuses command, enabling the watchdog can be done either with a fuse or calling the setup_wdt() function.
Note that in contrast to the PIC16 you can disable the watchdog from a running program in the PIC18 by calling setup_wdt(WDT_OFF), but only when the watchdog was enabled by calling setup_wdt(WDT_ON) not when the watchdog was enabled by a fuse.
Enable by setup_wdt() call: Code: | #fuses WDT128
main()
{
setup_wdt(WDT_ON);
while (TRUE)
{
restart_wdt();
perform_activity();
}
} |
Enable by fuses: Code: | #fuses WDT, WDT128
main()
{
while (TRUE)
{
restart_wdt();
perform_activity();
}
} |
|
|
|
global
Joined: 01 Feb 2005 Posts: 21 Location: Paris
|
|
Posted: Thu May 19, 2005 9:47 am |
|
|
Ok thanks !
That really clears thing up !
Thanks for your help.
One last thing :
How can i fix the period to max = 33 ms ?
Best regards.
Last edited by global on Fri May 20, 2005 4:22 am; edited 1 time in total |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu May 19, 2005 10:24 am |
|
|
Quote: | How can i fix the period to max = 33 ms ? | Like I said, this depends on the exact processor you are using. Which processor do you have?
For the PIC18F458 I would set the postscaler to a divide by 2 value (WDT2). This would result in a nominal value of 36ms, but because of production differences this could be anyhere in the range of 14ms to 66ms.
Conclusion: Don't use the watchdog for creating an accurate timer.... |
|
|
global
Joined: 01 Feb 2005 Posts: 21 Location: Paris
|
|
Posted: Tue Jun 07, 2005 6:32 am |
|
|
Hello,
i wondered if the watch dog would see if there is a infinite loop like a wrong while (doing something or not in this while), because if the watch dog is set it won't see that there is a problem, isn't it ?
If i understood well, it will only see if there is nothing done for 36 ms for example ... but is 36 ms ok ? I know that the instructions are done quickly but if nothing is done, i don't know if it is not too short, maybe because i don't imagine well how it is working, any explanation ?
Is that a reasonnable time or should i better say if there is nothing done for the longest period of seconds, i reset my pic ?
Thank you very much ! |
|
|
|