Button interrupt MPASM code
processor 12F629
cblock 0x20
d1
d2
button
INT_CONTEXT_W
INT_CONTEXT_STATUS
endc
#include
__config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF
errorlevel -302
org 0
goto main
; --------------Interrupt start -----------------------------------
org 0x4
; On an interrupt, the PIC only pushes the PC on the stack.
; Program has to save the context !
movwf INT_CONTEXT_W ;save w in INT_CONTEXT_W
movf STATUS, W
movwf INT_CONTEXT_STATUS ;save STATUS in INT_CONTEXT_STATUS
; Light up the red LED if the button is pressed and not otherwise.
bcf STATUS, RP0 ; select bank 0
btfss button,0 ; if set, skip line
goto $+4
bcf button,0 ; clear button
clrf GPIO
goto $+4 ; skip next 3 lines
bsf button,0 ; set means toggle
bsf GPIO,0 ; turn GP0 on
bcf GPIO,1
; Manually clear the interrupt flag.
bcf INTCON, INTF ; clear GPIF, Port Change Interrupt Flag bit
; Restore STATUS and W
movf INT_CONTEXT_STATUS, W
movwf STATUS
movf INT_CONTEXT_W, W
retfie ;Return from interrupt
;-----------------interrupt end-------------------------------------
main
bcf STATUS, RP0 ; select bank 0
bcf button, 0 ; set button to 0
movlw 0x00
movwf GPIO ; GPIO = 0000 0000
; Disable the comparator, this lets pins GP{0,1,2} act as digital I/O pins.
movlw b'111' ; 0x03
movwf CMCON
bsf STATUS, RP0 ; select bank 1
movlw b'00000100' ; GP2 is input
movwf TRISIO
; Enable external interrupt on GP2 .
;bsf IOC, IOC2 ; INTERRUPT-ON-CHANGE GPIO REGISTER, set GP2
bsf INTCON, PEIE ; set bit 6
bsf INTCON, GIE ; set bit 7
bsf INTCON, INTE ; external interrupt GP2
bsf OPTION_REG, INTEDG ; rising edge
bcf STATUS, RP0 ; select bank 0
loop:
;99998 cycles
movlw 0x1F
movwf d1
movlw 0x4F
movwf d2
Delay_0
decfsz d1, f
goto $+2
decfsz d2, f
goto Delay_0
btfss button,0 ; if bit 0 == 1, skip next line
goto $+3
movlw b'00000011' ; Toggle LEDs
xorwf GPIO, F ; xor GPIO with 0000 0010 ^ 0000 0011
goto loop
end