Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

Read Switch with PIC16F877A

Read Switch with PIC16F877A

 

Fig: Switches

Read Switch with PIC16F877A

In this tutorial i will take you through the process of interfacing a PIC16F877A  with a switch. If you haven't gone through my previous post on blinking LEDs with PIC16F877A click here .

A switch is an electrical component used to disconnect or connect the conducting path in an electrical circuit. Electronic devices such as transistors, MOSFETs, and relays can be acted as switches and they fall under the category of electrical/electronic switches.

There are two ways of connecting a switch to a PIC MCU:

  • Pull-Up method
  • Pull-Down method
When using the Pull-Up method, the I/O pin reads a Logic One (i.e. a digital HIGH) when the switch is open but reads a Logic Zero (i.e. a digital LOW) when the switch is closed. In this method, the power supply is connected directly to a pull-up resistor.
Fig: Pull-Up method



When using the Pull-Down method, the I/O pin reads a Logic Zero (i.e. a digital LOW) when the switch is open but reads a Logic One  (i.e. a digital HIGH) when the switch is closed.
Fig: Pull-Down method

Switch Debounce:

Whenever Switches are pressed or when two metals strike each other, Spikes are usually generated. The PIC MCU  can misread these spikes as different or multiple switch press whereas the switch was pressed just once. To avoid this error ,there is need for a filtering mechanism which can be achieved with hardware (a filtering circuit) or software. In this tutorial we would use the software approach and what we are going to do is to initiate a small delay and check again for the status of the switch. if the microcontroller reads a Logic Zero ( Pull-up method) then we can be certain that the switch was pressed and vice-versa.

In this Tutorial we would be using the Pull-Up method. Our aim is to turn ON or turn OFF an LED whenever the switch is closed or opened . our switch is connected to the sixth bit of PORTD (RD5 ,  "starting from RD0") and note that RD5 must then be configured as an input pin in order to read the status of the switch. The LED is connected to the first bit of PORTB (RB0)  which is configured as an output pin.


Materials Required:

  • PIC16F877A
  • 22pf   (2)
  • 8MHZ Crystal
  • LED
  • 330R Resistor
  • 10k Resistor (2)
  • Switch

CODE:



/*
    Name     : Read Switch with PIC16F877A
    Author   : Daniel Oluwole
                       (C) 2020 Danitronics
                       danieloluwole51@gmail.com, danieloluwole51@yahoo.com
    Mobile:    +234 8188508765.
   
    Notes    :   Our aim is to turn ON or turn OFF an LED whenever the switch is closed or opened
    Device   : PIC16F877A.
*/
void main() {
  TRISD.B5 = 1;  //SET RD5 AS INPUT
  TRISB.B0 =0;   //SET RB0 AS OUTPUT
  PORTB.B0 = 0;  //INITIALIZE RBO AS LOW
 do{
  if(PORTD.B5 == 0) //CHECK IF SWITCH IS PRESSED
    {
      delay_ms(100);   //100ms delay (debouncing)
       if(PORTD.B5 == 0)   // RE-SCAN RD5
         {
             PORTB.B0 = 1;//turn ON LED
         }
    } else
        { 
          PORTB.B0 = 0;      //turn OFF LED
        }
}  while(1); // loop forever
}



Check out our next post on interfacing pic16f877a with LCD

Post a Comment

0 Comments

Ad Code

Responsive Advertisement