Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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) . :oops:

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**

Interrupt upon a change on pins 4, 5, 6 and 7 of port B

Change of input signal on PORTB <7:4> sets RBIF (INTCON<0>) bit. Four pins RB7, RB6, RB5 and RB4 of port B, can trigger an interrupt which occurs when status on them changes from logic one to logic zero, or vice versa. For pins to be sensitive to this change, they must be defined as input. If any one of them is defined as output, interrupt will not be generated at the change of status. If they are defined as input, their current state is compared to the old value which was stored at the last reading from port B.

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 :p).

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 :oops:) 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.

Latest threads

New Articles From Microcontroller Tips

Back
Top