CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PortB Interrupts and Interrupt Flags

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
feederic



Joined: 08 Apr 2008
Posts: 7

View user's profile Send private message

PortB Interrupts and Interrupt Flags
PostPosted: Sat Apr 19, 2008 7:05 pm     Reply with quote

I'm trying to use the portb interrupts using RB0-RB4 on the 16f887.
I'm not sure if there are flags for each pin that I can check. Basically i want my code to do something like this. Each button will output a LED on interrupt. I've tried the code below, but after an interrupt is generated all the flags are true.

I'm not sure if each pin on port b already has its own interrupt flag that I can check or if I have to write my own.

Code:

#INT_RB
void button_interrupt_service(void)
{
if(int_rb0)
{
while(1)
output_d(0xff);
}

if(int_rb1)
{
while(1)
output_d(1);
}

if(int_rb2)
{
while(1)
output_d(2);
}

if(int_rb3)
{
while(1)
output_d(4);
}

if(int_rb4)
{
while(1)
output_d(8);

}
}


Please offer any advice or helpful links.
foodwatch



Joined: 18 Apr 2006
Posts: 66

View user's profile Send private message Yahoo Messenger

PostPosted: Sat Apr 19, 2008 8:55 pm     Reply with quote

If memory serves me correctly, the interrupt occurs when ANY of the four inputs change. You must then quickly read the pins with your interrupt routine to see which one really did change.
Ttelmah
Guest







PostPosted: Sun Apr 20, 2008 2:45 am     Reply with quote

The existing reply is spot on.
There is an internal 'latch' in the chip, reflecting the state of the inputs, _when they were last read_. The interrupt occurs, when the input pins differ from this state. Inside the interrupt, you _must_ read the port (to reset this latch). It is up to you to determine which pin changed. Also remember that the interrupts will occur on both the rising and falling edges of the signals. A typical 'RB changed' handler, would be:
Code:

static int8 old_pins;
#define first_bit(x) (x&0x1)
#define second_bit(x) (x&0x2)
#define third_bit(x) (x&0x4)
#define fourth_bit(x) (x&0x8)

#INT_RB
void b_changed(void) {
    int new_pins;
    int changes;
    new_pins=input_b(); //read the port to reset the latch
    //Now simply test the bits, to see which pin(s)
    //have changed.
    changes=new_pins^old_pins; //This now reflects every bit that has
    //changed.
    changes=changes&new_pins; //This now reflects every bit that has
    //gone _high_ (use 'old_pins', instead of 'new_pins',if you want to find
    //the ones that have gone low).
    old_pins=new_pins;  //update your stored value for the future

    if (first_bit(changes)) {
         //here RB0 has gone high
    }
    if (second_bit(changes)) {
        //Here RB1 has gone high
    }
    if (third_bit(changes)) {
       //Here RB2 has gone high
    }
    if (fourth_bit(changes)) {
       //Here RB3 has gone high
    }
}
 
//Inside the 'main' at the start of the code, just before enabling the
//interrupt  have:
    old_pins=input_b();


There are individual flags in this chip to _enable_ which pins will generate the interrupt, but nothing to say which pin has triggered the interrupt. If you look at Figure 14-7 in the data sheet, you will see that there is only one interrupt 'flag', fed by all these sources (top left corner of the diagram).

Best Wishes
feederic



Joined: 08 Apr 2008
Posts: 7

View user's profile Send private message

PostPosted: Mon Apr 21, 2008 12:56 am     Reply with quote

Thanks for the advice! The buttons are now working perfectly!

I'm glad I stumbled on to this forum, I've learned more this past week than in the past two months Smile !
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group