SoftwareWDT
This is a simple module which allows you to enable or disable the WDT (and postscaler) under software control.
As per the datasheet, enabling or disabling the WDT under software control will only work when the configuration bit WDT is set to off. That is, if you set the WDT configuration fuse to on when programming your code into the MCU, you will not be able to enable or disable the WDT under software control.
Also note that built in routines, such as DelayMS, will not tickle the WDT. If you enable the WDT with software, then a call to DelayMS will reset the PIC. The Watchdog module therefore includes a Delay() routine which enables you to make WDT safe delay calls. However, Swordfish source code library blocking functions are not affected and will automatically tickle the WDT.
Sample Code
// device and clock device = 18F452 clock = 20 // import WDT module... include "wdt.bas" // toggle LED... high(PORTD.0) delayms(1000) low(PORTD.0) // set postscaler and enable WDT... Watchdog.Postscale(ps64) Watchdog.Enabled = true // with the WDT disbaled, this loop would normally execute forever - however, we // have enable the WDT with PS value of 64, so PIC will reset in approx 1 second and // toggle the LED again - assumes nominal WDT value of 18ms - some PICs have lower // nominal values - read the datasheet for your device... while true wend
Watchdog Module
{ ***************************************************************************** * Name : Watchdog.bas * * Author : David John Barker * * Notice : Copyright (c) 2007 Mecanique * * : All Rights Reserved * * Date : 07/08/2007 * * Version : 1.0 * * Notes : Enable or disable the WDT (and postscaler) under software * * : control. As per the datasheet, enabling or disabling the WDT * * : under software control will only work when the configuration * * : bit has disabled the WDT. That is, if you set the WDT * * : configuration fuse to ON, you CANNOT disable the WDT under * * : software control * * : Setting the #option WDT = true ensures that any modules with * * : blocking calls tickle the watchdog timer. Note however that * * : low level routines such as DelayMS WILL NOT tickle the WDT, as * * : the fuse setting is disabled - use the delay() routine in this * * : module for WDT safe delays * ***************************************************************************** } module Watchdog #option WDT = true // ensure blocking module calls tickle WDT include "system.bas" // import system module config WDT = off // WDT fuse setting MUST BE OFF for this module to work // public postscaler value - some devices support up to 32768, some // don't - you need to READ THE DATASHEET to determine (a) what PS values are // supported and (b) the nominal WDT timeout, in ms... public const ps32768 = %11110, ps16384 = %11100, ps8192 = %11010, ps4096 = %11000, ps2048 = %10110, ps1024 = %10100, ps512 = %10010, ps256 = %10000, ps128 = %01110, ps64 = %01100, ps32 = %01010, ps16 = %01000, ps8 = %00110, ps4 = %00100, ps2 = %00010, ps1 = %00000 // public dims... public dim Enabled as WDTCON.Booleans(0), // enable of disable the WDT Reset as sys.ClrWDT // reset the WDT { **************************************************************************** * Name : Postscale * * Purpose : Set the WDT postscale value. You should ALWAYS disable any * * : interrupts before making this call and re-enable them when the * * : call returns * **************************************************************************** } public sub Postscale(pValue as byte) // point to CONFIG2H reg at 300003h TBLPTRU = $30 TBLPTRH = $00 TBLPTRL = $03 // enable write to configuration area, then write new // value to latch... EECON1 = $C4 TABLAT = pValue asm TBLWT* end asm // mandatory write sequence... EECON2 = $55 EECON2 = $AA EECON1.1 = 1 // enable WR Nop // delay EECON1 = $80 // back to FLASH read... end sub { **************************************************************************** * Name : Delay * * Purpose : DelayMS WILL NOT tickle the WDT when the WDT has been enabled * * : through software. Use this routinee for WDT safe delays * **************************************************************************** } public sub Delay(pDelay as word) while pDelay > 0 delayms(1) Reset dec(pDelay) wend end sub