Here's an example of wake up from sleep on change without global interrupts enabled - or an interrupt handler.
Without global interrupts enabled, and an interrupt handler, the response time is a LOT faster since there's no context save/restore needed.
Code:
@ DEVICE INTRC_OSC,MCLR_OFF,LVP_OFF,WDT_OFF
DEFINE OSC 4
DEFINE NO_CLRWDT 1 ' don't need it cleared when it's disabled in config
PortVal VAR BYTE
LED VAR PORTB.0 ' alias LED on RB0
IntFlag VAR INTCON.0 ' alias RBIF interrupt flag bit
PORTB = 0 ' LEDs off
TRISB = $F0 ' upper 4-bits inputs, lower 4-bits outputs
OPTION_REG.7 = 0 ' internal pull-ups on
INTCON = %00001000 ' global ints disabled, int-on-change enabled for wake up
' PORTB should always = %1111xxxx when no switch is pressed
' the lower 4-bits can be anything since these are outputs.
Main:
GOSUB CheckState ' check for switch press before sleeping
' and clear interrupt flag bit
@ SLEEP ' sleep until switch is pressed
TOGGLE LED ' do stuff here immediately on wake up
PAUSE 250
TOGGLE LED
PAUSE 250
GOTO Main
CheckState:
WHILE (PORTB >> 4) != 15 ' read port & wait for switch release
PAUSE 20 ' short debounce period
WEND
IntFlag = 0 ' clear int-on-change flag bit
RETURN
END
And if you do use interrupts, having something similar to CheckState in the beginning of your interrupt handler helps avoid another interrupt when a switch is released.
I still recommend you learn to use DT_INTS though. It's pretty cool stuff...
Bookmarks