Using Push Button Switch – MPLAB XC8

Using Push Button Switch – MPLAB XC8

Push Button Switch

Push Button Switch

I hope that you already go through the first tutorial of MPLAB XC8, Getting Started with MPLAB XC8 – LED Blinking. In that tutorial we learn how to use an output pin by driving an LED. In this we will learn how to read an Input pin using a push button switch. We already seen that TRIS register is used to set direction of each IO pin, ie Input or Output and PORT register is used to read or write status of an IO pin. 1 at TRIS bit makes the corresponding pin Input while 0 at TRIS bit makes the corresponding pin Output. An Input pin will be in Hi-Impedance state.Writing 1 to PORT bit corresponding to an Output pin makes the pin Logic High (VDD) while writing 0 to PORT bit makes the corresponding Output pin Logic Low (VDD). PORT register can be used to read physical state (actual voltage level) of an input pin. If an Input pin is at a potential near to VDD, reading PORT bit will give 1 and if the input pin is at a potential near to VSS, reading PORT bit will give 0.

In this tutorial a push button switch is connected to a pin declared as input (TRIS bit 1) and an LED is connected to a pin declared as output (TRIS bit 0).  When the switch is pressed, the LED will glow for 3 seconds.

Circuit Diagram

Using Push Button Switch - PIC Microcontroller

Using Push Button Switch – PIC Microcontroller

VDD and VSS of PIC Microcontroller is connected to +5V and GND respectively to provide the necessary power for the operation of microcontroller. 8MHz crystal is used to provide necessary clock for the operation of microcontroller. Pin RD0 (PIN 19) is declared as an input pin (TRIS bit 1) to which push button switch is connected. Pin RB0 (PIN 33) is declared as an output pin (TRIS bit 0) to which LED is connected. A 10KΩ PULL UP resistor is used along with push button switch to ensure that RD0 is at Logic High (VDD) when the switch is not pressed. Whenever the switch is pressed, RD0 becomes Logic Low (VSS). A 470Ω resistor is used to limit the current through the LED.

MPLAB XC8 Code

#define _XTAL_FREQ 8000000

#include <xc.h>

// BEGIN CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
//END CONFIG

int main()
{
  TRISB0 = 0; //RB0 as Output PIN
  TRISD0 = 1; //RD0 as Input PIN

  RB0 = 0; //LED Off

  while(1)
  {
    if(RD0 == 0) //If Switch Pressed
    {
      RB0 = 1; //LED ON
      __delay_ms(3000); //3 Second Delay
      RB0 = 0; //LED OFF
    }
  }
  return 0;
}

I hope that you can understand program through its comments. In some cases you may need to recheck the switch is being pressed after a delay, like the following.


if(RD0 == 0) //If Switch is presseed
{
  __delay_ms(100); //Provides required delay
  if(RD0 == 0) //If Switch is still pressed
  {
    RB0 = 1; //LED ON
    __delay_ms(3000); //3 second delay
    RB0 = 0; //LED OFF
  }
}

You should recheck the status of the switch after a small delay due to following reasons.

  • As the clock frequency is 8MHz, the program is running with that speed but the closing and opening of switch varies from 100 – 300ms. Thus a delay should be provided while reading switch. For eg : If you haven’t used delay while reading status of a switch which is used for counting purposes, it may count multiple times with a single press due to high speed of program execution.
  • It will avoid false triggering by makes sure that switch is being pressed after a delay.
  • It will provide Switch Debouncing.

Download Here

You can download the entire project files here . . .

Share this post

Comments (21)

  • Nam

    What will happen if switch is press and hold down for long time…

    January 27, 2015 at 1:02 pm
  • Ligo George

    It will make the LED glow continuously…

    January 27, 2015 at 5:36 pm
  • mayur

    can you mail me the correct and whole program on [email protected]

    February 14, 2015 at 5:39 pm
  • Ligo George

    The above program is 100% correct.

    February 17, 2015 at 9:07 am
  • ali

    hi friend.i’m a new beginner in pic .so i wish help me for c code in mplab.

    May 5, 2015 at 12:02 pm
  • Ligo George

    Please follow our tutorials… If you have any doubts, you can use our forums https://electrosome.com/forums

    May 6, 2015 at 10:27 am
  • chirag patel

    HI.

    i m using mplab with hi-tech compiler but it doesn’t allow me to put delay more than 100ms (__delay_ms(150); )

    please give me solution for that.

    thanks heaps.

    August 15, 2015 at 6:32 pm
  • RnD

    Its not working with MPLAB X IDE V3.10..
    Can u please tell me why ?

    September 16, 2015 at 5:29 pm
  • RnD

    its not working…

    September 16, 2015 at 5:30 pm
  • RnD

    i am using MPLAB X v3.10 but so many errors..!! 🙁

    September 16, 2015 at 5:31 pm
  • Ligo George

    For Hi-Tech C you need to include the header file htc.h. Please refer the following article for that.

    https://electrosome.com/switch-pic-microcontroller-hi-tech-c/

    October 3, 2015 at 12:42 pm
  • Ligo George

    You have to use install MPLAB XC8 compiler along with IDE.

    October 3, 2015 at 3:24 pm
  • Nuzly Nuz

    I tried to use the push button in port A pins and port E pins but it didnt work then i switched to port B pins it worked. why is that???? i have no idea. the pic i used is 16F877A

    February 15, 2016 at 2:41 pm
  • Ligo George

    PORTA and PORTE are multiplexed with analog inputs. So you need to define those pins are digital by writing to ADCON1 or ANSEL registers.

    February 27, 2016 at 7:51 pm
  • Lisa Nielsen Bred

    Hello. I’m getting a “newmain1.c:20: error: (192) undefined identifier “RB0″” error. What can I write instead of RB0? Or do I have to define it?

    April 4, 2016 at 1:55 pm
  • Ligo George

    Don’t you included the header file. Please verify that you selected the correct pic. You can also try PORTBbits.RB0.

    April 15, 2016 at 8:06 am
  • henry2576

    Hi, what happens when an input pin receives a short input before the pin is read? is this short input missed or is it held in the port register until its been read and then register is cleared? my problem is I cant afford to lose this input signal as this will stop another process/action taking place, at the moment I’m using an interrupt to ensure this doesn’t get lost but I’m not sure if this is necessary and have limited inputs with interrupts.

    April 15, 2016 at 5:42 pm
  • Ligo George

    Yes, you can definitely use interrupt for that purpose.
    If the polling is fast enough, it will not lose any inputs.

    June 2, 2016 at 5:58 pm
  • Md. Aminur Rahman (Sohel)

    we know the processes of continuous monitoring is known as POLLING , but how is it in c code. actually i wanted to make a project that will monitor its input and when the input is high it will send a sms to its given number again when its input is low it will also send a sms to given number. i putted all code in while(1) loop but its continuously sending sms. please help me how can i get rid of from it.

    October 30, 2016 at 2:48 pm
  • Ligo George

    You might need to add delay in required places.

    Kindly use our forums ( https://electrosome.com/forums/ ) for asking doubts outside the scope of above article.

    November 27, 2016 at 2:29 pm
  • kristiano

    I’m trying to do in pic12f683, I just change RB0 for GP0 ​​and RD0 for GP1 will work? i need to help to work in pic12f683

    June 6, 2018 at 7:24 am

Leave a Reply

Your email address will not be published. Required fields are marked *