The 80C320 provides another useful feature to make sure your application is always running: A watchdog timer. The watchdog, effectively, makes sure your software hasnt crashed. If the watchdog determines that your software has crashed, it automatically reboots the 80C320. This is very helpful for applications that perform critical functions where crashing would cause major problems. The watchdog also offers a feature that, instead of rebooting the system, will trigger an interrupt. This is useful in situations where your software does not need to be completely reset to be "revived."
When the watchdog is enabled, the software must write to a certain SFR on a regular basis to let the watchdog know that the program is still running correctly. If the program does not perform this write within a given period of time, the watchdog assumes the program has crashed and effects a system reboot (or triggers an interrupt depending on how the watchdog has been configured).
The application may determine after what amount of time the watchdog assumes the program has crashed. The application configures the watchdog timeout value by setting CKCON.6 and CKCON.7. CKCON.7 is referred to as WD1 and CKCON.6 is referred to as WD0. The following configurations determine how often the software must "kick" the watchdog to avoid a system reboot.
WD1 | WD0 | Instruction Cycles for Interrupt | Instruction Cycles for System Reboot |
0 | 0 | 32,768 (5.243ms) | 32,896 (5.263ms) |
0 | 1 | 262,144 (41.94ms) | 262,272 (41.96ms) |
1 | 0 | 2,097,152 (335.54ms) | 2,097,280 (335.56ms) |
1 | 1 | 16,277,216 (2684.35ms) | 16,777,344 (2684.38ms) |
Note that the time in parenthesis assumes a 25Mhz crystal. Also note that the watchdog will provoke an interrupt (if so configured) 128 instruction cycles before it initiates a reset.
Thus, if WD1=0 and WD0=0 and the watchdog is enabled, your software must "kick" the watchdog at LEAST once every 32,768 instruction cycles. If your software does not kick the watchdog at least once every 32,896, the watchdog will assume your software has crashed and reboot the 80C320. How the watchdog is "kicked" is explained below.
The watchdog has a new SFR dedicated exclusively to watchdog functionality and control. The SFR is called WDCON and is at D8h. The SFR has the following component bits:
SFR Bit | Bit Label | Bit Number | Description |
WDCON.3 | WDIF | DBh | Watchdog Interrupt. This bit is set 128 instruction cycles before the watchdog initiates a system reset. |
WDCON.2 | WTRF | DAh | Watchdog Reset Has Ocurred. This bit is set when a watchdog reset has occured. If it is clear, it indicates that the last reset was not caused by the watchdog. |
WDCON.1 | EWT | D9h | Enable Watchdog Timer. The watchdog is enabled when this bit is set. The watchdog is disabled when this bit is cleared. |
WDCON.0 | RWT | D8h | Reset Watchdog Timer. Setting this bit is how the software tells the watchdog that it is still alive. This bit must be set before the watchdog timer expires. |
As has been explained, the watchdog will wait for the configured amount of time. If the watchdog
determines that the program has crashed, it will trigger a Watchdog Interrupt (if EWDI is set) after
the configured amount of time. 128 Instruction cycles later, if the program still appears to be crashed,
it will reboot the system.
In order to prevent a watchdog reboot, your software must let the watchdog know that it is still alive.
It does this by executing the following code:
You may be asking, "What are those first two MOV instructions for?" The RWT bit is a "protected
bit." You may not write to it without executing the two MOV instructions. Note that you must also
execute those two same instructions before modifying WDCON.0, WDCON. 1, and
WDCON.3. Please see the chapter regarding "Timed Access Protection" for a more full discussion
of why this is necessary.
Kicking The Watchdog
Setting the RWT bit (WDCON.0) is how your program lets the watchdog know that everything
is ok--that your program hasnt crashed. Doing so will reset the watchdog timer.
Previous: Dual DPTR | Tutorial Contents | Next: Power Features |