Wednesday, August 25, 2010

Interfacing Switch to Microcontroller & Switch Debouncing

In 8051 PORT 1, PORT 2 & PORT 3 have internal 10k Pull-up resistors whereas this Pull-up resistor is absent in PORT 0. Hence PORT 1, 2 & 3 can be directly used to interface a switch whereas we have to use an external 10k pull-up resistor for PORT 0 to be used for switch interfacing or for any other input.Figure 1 shows switch interfacing for PORT 1, 2 & 3. Figure 2 shows switch interfacing to PORT 0.

Image
Image
For any  pin to be used as an INPUT PIN a HIGH (1) should be written to the pin if you don’t do this the pin will always be read as LOW.In the above figure when the switch is not pressed the 10k resistor provides the current needed for LOGIC 1 closure of switch provides LOGIC 0 to the controller PIN.Let's write a small program where whenever a switch is pressed a LED is turned ON. Consider that switch is connected to P2.0 & an LED connected to P2.1. 

PROGRAM 1 

ORG 0000h
                SETB P2.0
                SETB P2.1
loop1:
                JB P2.0,loop1
                clr P2.1
loop2:
                JNB P2.0,loop2
                SETB P2.1
                AJMP loop1
 First we initialize the Port Pins that we are using. Since we are using P2.0 as an InputPin we write logic 1 to it (SETB P2.0). We are using PIN P2.1 for LED. Initially LED is turned OFF this is done by pulling the PIN HIGH (check LED interfacing Section).  Now we check if the Switch is pressed or not. So we wait till switch is pressed (loop1: JB P2.0,loop1) as soon as switch is pressed the LED is TURNED ON ( CLR P2.1 ) . Then we wait till the switch to be released (loop2: JNB P2.0, loop2 ) & then we TURN OFF the LED (SETB P2.1). You must have noticed in the above program that the status of the input pin is same as that of Output Pin i.e. if the switch is not PRESSED (HIGH) the Output pin is also HIGH (LED OFF) & when the switch is PRESSED (LOW) the Output pin is also LOW (LED ON) using this logic we can write in another way. 
PROGRAM 2 
ORG 0000h
                SETB P2.1
                SETB P2.0
loop:
                MOV C,P2.0
                MOV P2.1,C
                AJMP loop
 The Output of both the programs will be the same.Now you know the basic concept of switch interfacing. There is a problem in practically interfacing these switches to the controller. In the above case you have considered the switches to be an IDEAL SWITCHwhere when the switch is pressed the controller directly gets LOGIC 0. But practically when a switch is closed the contacts open & close rapidly for about 30ms. This is called as SWITCH BOUNCING. Figure 3 shows its waveform.
Image
As you can see the switch release is clean without any bouncing. When a switch is pressed the contacts open & close for about 20ms. In the above programs the LED will flicker initially when the switch is pressed because of the SWITCH BOUNCING but since the flickering will be very fast & will not be detected by human eye.Even though 20ms is very short time in human terms for a microcontroller it is a very long time. WithoutSWITCH DEBOUNCING the controller will think that the switch was pressed many times.Let us modify PROGRAM 1 & used SWITCH DEBOUNCING in it. 

PROGRAM 3
ORG 0000h
                SETB P2.0
                SETB P2.1
loop1:
                JB P2.0,loop1
                mov R7,#255
deb1_loop1:
                JB P2.0,loop1
                djnz R7,deb1_loop1
                CLR P2.1
loop2:
                JNB P2.0,loop2
                SETB P2.1
                AJMP loop1
 In this modified program the controller waits  for LOGIC 0 ( loop1: JB P2.0,loop1 ) on input pin i.e. P2.0 then loads R7 with 255 now it checks if the input PIN still has LOGIC 0 (  JB P2.0,loop1 ) the controller checks this LOGIC 0 till the value in R7 becomes 0 which in this case is 255 times. If even once the controller detects LOGIC 1 then it exits the debouncing loop & goes back to the main loop ( loop1 ) indicating error in switch press. Basically the code:- 
                mov R7,#255
deb1_loop1:
                JB P2.0,loop1
                djnz R7,deb1_loop1is used for switch debouncing if you think the debounce period is less then use the LOOP WITHIN LOOP TECHNIQUE to check the input pins for more than 255 times. But personally I never had any problems with debouncing using the above code. 

No comments:

Post a Comment