Wednesday, February 3, 2016

PUSH BUTTON INTERFACING WITH MICROCONTROLLER

Hi Friend's Today we interface a push button with PIC microcontroller 16F886 .




Circuit Diagram of Push Button with Microcontroller

In this circuit we connect a PUSH BUTTON at PORTA.F0 and we connect a LED at RORTB.F0 for indication. in this circuit we connect a 10K Ohm Resistance with PUSH BUTTON for PULL DOWN the Floating input of Microcontroller. 

CODE of mikroC for PIC

PROGRAM 1:-

void main()
{
 ANSEL = 0x00;                                          // all input are Digital
 TRISA.F0 = 1;                                     //make PORTA 0th bit as input
 TRISB.F0 = 0;                                    //make PORTB 0th bit as output

 while(1)                                                        // infinit loop
 {
  if(PORTA.F0 == 1)                                          //chack Push button
  {
   PORTB.F0 = 1;                                        //if pressed than on LED
   Delay_ms(10);                         //delay of 10 ms to stabilize the output
  }
  else
  {
   PORTB.F0 = 0;                               //if not pressed than off the LED
   Delay_ms(10);                         //delay of 10 ms to stablise the output
  }
 }
}

OUTPUT
In this code if we press the PUSH BUTTON than the LED connect at portb.f0 is on but if we realise
the PUSH BUTTON than the led is off.

PROGRAM 2:-

bit LED_STATUS;                            // define bit variable LED_STATUS

sbit LED at RB0_bit;                                      // LED at PORTB.F0

void main()

 ANSEL = 0x00;                             // confingure ansel as digital i/p
 ANSELH = 0x00;                           // confingure anselh as digital i/p
 TRISA = 0x01;                                // SELECT PORTA.F0 bit as input
 TRISB = 0x00;                               // select PORTB.F0 bit as output

 LED_STATUS = 1;                                          // initially LED ON

 while(1)                                                         // inf loop
 {
  if(PORTA.F0 == 1)                                // check input high or not
  {
   LED_STATUS = ~LED_STATUS;                         // invert LED_STATUS bit
   Delay_ms(200);                           // delay of 200ms to stablise i/p
  }
  LED = LED_STATUS;                               // LED output is LED_STATUS
 }
}

output:- Output of this program is initially LED on than we need press Button only one time than LED continues ON or OFF. We press Button again and again to ON or OFF LED. 


Note:- We need to use Pull Down or Pull up Resistance All time.