Embedded Systems – IE403

Lab Work

Lab #10

Objective
To read data from hex keypad and send its hexcode on another port.

Theory
Keypads Keypads 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 8 x 8 matrix of keys can be connected to micrporcessor. When a key is pressed, a row and a column make a contact; otherwise, there is no connection between rows and columns. Scanning and Identifying the Key A 4 x 4 matrix connected to a port is shown in figure 10.1. The columns are made as output and rows are made as inputs to be scanned. If no key is pressed, reading the input will yield 1s for all rows. If all the columns are grounded and a key is pressed, one of the rows will have 0. It is the function of the microcontroller to scan the keyboard continuously to detect and identify the key pressed. Grounding Columns and Reading Rows To detect a pressed key, the micrcontroller grounds all columns by providing 0 to the output latch, then it reads the rows. If the data read from the rows D4 – D7 = 1111, no key has been pressed and process continues until a key press is detected. However, if one of the row bits has a zero, this means that key press has occurred. For example if D4 – D7 = 1101, this means that a key in the D1 row has been pressed. After a key press is detected, the micrcontroller will go through the process of identifying the key. Starting with the left column, the micrcontroller grounds it by providing a low to column D0 only, then it reads the rows. If the data read is all 1s, no key in that column is activated and process is move the next column D1. It grounds the next column, reads the rows, and checks for any zero. This process continues until the column is identified. After identification of the column, the next task is to find out which row the pressed key belongs to. This should be easy since the microcontroller knows at any time which row and column are being accessed.

Figure 10.1: Interface to hex keypad

Group Members:

Ali Asad (1936)

Safdar Abbasi (1926)

Embedded Systems – IE403 Lab Work Lab #10 Flow Chart Group Members: Ali Asad (1936) Safdar Abbasi (1926) .

key pressed? JC BACK2 .#0F0H .get keypad status .no: repeat 50 times POP ACC .return .for key press and key release (50 repeats for each) IN_HEX: MOV R3.#50 .activate (ground) current column MOV R7.R7 .Embedded Systems – IE403 Lab Work Lab #10 Source Code LOC OBJ LINE 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 SOURCE .A SJMP MAIN 0000 0000 0000 1106 0002 F5A0 0004 80FA .#0FEH .yes: repeat 50 times PUSH ACC .no: check again DJNZ R3.isolate rows by masking columns CJNE A.KEY_HIT .#50 .get hex code from keypad .TEST CLR C .wait for key release BACK3: CALL GET_KEY .no: move to RL A .input hex code from keypad with debouncing .start with column 0 by grounding it MOV R6.#0F0H.#4 .GET_KEY .BACK3 . .read back port ANL A.any row active? MOV A.no key pressed SJMP EXIT .and sends it to port2 ORG 0000H MAIN: CALL IN_HEX MOV P2.ground next column DJNZ R6.returns C = 1 and hex code in Accumulator if GET_KEY: MOV A.save hex code BACK2: MOV R3.save in R7 pressed 001B 001B 74FE 001D 7E04 001F 001F F590 0021 FF 0022 E590 0024 54F0 0026 B4F007 0029 EF 002A 23 002B DEF2 002D C3 002E 8015 0030 0030 FF Group Members: Ali Asad (1936) Safdar Abbasi (1926) .save ACC (accumulator) MOV A.IN_HEX .A .returns C = 0 if no key is pressed . .recover hex code RET .A .BACK .send this to port2 .P1 .repeat this forever 0006 0006 0008 0008 000A 000C 000E 0010 0010 0012 0012 0014 0016 0018 001A 7B32 111B 50FA DBFA C0E0 7B32 111B 40FA DBFA D0E0 22 .A .debounce count BACK: CALL GET_KEY .This program reads data from a hex keypad .yes: keep checking DJNZ R3.return with C = 0 KEY_HIT: MOV R7.use R6 as column counter TEST: MOV P1.key pressed? JNC IN_HEX .

repeat until all rows checked .holds the hexcode of pressed .R6 EXIT: RET END Lab #10 .A MOV A.put this in low nibble .R6 .save result in R6 .#4 CLR C SUBB A.save key hex code in A Code 10.clear carry flag .R7 SWAP A MOV R5.column number = 4 .prepare to calculate column .Embedded Systems – IE403 0031 0033 0034 0035 0036 0037 0038 003A 003A 003B 003D 003E 003F 0040 key 0041 0043 0043 0044 0045 0045 7404 C3 9E FE EF C4 7D04 13 5006 0E 0E 0E 0E DDF7 D3 EE 22 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 Lab Work MOV A.restore rows scan result .R6 finally .use R5 as counter .rotate-right in carry .R6 MOV R6.done when C = 0 .C = 1 (key press confirmed!) .#4 AGAIN: RRC A JNC DONE INC R6 INC R6 INC R6 INC R6 DJNZ R5.AGAIN DONE: SETB C MOV A.1: ASM Listing for hex keypad program Group Members: Ali Asad (1936) Safdar Abbasi (1926) .add 4 .until the row is detected .

Sign up to vote on this title
UsefulNot useful