Momentary Pushbutton Switch Implementation with PIC Microcontroller | ECE 480, Study notes of Principles of Theater Design

Material Type: Notes; Professor: Goodman; Class: Senior Design; Subject: Electrical & Computer Egr; University: Michigan State University; Term: Spring 2007;

Typology: Study notes

Pre 2010

Uploaded on 07/22/2009

koofers-user-5yx
koofers-user-5yx ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Momentary Pushbutton Switch Implementation
with PIC Microcontroller
Application Note
Cammi Stewart
ECE 480 โ€“ Spring 2007
March 30, 2007
Executive Summary
This application note explains both the hardware as well as the
software behind using a momentary pushbutton switch with a PIC
(peripheral interface controller). The hardware portion details the
inner workings of a basic pushbutton switch and how it makes contacts
with a circuit. The software portion details how to handle user input in
the PIC programming when the button is pressed.
Keywords
Switch, pushbutton, actuator, PIC, bounce, debounce
pf3
pf4
pf5

Partial preview of the text

Download Momentary Pushbutton Switch Implementation with PIC Microcontroller | ECE 480 and more Study notes Principles of Theater Design in PDF only on Docsity!

Momentary Pushbutton Switch Implementation

with PIC Microcontroller

Application Note

Cammi Stewart

ECE 480 โ€“ Spring 2007 March 30, 2007 Executive Summary This application note explains both the hardware as well as the software behind using a momentary pushbutton switch with a PIC (peripheral interface controller). The hardware portion details the inner workings of a basic pushbutton switch and how it makes contacts with a circuit. The software portion details how to handle user input in the PIC programming when the button is pressed. Keywords Switch, pushbutton, actuator, PIC, bounce, debounce

Introduction Switches can be used for a variety of applications but their basic purpose is simple. Switches, denoted by the name, are used to make, break, or change connections in a circuit. There are many different kinds of switches. Some switches, such as sliders, when toggled, will remain in that position until further intervention (Figure 1). These are used to select between multiple contact points to close the circuit. Other switches are only temporary when pressed. A momentary pushbutton switch contains a spring that returns the actuator to the original position. This type of switch can be used when a user wants to use the same switch for multiple triggers. Slider (SPST) Single Pole Single Throw Circuit closed when slid Slider (SPDT) Single Pole Double Throw Circuit closed when slid either way Figure 1 โ€“ Example of different switch types: slider switches Objective This application note explains how a momentary pushbutton switch works and how to implement it with a PIC microprocessor. Figure 2 โ€“ Pushbutton Switch Hardware There are two types of momentary pushbutton switches. When a momentary-on switch is pressed the circuit is closed; it then opens again when the button is released. Momentary-off switches act in a

contacts are made the current rapidly pulses and oscillates like a sinusoid before it finally sticks at the proper logical value (Figure 5). This is a problem because the PIC may receive multiple triggers when the button is only pressed once. Logic Low Logic High Logic Threshold Figure 5 โ€“ Button bounce 1 There are two methods for correction, called debouncing. The first approach is to add additional hardware to the circuit. Inserting a capacitor across the switch and a pull-up resistor will be sufficient. When the switch is closed, the capacitor is discharged and has no effect. When the switch is opened the capacitor takes time to recharge so the bounce has minimal effect on the input to the PIC. This approach is easier to implement but costs more due to the additional hardware components. The other method to account for bounce uses a software approach. After the initial button press a delay is triggered in the code sufficiently long to keep the bounce from being detected. This approach is cheaper but the delay must be determined based on the switch model for optimal operation. Software There are multiple ways to implement a switch in the PIC programming. An inline method using a loop is adequate for simple applications. Typically a continuous loop runs until the button is pressed and then the intended action is performed. The logic is very simple as outlined below.

//Loop Code PORTBbits.RB7 = 1; //PIC input pin while(1) { if(PORTBbits.RB7 == 0 ){ //delay set time accounts for bounce //perform action here โ€“ 1 st^ button press } if(PORTBbits.RB7 == 0 ){ //delay set time accounts for bounce //perform action here โ€“ 2 nd^ button press } if(PORTBbits.RB7 == 0 ){ //delay set time accounts for bounce //perform action here โ€“ 3 rd^ button press } //if another button press, recycle } Another, probably more efficient, way to program the PIC to handle a button is to use an interrupt. When the button is pressed, the interrupt handles the actions to be performed then jumps back to the main code. The following code was used to change between three modes on a particular device when the button is pressed. This assumes the user accounts for bounce using the hardware method. //Interrupt Code /* setup the mode switching button interrupt / ConfigINT1(RISING_EDGE_INT & EXT_INT_ENABLE & EXT_INT_PRI_3); / This interrupt is for the mode selection button */ void attribute((interrupt)) _INT1Interrupt(void) { switch (active_mode) { case PEAK_MODE: active_mode = AVERAGE_MODE; *active_mode_str = "AVERAGE"; // display_number(current_peak); //send peak value to //Display break; case AVERAGE_MODE: active_mode = INTEGRATED_MODE; *active_mode_str = "INTEGR."; break;