0
votes

Here's the task: You will use the “External Interrupt” pin (pin name is “INT”) to generate an interrupt. This pin is tied to switch “S3” on the PICDEM 2 Plus eval board. Pushing this switch will generate the interrupt.   We will configure this pin as in input, enable External Interrupts, enable Global Interrupts, and keep track of how many times we’ve generated this interrupt with a register we create called “Push_Count”.

Here's my code:

; PIC16F877A Configuration Bit Settings

; ASM source line config statements

#include <p16F877A.inc>

; CONFIG
; __config 0xFF7B
 __CONFIG _FOSC_EXTRC & _WDTE_OFF & _PWRTE_OFF & _BOREN_ON & _LVP_OFF & _CPD_OFF & _WRT_OFF & _CP_OFF

                cblock  0x020
                        COUNTERL
                        COUNTERH
                        PUSH_COUNT
                endc

                org     0x0000          ;put next line of code at address 0x0000
RESET_V         goto    START           ;reset vector


START           org     0x040
                clrf    PUSH_COUNT      ;reset counter to 0
                clrf    INTCON          ;clear INTCON register
                bsf     INTCON,INTE     ;enable external int on INT pin
                bsf     INTCON,GIE      ;enable global int

                bsf     STATUS,RP0      ;switch to bank 1
                movlw   b'00000001'     ;set RB0 as input
                movwf   TRISB           ;move value to TRISB
                bcf     STATUS,RP0      ;switch to bank
                clrf    PORTB           ;clear PORTB output vector
                bsf     PORTB,0         ;turn on LED on RB0
                goto    $               ;loop here forever

INT_SERVICE     org     0x004
                call    DELAY
                incf    PUSH_COUNT,f    ;increment PUSH_COUNT
                bcf     INTCON,INTF     ;clear int flag
                retfie

DELAY           decfsz  COUNTERL        ;decrement COUNTERL
                goto    DELAY           ;if not zero, keep decrementing COUNTERL
                decfsz  COUNTERH        ;decrement COUNTERH
                goto    DELAY           ;if not zero, keep decrementing COUNTERH
                return

                END

My problem is that every time I'm pressing the S3 switch on PICDEM 2, the interrupt is not working and the value of the PUSH_COUNT register is not increasing/counting. Any problem with my code? I'm just new to PIC. Thanks

2
What is your clock frequency? Could DELAY be too long. How do you inspect the contents of 'PUSH_COUNT'? Are you running in debug mode? - jolati
My oscillator was set to EXT and I don't set any clock frequency. I'm checking the contents of PUSH_COUNT in the "File Register" window in MPLAB X - elvinguitar
For EXT clock frequency check Y2 (or Y1) on the board. But more importantly: the file register window will show you the current register value if that variable is in scope when you break in debug mode (running by clicking 'Debug Main Project'). - jolati

2 Answers

0
votes

I'n not familiar to PICs, and I don't know how the switch is connected, but: Check that the pull-up configuration matches the switch connection (no pull-up, if the switch is between the pin and VCC). Check the edge configuration (OPTION-register) and check that you clear the INTF before first enabling the interrupt.

Also, watch out if there is no debouncing filter for the switch. The switch alone may produce long series of pulses before the line state settles. Upto 100 ms. That could make the looping inside the interrupt a problem. Not knowing your clock frequency, I don't have any idea how long the looping takes. Looks like you are looping 65536 times.

0
votes

At first glance, your code looks OK (disclaimer; I have never worked with this particular pic and I usually code in C).

This could simply be a refresh issue as the microchip tools (ex. PicKit, ICD, ... ) don't get real-time memory information, they load the memory of the pic when code execution pauses. Your post does not mention anything to rule out this issue. To see a change in the variable, you will have to occasionally pause execution so that the IDE has a chance to reload the PIC's memory and refresh the value.