Push button switch interfacing with pic microcontroller

In: Project Code: ES000005

In this project we will control led delay using timer interrupt implementation with an interrupt service routine(ISR) with PIC16F877A 8-bit microcontroller.If you are new to PIC microcontroller programming we suggest you to execute the basic led blinking project before trying this project. If you don't have access to your own hardware for this project, you can and execute it remotely.

course thumb

Project Content

Required hardware and software

The required hardware and software for this project is as below:

  • A breadboard
  • 1 x 4mmLED
  • 1 x resistor between 220 Ohm to 1 kOhm
  • Hookup cables
  • Development board hosting PIC16F877A 8-bit microcontroller
  • MPLAB IDE(v5.05) from Microchip
  • PICKIT3 Programmer, connecting cable from PICKIT3 to your computer.
  • Power supply for the development board

  • Schematic diagram

    The schematic diagram for this project is as shown below:

    Required Components

    PIC16f877A Timer description

    Timer hardware is a crucial component of most embedded systems. In some cases, a timer measures elapsed time (counting processor clock ticks). In others, we want to count or time external events. An 8-bit timer can only count up to 256 (8-bit timer) where as an 16-bit counter can count up to 65535 (16-bit timer).

    In this project we will use timer0 to create a delay and use the delay to control the led blinking frequency.

    Prepare source code, building program source code, creating binary

    Prepare source code
     * File:   switch.c
     * IDE: MPLAB X 5.05
     * Programmer: PICKIT3
     * Compiler: XC8
    #pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
    #pragma config WDTE = OFF        // Watchdog Timer Enable bit (WDT enabled)
    #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
    #pragma config CP = OFF         // FLASH Program Memory Code Protection bits (Code protection off)
    #pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
    #pragma config LVP = OFF        // Low Voltage In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
    #pragma config CPD = OFF        // Data EE Memory Code Protection (Code Protection off)
    #pragma config WRT = OFF        // FLASH Program Memory Write Enable (Unprotected program memory may not be written to by EECON control)
    void msDelay(unsigned int );
    #define bit PORTAbits.RA0
    void main()
        unsigned int check;
        TRISB7 = 1; //RD0 as Input PIN
        TRISA0 = 0; //RA0 as Output PIN
        check = 100;
        unsigned int var = 0;
        bit = 1; //LED ON
        while(check > 1)
          if(RB7 == 0) //If Switch is NOT pressed
            bit = 0;//LED OFF
            var = 1;
    void msDelay(unsigned int x)
        unsigned int i;
        unsigned char j;
        for(i = 0;i < x;i++)
            for(j = 0;j < 165;j++);

    Let me explain about the above source code developed for this project. The oscillator is used as a timing source to count the number of clock cycles. We are using OPTION_REG register from PIC16F877A microcontroller which contains various control bits to configure the TMR0. The various control bits of OPTION_REG register is shown below.

    Required Components

    Follow the procedure as followed in to load the .hex file in the microcontroller

    Looking for a new project? Write and upload your project information .


    C Programming

    More Articles Categories

    Technical articles

    Prepare for software jobs

    Test your skills