TOP

How to use Watchdog properly?

SONIX micro-controller watchdog timer's clock source is from internal low-speed RC oscillator. Watchdog timer overflow time: 256ms @ 5V, 512ms @ 3V, because the internal RC oscillator frequency is effected by operating voltage and environmental temperature, so Watchdog overflow time is a general experience value, 1 series may be different, such as 1917, you can set watchdog timer overflow time by WTCKS, WTRATE, CLKMD bits.
Watchdog is used as a timer in special occasions, in addition, it is mainly used to monitoring program execution and prevent programs goes into unknown status. However, if used incorrect, watchdog's function can not be fully realized.
Users can control watchdog timer by Code Option. In code option selection table, Watch_Dog have three configuration options to choose: Always_On, Enable and Disable. You can select the options refer to watchdog timer relevant chapters in datasheet. Program can not enable or disable Watchdog, WatchDog can be cleared by @ RST_WDT, that is, assigning 5A to WDTR. 1 series watchdog can be cleared by B0BSET FWDRST instruction.

Watchdog timer reset position:
Generally, the best position to reset watchdog timer is to clear it in the top of the main routine of the program. So, if the program runs into unknown status by noise interference and can not run back to the main program, watchdog overflow signal raises in time to reset the chip. Special attention should be paid that you'd better not clear watchdog timer in the subroutine or interrupt service routine. And it is best to clear watchdog timer only at one part of the program, this way is the best structure to enhance the watchdog timer function.
Also watchdog timer can not be cleared in timer interrupt routine, when MCU go dead because of the time loop, they would break into the TIMER, it will clear the watchdog timer.
In addition, by detecting system register Pflag NT0 = 0, NPD = 0 in the beginning of program, users can determine whether watchdog timer overflow and take measures accordingly.