interrupt on portb change

Status
Not open for further replies.

spirosd

New Member
Hello all,

I am having a tricky time coding the portb on change interrupt feature on a 16f84a (using C and PICCLite) .

This is the interrupt routine that works

Code:
static void interrupt isr(void)
{       
                
        // TMR0 interrupt handler
        if(T0IF) {
                RB0 ^= 1;
                if(EnableMotors) {
                        PulseCounter++;
                        if(PulseCounter == PULSE_MAX) {
                                PULSE_MOTORS=1;
                                PulseCounter = 0;
                        }
                }       
                // reset timer0
                T0IF=0;
        }
        
        // handcontroller interrupt on PORTB
        if(RBIF) {      
                RBIF = 0;
                CheckPaddle = TRUE;
        }
        
}

Notice that I am toggling the RB0 pin. Now, if I comment this line out, the RBIF interrupt seems to not get triggered. :roll:

Any insights would be greatly appreciated.

Thanks for your help
Spiros
 
Is it possible that toggling RB0 triggers the PORTB interrupt code? I would guess that you are actually passing through the interrupt handler twice. The first pass starts with the TMR0 interrupt. That pass is halted when you toggle RB0 because the PORTB interrupt occurs. The second pass then occurs which executes your PORTB interrupt block. When it is done, pass 1 resumes.
 
Thanks for your reply loosewire,

After your reply, it occured to me that RB0 is the general interrupt pin of PORTB and that might be confusing things. Using RB1 has exactly the same effect. ':?'

RB0/RB1 is set up as an output pin (and should not trigger an interrupt, I think). This is how I am setting up PORTB:

Code:
        // port B setup (our control port)
        PORTB = 0;
        RBIF = 0;
        RBIE = 1;
        RBPU = 0;       // enable pull-ups
        TRISB = 0b00111000; // pins 3,4,5 are input the rest outputs

I am trying to utilise the interrupt on change feature of pins 4,5,6,7 of PORTB (generate interrupt when button is pressed, wake from sleep).

Really confused now! Thanks for your help.
 
I found the following quote at this URL
**broken link removed**


The wording is a little ambiguous, but it sounds like you might need to define all four pins as input in order to get the interrupt to occur.
 
Very interesting point loosewire (thanks for the url by the way ).

I spent about 4 hours yesterday working through my code and the test circuit (checking and tripple checking to make sure I did not do my usual silly wiring or coding mistake ) and I discovered that disabling the internal weak pull-ups fixed the thing. Setting RBPU = 1 allowed the interupt to take place, without pulsing any other pins of PORTB.

Now, contrary to what the quote specifies, the interrupt still takes place with only 3 out of the 4 pins specified as input (decided to use pin 6 instead of pin 3).

Hmmmm, more testing ahead of me I guess :lol:. I will try specifying pins 4:7 all as input (and ground pin 7 so it is not left floating), with RBPU = 0, and see what happens. Will report back with the findings.

Thank you all for your help.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…