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
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
0 Comments