# Increasing the Watch Dog Timeout PIC 16F877A - CCS PIC C

I have been trying to look around for an example on how to increase the watchdog timer but unfortunately I cant find any. The only example that is included in the example uses setup_wdt(WDT_2304MS)

Could anyone tell me how I can increase this delay to say 5,8,9 or 10 seconds ? Any suggestions would be appreciated

• You can't increase past that 2304 ms. In fact, it's not even guaranteed that it will be exactly 2304 ms. It could vary between 896 ms and 4224 ms. Sep 3 '12 at 13:09
• @m.alin: I don't see where you got those figures. 7 to 33 ms times the maximum prescaler value of 256 is 1.8 to 8.4 seconds. Sep 3 '12 at 13:18
• @OlinLathrop The max. value of the prescaler for the WDT is 128. See datasheet, page 56. Sep 3 '12 at 13:20
• @m.alin: Oops, you're right. It's 256 when assigned to timer 0, but only 128 when assigned to the WDT. I'll fix my answer. Sep 3 '12 at 13:29

You don't want that. Watchdog timers keep a close look on the correct operation of the microcontroller, and the controller should be able to reset the watchdog timer every so often, after a few tens of milliseconds maximum. For a modern microcontroller a period of 10 seconds is eternity; it may execute 100 million or more instructions in that time, in which anything can go wrong. Then you don't want to let it go for another 10 seconds before you take action. In that time a motor you should be controlling may burn.

If you can't spare 1 of those 100 million to reset the timer there's something seriously wrong with the structure of your code.

• I was assuming he wants to use the watchdog to wake up the micro periodically, otherwise a extra long period is pointless as you say. Sep 3 '12 at 13:33
• @Olin - You're more familiar with PIC than I am (I: not at all). Isn't this abuse? Isn't the watchdog meant to reset the controller if it doesn't get kicked often enough, and shouldn't you be using a "regular" timer for wake-ups? Sep 3 '12 at 13:35
• Using the watchdog to wake a PIC from sleep is common. Technically a watchdog is for the purpose you state, but especially on older PICs was the only way to wake from sleep without external hardware. You can't use a timer since sleep shuts down the clock. That watchdog runs from its own R-C oscillator, at least in older PICs like this one. You can use timer 1 with a external 32 kHz crystal to wake the PIC, but the watchdog works when accuracy is not important. Genuine watchdogs are greatly overrated in my opinion. Usually they are worse than what they cure. Sep 3 '12 at 19:44

The watchdog timer always has a fixed period which is nominally 18 ms, but can vary from 7 to 33 ms. There is a prescaler that can either be assigned to timer 0 or the watchdog. With the maximum prescaler setting, the watchdog trip period is multiplied by 128. This means the maximum native watchdog period is nominally 2.3 seconds, with a possible range of 900 ms to 4.2 seconds.

However, the basic watchdog period can be extended to any length by counting in firmware. Even 18 ms is a "long" time for the micro. Waking up every 18 ms to decrement a counter and see if it has reached zero, then going back to sleep takes very little average current. 10 seconds would be 556 watchdog trips at 18 ms each, for example. Or, you could set the prescaler to 8, for example, to get nominal 144 ms periods. Then you would count 69 of those to get about a 10 second wakeup period.

One problem with using the watchdog for timing wakeups is that it is so inaccurate on these old PICs. A trick I have used a few times is to measure the watchdog interval by leaving the processor running every once in a while, like every 1024 wakeups, for example. That allows you to measure the watchdog period against the much more accurate main oscillator, then decide how many watchdog periods to count per desired wakeup time. In this example the average current from leaving the processor on every 1024 watchdog intervals is still less than .1% of leaving it on all the time.

• Just to clarify for the original poster, I expect you mean setting a few registers in user RAM to values indicating that you're "expecting" a watchdog reset, then running continuously until the watchdog does hit, and then having your reset code look and clear those register values. Doing that will require instructing CCS not to clear the registers in question, but I forget how to do that (it's been a decade since I migrated to a different compiler). Sep 3 '12 at 14:11