;*********** ; author: Chris Hodapp ; version: 1.0 ; filename: interrupt-wtfasm ; chip: PIC16F887 ; clock freq: 4 MHz ; ********** list P = 16F887 #include __CONFIG _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT __CONFIG _CONFIG2, _WRT_OFF & _BOR21V ; ************* ; Declarations ; ************* cblock 0x70 ; unbanked RAM W_Save STATUS_Save endc org 0 ; reset vector goto Start org 4 ; ISR goto ISR ;************* ; Subroutines ;************* Init: bsf STATUS,RP0 ; select Register Bank 1 ; Set b'10001000' instead of b'10000000' to enable PORTB change interrupt; ; this also completely breaks the program. movlw b'10000000' ; global interrupts movwf INTCON movlw b'00000001' ; set PORTB pin 0 to interrupt-on-change movwf IOCB movlw b'00000000' ; PORTA I/O configuration movwf TRISA movlw b'00000001' ; PORTB " movwf TRISB movlw b'00000000' ; PORTC " movwf TRISC movlw b'00000000' ; PORTD " movwf TRISD movlw b'00000000' ; PORTE " movwf TRISE bsf STATUS,RP1 ; select Register Bank 3 movlw 0x00 movwf ANSELH ; PortB pins are digital bcf STATUS,RP1 ; back to Register Bank 0 bcf STATUS,RP0 retlw 0 ISR: movwf W_Save ; Save context movf STATUS,w movwf STATUS_Save btfsc INTCON,RBIF ; is this an interrupt from PORTB changing? goto portb_int goto other_int portb_int: bsf PORTD,6 ; LED#7: Interrupt was thrown from PORTB changing other_int: bsf PORTD,7 ; LED#8: Some unexpected interrupt was thrown leave_int: movf STATUS_Save,w ; Restore context movwf STATUS swapf W_Save,f swapf W_Save,w retfie ;*************** ; Program Start ;*************** Start: call Init clrf PORTD bsf PORTD,0 ; LED#1: Init subroutine ran successfully Main: btfsc PORTB,0 goto L2off bsf PORTD,1 ; LED#2: Pushbutton is pressed goto L2done L2off: bcf PORTD,1 L2done: btfsc INTCON,RBIF goto L3off bsf PORTD,2 ; LED#3: Interrupt flag is set goto L3done L3off: bcf PORTD,2 L3done: bsf PORTD,3 ; LED#4: Main loop has run at least once goto Main end