Push-button or simply button is a switch used mainly as a control device in many of the Embedded applications. Push-button switches are mechanical switches. Push-button switches are of two types push to on and push to off switches. Push-button switches open and closes the contact between two ends of a line and returns two to the default state when released. It is used as an input device, by connecting it to a pin of the microcontroller which is configured as an input. The button is connected to the IO pin such that it can change the state of the controller pin(high or low)when it is pressed. The change in the state of the pin can be scanned in firmware. In this article, we will discuss push-button interfacing with PIC16F877A.

Pull up mode

pull up circuit

The above figure shows push-button interfacing in pull up mode. The push-button switch with a Pull-Up or a Pull-Down mode is a better choice than a direct interface, which will source current to the microcontroller pin. The microcontroller pin is connected to Vcc through a pull-up resistor. It is also connected to ground through a push-button switch. When the switch is pressed the pin will be connected to ground and thus the voltage will drop to zero. This change can be verified in firmware if the above-mentioned pin is configured as an input.

We can also connect the push button in pull-down mode. The below figure shows the switch connection in pull-down mode. The pin will be connected to ground by default and it reads a logic zero when not pressed. When the key is pressed the pin reads a logic high.

Pull down mode
pull down circuit

Let us do a simple code to turn the LED connected to some other pin say RC0 OFF and ON according to the input at RB0 pin.

Example Firmware

Initially, we will define the clock frequency and include <xc.h> header file. You can call the defined functions such as TRIS, PORT, and __delay_ms() after adding xc.h in your source file.

Inside the main function, the data direction registers need to be configured for input and output

In a continuous infinite loop, the state of the RB0 pin need to be checked continuously for a logic 0
and when the button press is identified the controller has to wait until the denounce of the push button.RC0 pin is toggled in the next line and the led state is updated.

Switch Debouncing

When the metal contacts of the mechanical switch are in touch with another it generates spikes. The spikes generated will lead to a switch press condition when it is not done. We can introduce a filtering mechanism to avoid denouncing effect. on of the simple approach is to introduce a delay and scan again for the switch press condition. We can also use a hardware filter such as a low pass filter which filters the high-frequency signals.

Circuit diagram for Push Button Interfacing with PIC16F877A

Complete firmware example: Push Button Interfacing with PIC16F877A

The below code is the example using the above functions. Firmware toggles the LED status according to the keypress.

Spread the love, share this