Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Detect button press via Interrupt in PIC16f877A

Detecting Button Press in PIC becomes easy if you use the 'RB' interrupt.The RB interrupt will happen when there is any change (input or output) on pins B4-B7. There is only one interrupt and the PIC does not tell you which pin changed.  The programmer must determine the change based on the previously known value of the port.  Furthermore, a single button press may cause several interrupts due to bounce in the switch.  A debounce algorithm will need to be used.
here is a sample simulation ...........

#include <16F877A.h>
#device adc=8
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
short int but4, but5, but6, but7;
int a=0,done=0;
#define LCD_TYPE 2
#include
void clear_data();
void button_read();
#int_rb   //interrupt service routine
void detect_button_press()
{
int current;
static int last=0;
lcd_gotoxy(1,1);
lcd_putc("interrpt start");
set_tris_b(0xF0);
current=input_b();
if ((!bit_test(last,4))&&(bit_test(current,4)))
{
but4=1;
lcd_gotoxy(1,2);
lcd_putc(" pressed 4");
}
if ((!bit_test(last,5))&&(bit_test(current,5)))
{
but5=1;
lcd_gotoxy(1,2);
lcd_putc(" pressed 5");
}
if ((!bit_test(last,6))&&(bit_test(current,6)))
{
but6=1;
lcd_gotoxy(1,2);
lcd_putc(" pressed 6");
}
if ((!bit_test(last,7))&&(bit_test(current,7)))
{
but7=1;
lcd_gotoxy(1,2);
lcd_putc(" pressed 7");
}


void main()
{
button_read();//this fun sets the value of variable a by pressing buts 1&2 and returns when but 3 is presed
//ints are disabled while exiting the fun,
//after that u can use the value of a to set 1307 values or any thing

 while(1)
 {        
 output_bit(pin_c3,0);
  delay_ms(200);
   output_bit(pin_c3,1);
   delay_ms(200);
   output_bit(pin_c3,0);
    delay_ms(200);
   output_bit(pin_c3,1);
     delay_ms(200);
 }

}
void clear_data()
 {
   but4=0;
   but5=0;
   but6=0;
   but7=0;
 }
void button_read()
{
 clear_data();
enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);
lcd_init();
lcd_gotoxy(1,1);
lcd_putc("start");
while(done==0)
{
lcd_gotoxy(1,1);
lcd_putc(" loop");
if(but4==1)
   {
   output_high(pin_c0);
   a++;
   clear_data();
   }
 if(but5==1)
  {
  output_high(pin_c1);
  a--;
  clear_data();
  }
   if(but6==1)
   {
   output_high(pin_c2);
   done=1;
   clear_data();
   }
   if(but7==1)
   {
  output_high(pin_c3);
  clear_data();
   }
  lcd_gotoxy(1,1);
   printf(lcd_putc,"%d",a);
  delay_ms(1000);
  lcd_putc("\f");
  }
  disable_interrupts(GLOBAL);
  disable_interrupts(INT_RB);
  lcd_gotoxy(1,2);
  lcd_putc("int disabled");

}

simulated in PROTEUS and programmed in CCSC


This post first appeared on Compizspec.blogspot.com, please read the originial post: here

Share the post

How to Detect button press via Interrupt in PIC16f877A

×

Subscribe to Compizspec.blogspot.com

Get updates delivered right to your inbox!

Thank you for your subscription

×