0
\$\begingroup\$

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;
    initialise();
    writeCommand(0b00001111);
    writeCommand(0b00000110);
    writeString("Hello World ", 11);
    GIE = 1;
    RBIE = 1;
    PEIE = 0;
    while(1);
}

void interrupt interruptRoutine()
{
    if(RBIF)
    {
        writeString("Interrupt", 9);
        RBIF = 0;
    }
}
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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