Unformatted text preview:

University of Florida EEL 4744 Dr. E. M. SchwartzElectrical & Computer Engineering Dr. A. A. ArroyoPage 1 of 2 Comments on Scanning a Keypad TA: Vic Brennan1. IntroductionThere was a lot of confusion on scanning a keypad.Instead of coming up with their own scan routine,the handout scheme was used without examining theunderlying hardware. This paper is a supplement tothe original Lab 6 handout.31 24 5367 8 90 * #Port C 0Port C 2Port C 1Port C 3Port D 4 Port D 3 Port D 2Figure 1: Key Pad2. Basic HardwareEach key has two contacts, one attached to a “row”wire and the other attached to a “column” wire.When a key is pressed, the column wire and rowwire are connected. There is no power supplied tothe keypad except through an output port or with apower supply (through a pull-up or pull-downresistor). I will briefly discuss:1) Scanning schemes,2) Generating an interrupt,3) Pressing multiple keys.3. Basic Approach for ScanningWhen a key is pressed, a connection is establishedbetween port C and port D. A value written to oneport can be read from the other port. But what willbe read on the unconnected (key not pressed) pins ofthe input ports? Pull-up or pull-down resistors areused so that the input port pins don’t have floatingvalues. A large enough resistance should be used sothat the current drawn through the resistor can behandled by the port. Without additional informationon the ports of a specific board, the following values(Figure 2) should be assumed.Key NoResistorsPull-up on InportPull-downon InportNot Pressed FLOAT 1 0Pressed,outport bit=00 0 0Pressed,outport bit=11 1 1Figure 2: Values at the Input Port Pins• With pull-down resistors the input ports will beat “0” when no key is pressed and a “1” writtenon the output port can be detected.• With pull-up resistors the input ports will be at“1” when no key is pressed and a “0” written onthe output port can be detected.4. Two Sets of ResistorsAt the design stage, it does not matter whether theresistors are pull-up or pull-down, or which port isinput and which is output. With one set of resistorsyou can use one port as an input port and know thatyou will either read “0” (GND) or “1” (Vcc).Resistors on both the columns and rows allow bothports to be used as input ports. The resistors do notinterfere in any way with the ports when they operateas output ports. Although this example uses 2 setsof pull-up resistors, 2 sets of pull-downs or a pull-up/pull-down set can be used just as easily. Here is apossible sequence:1) Find Columns• Set port C (rows) as input ($00 to DDRC).• Set port D (columns) as output ($FF toDDRD) and write zeros ($00 to PORTD).• Read Port C and the pin with a “0” is thecolumn of the pressed key.2) Find Rows• Set port D (columns) as input ($00 toDDRD).• Set port C (rows) as output ($FF to DDRC)and write zeros ($00 to PORTC).• Read Port D and the bit with a “0” is the rowof the pressed key.This is really simple to code. This is also the type ofscheme which could give you wrong results ifmultiple keys are pressed.EEL 4744C LAB 6: PARALLEL PORT IO PAGE 2 OF 231 24 5367 8 90 * #Port C 0Port C 2Port C 1Port C 3Port D 4 Port D 3 Port D 2VCCFigure 3: Pull-Up Resistors4.1 Keypad ScanningIf hardware minimization is important, only one setof resistors is needed. The remainder of this paperwill assume only one set of pull-up resistors.With only one bank of resistors, only one port can beused as an input port. The code is only slightly morecomplicated. Place pull-up resistors on port D(Figure 3) to use port D as an input port. Port C isan output port. Since the directions on the portswon’t change, write $00 to DDRD and $FF toDDRC in the initialization. The row and column ofthe pressed key are found at the same time,1) Send %XXXX1110 to port C (Check row 1).2) Read Port D• The bit with a “0” is the column of thepressed key.• If no bit was “0”, go to next row3) Check subsequent rows by writing to port C,• %XXXX1101 to check row 2.• %XXXX1011 to check row 3.• %XXXX0111 to check row 4.4) Loop back to step 2.This technique requires more loops, but is easier toupdate the index into the lookup table. However, iftwo keys on the same column are pressed, an outputport pin at GND can be shorted to an output port pinat Vcc. The question of damage aside, I would notknow the actual voltage at the inport whensimultaneously attached to two outport pins atdifferent voltages.4.2 Generating an InterruptAgain, pull-up resistors are on the input port D, a“0” written on port C can be detected if theappropriate key is pressed. If all the pins on port Care “0”, then one of the pins of input port D will alsobe “0” when any key is pressed. Connect port D pins2-4 (column wires) to the inputs of a 3-input NANDgate. When any key is pressed, the output of theNAND gate goes high. Therefore, choose a risingedge to trigger the STAF. A read of port D also tellsyou the column of the pressed key, so the scanroutine is simplified.4.3 Short between Pins of the Output PortWe don’t have to worry about shorts while waitingfor the interrupt since all the output pins are at thesame voltage anyway. When scanning, two outputpins cannot be shorted if there is only one output pin.1) Send %XXXX0001 to DDRC.2) Write %XXXX0000 to PORTC (Check row 1).• Read Port D• The bit with a “0” is the column of thepressed key.• If no bit was “0”, go to next row3) Check subsequent rows by writing to DDRC,• %XXXX0010 to check row 2.• %XXXX0100 to check row 3.• %XXXX1000 to check row 4.4) Loop back to step 2.5. Some Trade-Offs5.1 Hardware versus CodeThe bottom line is that some limitations in hardwarecan be overcome with good code, and that extrahardware can make coding a lot easier.5.2 SafetyThe scan cycle is short enough that damage shouldnot be a problem under normal operation. However,debugging is not normal operation, and why takechances on damage when there is an easyalternative?5.3 Polling versus InterruptInterrupt service routines usually are slightly morecomplicated than polling routines and moreappropriate for devices that don’t require frequentservicing. That is, ISR’s are better if the time freedby not polling is more that that incurred by extracode complexity (and you have something else to dowith the freed


View Full Document

UF EEL 4744 - Comments on Scanning a Keypad

Download Comments on Scanning a Keypad
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Comments on Scanning a Keypad and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Comments on Scanning a Keypad 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?