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

16F886 Port B interrupts

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 23, 2010 2:54 pm     Reply with quote

Here's a demo program that is a modification of the CCS Ex_pbutt.c
program. It demonstrates how to do PortB Interrupt-on-Change
interrupts and External interrupts in the same program.

It has Port B Interrupt-on-change interrupts on Pins B1 and B2,
and External interrupts on Pin B0. When you press a push-button
on any of those pins, you will get output on the Terminal window of
your PC, showing which button was pressed.
Code:

#include <16F886.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)


short int dbutton0, dbutton1, dbutton2;

//-------------------------
// This function reads Port B without changing the TRIS.
int8 input_state_b(void)
{
#byte PortB = getenv("SFR:PORTB") 

return(PortB);
}

//-------------------------
#int_ext
void int_ext_isr(void)
{
dbutton0 = TRUE;
delay_ms(1);   // Debounce delay for push-button switch
}

//-------------------------

#define HIGHTOLOW  FALSE
#define LOWTOHIGH  TRUE

int8 current, last;

#int_rb
void int_rb_isr(void)
{
current = input_state_b();

#if LOWTOHIGH
   if ((!bit_test(last,1))&&(bit_test(current,1)))
       dbutton1 = TRUE;
   if ((!bit_test(last,2))&&(bit_test(current,2)))
       dbutton2 = TRUE;
#elif HIGHTOLOW
   if((!bit_test(current,1))&&(bit_test(last,1)))
       dbutton1 = TRUE;
   if((!bit_test(current,2))&&(bit_test(last,2)))
       dbutton2 = TRUE;
#endif

last = current;

delay_ms(1);
}

//------------------------------
void init()
{
dbutton0=0;
dbutton1=0;
dbutton2=0;

current = input_state_b();
last = current;

}

//=======================================     
void main()
{
init();

port_b_pullups(0b00000111); // Enable pullups on PortB bits 0,1,2
delay_us(10);   // Allow time for pull-ups to initialize

enable_interrupts(INT_EXT_L2H);
enable_interrupts(INT_RB1);
enable_interrupts(INT_RB2);

input_state_b();   // Clear mismatch condition

clear_interrupt(INT_RB);
clear_interrupt(INT_EXT);

enable_interrupts(GLOBAL);

while(1)
  {
   if(dbutton0)
     {
      putc('0');
      dbutton0 = FALSE;
     }

   if(dbutton1)
     {
      putc('1');
      dbutton1 = FALSE;
     }

   if(dbutton2)
     {
      putc('2');
      dbutton2 = FALSE;
     }
  }
}
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