0% found this document useful (0 votes)
247 views8 pages

Mazidi Keyboard PDF

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
247 views8 pages

Mazidi Keyboard PDF

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 8
THE 8051 MICROCONTROLLER i AND EMBEDDED SYSTEMS | Muhammad Ali Mazidi \ Janice Gillispie Mazidi Prentice Hall Upper Saddle River, New Jersey Columbus, Ohio V Motor 4a Veo, 2N2222 1N400 ab} * Vee ot _> B ox c oF [> te x otor D3 (>? ar ea > com lec iu Use TIP 110 part for Qt - Q4 if motor needs several amps. I—f—> com Figure 13-4, Using Transistors for Stepper Motor Driver SECTION 13.2: 8051 INTERFACING TO THE KEYBOARD Keyboards and LCDs are the most widely used input/output devices of the 8051, and a basic understanding of them is essential. In this section, we first dis- cuss keyboard fundamentals, along with key press and key detection mechanisms. ‘Then we show how a keyboard is interfaced to an 8051 interfacing the keyboard to the 8051 At the lowest level, keyboards are organized in a matrix of rows and columns. The CPU accesses both rows and columns through ports; therefore, with two 8-bit ports, an § x 8 matrix of keys can be connected to a microprocessor. When a key is pressed, a row and a column make a contact; otherwise, there is.n0 connection between rows and columns. In IBM PC keyboards, ¢ single microcon- troller (consisting of a microprocessor, RAM and EPROM, and several ports all on a single chip) takes care of hardware and software interfacing of the keyboard. In such systems, i( is the funetion of programs stored in the EPROM of the micro- controller to scan the keys continuously, identify which one has been activated, and present it to the motherboard. In this section we look at the mechanism by which the 8051 scans and identifies the key: CHAPTER 13: REAL-WORLD 261 Scanning and identifying the key Figure 13-5 shows a 4 x4 matrix connected to two ports. The rows are con- nected to an output port and the columns are connected to an input port. Ifno key has been pressed, reading the input port will yield 1s for all columns since they are all connected to high (Ve). Ifall the rows are grounded and a key is pressed, one of the columns will have 0 since the key pressed provides the path to ground. It is the funetion of the microconteolle to scan the keyboard continuously t0 detect and idemtfy the key pressed, How itis done is explained next, a x » pe |__ AW 7 J] 6 yy m1 ANY bla » % be -\\\ r fe ys Ys B8 a “Wy Port 1 (ou) 1 _+ Vee Ds o2 BT D0] pon ny Figure 13-5. Matrix Keyboard Connection to Ports Grounding rows and reading the columns To detect a pressed key, the microcontroller grounds all rows by providing 0 to the output latch, Hen i reads the columns. If the data read from the columns is D3 - DO = 1111, no key has been pressed and the process continues until a key press is detected. However, if one of the column bits fas a zero, this means that a key press has occurred. For example, if D3 - D0 = 1101, this means that a key in the D1 column has been pressed. Afler a Key press is detected, the microcontroller will go through the process of identifying the key. Starting with the top row, the microcontroller grounds it by providing a low to row DO only; then it reads the columns. If the data read is all 1, no key in that row is activated and the process is moved to the next row. It grounds the next row, reads the columns, and checks for any zero, This process continues until the row is identified. After identification of the row in which the key has been pressed, the next task is to find out which column the pressed key belongs to. This should be easy since the microcontroller knows at any time which row and column are being accessed. Look at Example 1333, a 262 } Example 13-3 From Figure 13-5, identify the row and column of the pressed key far each of the fol- towing, {a) D3 - DO= 1110 for the row, D3 ~ DO = 1011 for the column {b) D3 - DO= 1101 for the row, D3 - Dt 111 for the column Solution: (a) The row belongs to DO and the column belongs to D2; therefore, key number 2 was pressed. } (>) The row belongs to D1 and the column belongs to D3; therefore, key number 7 was pressed. Program 13-i is the 8051 Assembly language program for deteetion and identification of key activation. In this program, it is assumed that PI and P2 are initialized as output and input, respectively. Program {3-1 goes through the fol- fowing four major stages: 1. To make sure that the preceding key’ has been refeased, 0s are output to all rows at once, and the columns are read and checked repeatedly until all the columns are high, When all columns are found to be high, the program waits for a short amount of time before if goes to the next slage of wailing for a key 10 be pressed, To see if any key is pressed, the columns are scanned over and over jn an infi- } nite loop until one of them has & 0 on it. Rememmber that the output latches con- 5 nected to rows still have their initial zeros (provided in stage 1), making them grounded. Afier the key press detection, it waits 20 ms for the bounce and then scans the columns again, This serves two functions: (a) it ensures that the first | key press detection was not an erroneous one due to a spike noise, and (b) the 20-ms delay prevents the same key press from being interpreted as 2 multiple } key press. If afier the 20-ms defay the key is still pressed, it goes to the next } stage (o detect which row it belongs to; otherwise, it goes back into the loop to detect a real key press. | 3. To detect which row the key press belongs to, it grounds one row at a time, reading the columns each time. 1f it finds that all columns are high, this means that the key press cannot belong to that row; therefore, it grounds the next row and continues until it finds the row the key press belongs to, Upon finding the row that the key press belongs to, it sets up the starting address for the look-up table holding the scan codes (or the ASCII value) for that row’ and goes to the nex! stage te identify the key. 4, To identify the key press, it rotates the column bits, one bit at a time, into the carry flag and checks to see if it is low. Upon finding the zero, it puils out the ASCH) code for that key from the look-up table; othenwise, it increments the pointer to point to the next element of the look-up table. Figure 13-6 flow- chars this process. From Figure 13-5 the row and column can be used to identify the key, f f t t CHAPTER 13; REAL-WORLD INTERFACING IL 263 Ground all rows 5 Read all colurnns Read all columns: Any ke down, yes Wait for debounce { Read all columns 3 b Ground next row j Read all columns 0 | ot) Tow yes Find which key is pressed Get scan code from table (Rewm) Figure 13-6. lowehart for Program 13-1 264 While the key press detection is standard for aft keyboards, the process for determining which key is pressed varies. The look-up table method shown in Program 13-1 can be modified to work with any matrix up to 8 x 8. Figure 13-6 provides the flowchart for Program 13-1 for scanning and identifying the pressed key. ‘There ate IC chips such as National Semiconductor's MM74C923 that incorporate keyboard scanning and decoding all in one chip. Such chips use com- binations of counters and logic gates (no microcontroller) to implement the under- lying concepts presented in Prograin 13-1 ;Keyboard subroutine. This program sends the ASCIT code ;for pressed key to PO.1 #P1.0-P1.3 connected to rows P2.0-P2.3 connected to columns f HOV P2,#0FFHY ;make P2 an input port a] Kl: MOV B1, #0 iground all rows at once j Moy A, P2 read all col. (ensure keys open) ANL A,OOOOL111B Masked unused bits CONE A, #00001111B, 1 feheck til all keys released K2; ACALL OELAY #call 20 msec del MOV A, P2 gsee if any key is ANL A, #00001111B #mask unused bits CINE A, #000011118, OVER ikey pressed, await closure SIMe K2 icheck i1 key pressed lover: AcaLL DELAY iwait 20 msec debounce time MOY A,P2 icheck key closure | ANL A, #00001111B mask unused bits CONE A,#000071118,OVERL ;key pressed, find row SUMP K2 sif none, keep polling overt: Mov Pz, 4111111108 iground row 0 WOV A, P2 yread all columns ANL A, #000011 CWE A, #000011 mask unused bits pkey row 0, find the col. wov P1,#11111101B ground row 1 { MOV A, 22 read all columns ANL A, £00001121B mask unused bits CIN A,#00001311B,ROW_1 ;keyrow 1, find the col. Mov P1,411111011B ground row 2 MOV A, P2 read all columns ANL A, $99001111B jmask unused bits CONE A,#00001111B,ROW_2 ;key row 2, find the co. MOY P1,#11110111B ground row 3 | MOV A, P2 read all columns | ANL a, £000071118 smask anused bits . CONE A,#00002111B,ROW_3 ;keyrow 3, find the coi. LIMP K2 if none, false input, repeat] Program 13-1. (continued on next page) CHAPTER 13: REAL-WORLD INTERFACING IL 265 a ROw_O: MOV DPTR, #KCODEO jset DPTR=start of row 0 SUMP FIND find col. key belongs to kow_i: MOV DPTR, #KCODEL jset DPTR-start of row 1 SJMP FIND y£ind col. key belongs to ROW_2: MOV DPTR, #KCODE2 set DPTR=start of row 2 SgMP FIND MOV DPTR, #KCODE3 ;find col. key belongs to et DPTR=start of row 3 RRC A isee if any CY Dit low ONC MATCH ;if zero, get the ASCII code INC DPTR joint to next col, address SMP FIND skeep searching et A=0 (match is found) jet ASCII code from table MATCH: CLR A MOVC A, 8A+DPTR Nov P0,A splay pressed key KL P TABLE FOR EACH RON 300K Fora, 121,13" 7ROW 0 att 5T, 165,17! 7ROW 1 DB rr 7ROW 2 KCODE3: DB NOt, TDY, TEN, TEP ROW 3 END Program 13-1, continued from previons page) Review Questions 1. True or false. To see if any key is pressed, all rows are grounded. 2. If D3 - DO = Ott is the data read from the columns, which column does the pressed key belong to? 3. True or false. Key press detection and key identification require two different processes. 4. In Figure 13-5, if the row has D3 - DO = 1119 and the columns are D3 - DO = 1110, which key is pressed? 5. True or false. To identify the pressed key, one row at a time is grounded. SECTION 13.3: INTERFACING A DAC TO THE 8051 This seetion will show how to interface a DAC (di er) to the 8051, Then we demonstrate how to generate a using the DAC. Digital-to-analog (DAC) converter jtal-to-analog convert- we wave on the scope ‘The digital-to-analog converter (DAC) is a device widely used 10 convert digital pulses to analog signals. In this section we discuss the basics of interfacing a DAC to the 805 Recall from your digital electronics book the two methods of making the DAC: binary weighted and R/2R ladder. The vast majority of integrated cireuit 266 APPENDIX F ASCII CODES T ctri|pec|Hex)ch|Code| |Dec|Hex|/ch) |Dec|Hex|ch| |Dec|Hex|ch -e | o | oa] jwun 32| 20 64] 40/ @| | 96/ 60 sa | 1 | 01] o}soH 33{ 21{ :| | 65| 43) a} | 97] 61] a -B | 2 { 02| e|stx 3a] 22| "| | 66] 42\ B| | 98] 62] b ec | 3 | 03| vlerx 35) 23| 4) | 67| 43) c] | 99) 63) ¢ sp | 4 | 04) ¢)EOT 36| 24| s| | 68| 44] Db] 100] 64} a *E | 5 | 05| 4| ENO 37| 25] | } 69) 45) B/ j101| 65] e *F | 6 | 06] alack 38) 26| | | 70| 46] F| |102| 66| £ sc | 7 | 07| ©|BEL 39] 27] *| | 71] 47) | {103| 67) g “x | 8 | 08| o|Bs 40} 28) (| | 72] 48] {| |104] 68{ h *r | 9} 09| ojnr 4i| 29! )} | 73) 49) t| |105| 69) £ -3 | 10] oa| w|LF 42| 2a *| | 4] 4a/ a| (106) say 3 “K | 11) 0B] ofvr 43{ 2B[ +{ | 75| 4B] K) j107) 6B) k *L | 12) oc| o|FF 44] 2c) ,| | 76] acl L] J108) 6c om | 23| op] 2{cr 45| 2b) “| | 77] ap] m} [109] 6D) nm sn | 14] 0£| 9}so 46| 28] .| | 78] ae) N| ja10) 6E! no so | 15] oF] lst 47| 2B] /) | 79} 4F/ of [112] 6F] 0 -p | 16] 10} »|DLE 48} 30{ 0) | 80) 50] P| 212] 70] p “a. { a7{ aa] «|pcr a9} 32) 2] | 81] 51) Q| J223} 711 4 rR | 18] 12) +|pc2 50) 32) 2] | 82} 52] R] 124) 72] r -s | 19] 13] ypc3 si} 33| 3| | 83] 53| s| |a35] 73) s -t | 20] 14] g}pca 52} 34} 4) | sg) 54] Tt] Jar6| 74] t su | 21) 15] §}NaK 53/ 35] 5| | 85| 55) u} |217| 75) u sv | 22) 16| .|syn 54] 36) 6| | 86] 56/ v| |a2e| 756] v ow | 23/ 17) ¢/ETB 55| 37| 7| | a7 57| w| |aa9] 77| w “x | 24] 18| 1/cAN 56| 38] 8| | ga] $3} x| |120/ 78) x ~y | 25) 19] 4/Em 57) 39} 9} | a3) 59| ¥) |12i| 79] y -z | 26] 1a! +/}suB 58/ 3A 90| 5a} 2| |122| 7a| 2 “[ | 27{ 1B] | | 94{ sei *| |126| 7E| ~ o_ | 32} 3F} rfus 63| 3F| 2] | 95; 5F| _} |127) 7F] 0 — APPENDIX F: ASCH CODES 401 \

You might also like