SLEEP should work fine on the 18F452. The WDT on this one has a typical time-out period of 18mS without the postcaler.
With the postscaler set to 128 you have 128 * 18mS for ~2.304S before wake-up from SLEEP.
It will definitely lock-up if you use SLEEP or NAP, and WDT isn't enabled, or you're not setting WDTCON.SWDTEN.
Using NAP with the 18F, the Period argument isn't used, so it just sleeps for whatever the typical WDT time-out period is * the postscaler.
With the postscaler set to 128, SLEEP 4 should give you roughly 4.6S.
Here are a few that I've tested on the 18F452.
Code:
' Config fuse settings in PBP 18f452.INC
' __CONFIG _CONFIG1H, _OSCS_OFF_1H & _XT_OSC_1H
' __CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_128_2H
' __CONFIG _CONFIG4L, _LVP_OFF_4L
DEFINE OSC 4
X VAR BYTE
PORTD = 1
TRISD = 0
Main: ' Use one at a time to see the difference
GOTO SL1
'GOTO SL2
'GOTO SL3
'GOTO SL4
SL1: ' Gives roughly 4.6 seconds / compiles to 130 bytes
PORTD = PORTD ^ 1 ' Toggle D0
SLEEP 4 ' ~4.6 seconds sleep
GOTO SL1
SL2: ' Gives roughly 4.6 seconds / compiles to 60 bytes
PORTD = PORTD ^ 1 ' Toggle D0
X = 2 ' 2 x ~2.3 seconds = ~4.6 seconds sleep
WHILE X
NAP 1
X = X - 1
WEND
GOTO SL2
SL3: ' Gives roughly 4.6 seconds / compiles to 46 bytes
PORTD = PORTD ^ 1 ' Toggle D0
X = 2 ' 2 x ~2.3 seconds = ~4.6 seconds sleep
WHILE X
@ SLEEP
X = X - 1
WEND
GOTO SL3
' Gives roughly 4.6 seconds / compiles to 38 bytes
ASM
SL4
MOVLW 1
XORWF LATD,F
MOVLW 2 ; 2 x ~2.3 seconds = ~4.6 seconds sleep
SLEEP
DECFSZ WREG,F
GOTO $-4
GOTO SL4
ENDASM
End
Bookmarks