Changing 18F WDT postscaler on-the-fly
Someone asked about this recently on the Microchip forum. It's handy to
know so I figured I would post the answer here also.
Code:
; Changing WDT postscaler settings on-the-fly
PROCESSOR 18F2620
#include p18f2620.inc
CONFIG OSC=HS,WDT=ON,WDTPS=128,LVP=OFF
CBLOCK 0x000
WDT_POST ; value for new WDT postscaler
LOOPS ; loop var
ENDC
org 0x0000
bra Main
org 0x0020
Main ; starts with WDT postscaler set to 1:128
clrf PORTB
clrf TRISB
movlw 0x0A ; toggle LED on/off 5 times
movwf LOOPS ; at ~0.5 second intervals
Next
sleep ; WDT postscaler = 1:128
nop ; so we sleep here for 128 * ~4mS
btg LATB,0 ; toggle LED on RB0
decfsz LOOPS,F ; decrement loop count
bra Next ; repeat until loops = 0
; point to CONFIG2H reg at 300003h
movlw 0x30
movwf TBLPTRU
movlw 0x00
movwf TBLPTRH
movlw 0x03
movwf TBLPTRL
movlw B'00001011' ; value for 1:32 WDT postscaler
movwf WDT_POST
movlw B'11000100' ; enable write to config area
movwf EECON1
movff WDT_POST,TABLAT ; Write new postscaler value
tblwt * ; to table latch
clrwdt ; kick the dog before the final write op
movlw 0x55 ; Unlock
movwf EECON2
movlw 0xAA
movwf EECON2
bsf EECON1, WR ; Start the write
nop
bcf EECON1, WREN ; Lock up when we're done
movlw 0x0A
movwf LOOPS
Finish ; toggles LED faster showing WDT change
sleep ; at ~0.128 seconds
nop ; WDT postscaler now = 1:32 so
btg LATB,0 ; LED toggles at ~32 * 4mS
decfsz LOOPS,F
bra Finish
Done
clrwdt
bsf LATB,1 ; show we're done
bra Done
end