PDA

View Full Version : Changing 18F WDT postscaler on-the-fly



Bruce
- 1st August 2006, 18:12
Someone asked about this recently on the Microchip forum. It's handy to
know so I figured I would post the answer here also.



; 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

Darrel Taylor
- 1st August 2006, 20:53
Hi Bruce,

I was looking at this code when it struck me that it looked very familiar.
A quick slap on the forehead and "oh Yeah", now I remember.

Run-Time Config
http://www.picbasic.co.uk/forum/showthread.php?t=4093

:)

Bruce
- 1st August 2006, 21:13
Hey Darrel,

Never saw that one before. Could have saved myself a little time...;o}

Would be nice if Mchip could stuff this somewhere in a data sheet. It's really
handy. I was fiddling with AN851 when I noticed the CFGS bit in EECON1.

Darrel Taylor
- 1st August 2006, 21:29
Hey Bruce,

AN851

Never saw that one before. Could have saved myself a little time... writing the Run-Time Config. http://darreltaylor.com/files/ROFL.gif

P.S. There's a nice autobaud routine in there too. Cool, needed that.<br>

Bruce
- 1st August 2006, 22:09
Nice job on the macros too. Thanks for the link.