I am trying to configure the port b interrupt on change interrupts to work with a program I am writing with MC XC8 demo version. I am using a PIC16F628A I am using this code to initialize the pic for interrupt reading. However it seems when I start up my program an interrupt is called. I don't understand why this is happening as the datasheet says interrupts will only be generated for pins set as an input and I have set all of port B to be output. Could someone please explain this strange behavior

int main()
    TRISA2 = 0;
    TRISA3 = 0;
    PORTB = 0;
    TRISB = 0;
    CMCON = 0x07;
    writeString("Hello World ", 11);
    GIE = 1;
    RBIE = 1;
    PEIE = 0;

void interrupt interruptRoutine()
        writeString("Interrupt", 9);
        RBIF = 0;

1 Answer 1


I'm guessing that the interruptRoutine gets called between RBIE = 1; and PEIE = 0;. That's a brief opportunity between instructions for an already-pending interrupt to fire. (note that the interrupt latency could make it appear that it happened an instruction or two later than it actually did) So you might want to rearrange your interrupt configuration instructions to close that gap.

Now the question is, "Why do you get the interrupt at all, given that they're all outputs and the datasheet says that it won't generate one?" I suspect that the interrupt was actually generated while they were still inputs, because that's the default direction and it's really hard to change immediately on power-up. So you already have an interrupt pending, just waiting to be enabled.

When I configure interrupts, I always make sure to clear the flags just before I enable them. That keeps any garbage interrupts from occurring.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.