If a program on your PC goes wrong the operating system will usually detect this before the computer crashes. If a program on a PICmicro goes wrong there is no operating system to step in and sort things out. To get around this the PICmicro provides a "watchdog" timer. This is driven from a separate clock (i.e. not the main PICmicro clock) and works in the same way as TIMER0, in that when it overflows it can cause an event. In the case of the watchdog it will cause the PICmicro to be reset or woken up from sleep (of which more later).

The idea is that when your program is running normally it will regularly reset the watchdog timer. There is a special PICmicro instruction to do this, C2C provides a function called clear_wdt:

/* reset the watchdog timer */
clear_wdt () ;

If your program crashes it will stop resetting the watchdog, which will overflow and cause a reset - forcing the program to restart. Status bits in the PICmicro can be used to detect that a reset has been caused by a watchdog overflow and so your program can know that it just fell over and hopefully do something about it.

The watchdog timer time-out is normally 18 milliseconds or around a 56th of a second. It is not possible to be precise about these figures because the clock circuitry on the PICmicro used to drive the watchdog is not as stable as a crystal. This means that if your program crashes and stops resetting the watchdog it will be reset within around 18 milliseconds of the crash occurring.

It might be that you don't want the watchdog to step in quite as quickly as that. If this is the case you can use a pre-scaler on the watchdog in just the same way that we can prescale TIMER0. Unfortunately this process also uses the same pre-scaler, which means that you can pre-scale the watchdog, or the timer, but not both.

The watchdog pre-scaler range runs from 1 to 128, meaning that the longest time-out you can have is 18 millisecond * 128 - or about 2.3 seconds.

If the PICmicro is in sleep mode (of which more later) when the watchdog triggers it will continue execution from the instruction after the sleep instruction. If you are feeling cunning you can use the watchdog timer to make a system which uses very low power and wakes up every now and then to check or do something.

If you want to use the watchdog you have to set one of the configuration bits to turn the watchdog hardware on. It is important that you set this bit correctly, if the watchdog is Accidentally switched on in a PICmicro you will find that your program spontaneously resets every now and then.

You can control the settings of the of the configuration bits from PPP. If you select the Watchdog tick box the watchdog timer is enabled for the PICmicro and your program must perform regular clear_wdt actions to avoid being reset.