Professional Documents
Culture Documents
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.
Group Members:
Lab Work
Lab #10
Flow Chart
Group Members:
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 ;This program reads data from a hex keypad ;and sends it to port2 ORG 0000H MAIN: CALL IN_HEX MOV P2,A SJMP MAIN
;get hex code from keypad ;send this to port2 ;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
;IN_HEX - input hex code from keypad with debouncing ;for key press and key release (50 repeats for each) IN_HEX: MOV R3,#50 ;debounce count BACK: CALL GET_KEY ;key pressed? JNC IN_HEX ;no: check again DJNZ R3,BACK ;yes: repeat 50 times PUSH ACC ;save hex code BACK2: MOV R3,#50 ;wait for key release BACK3: CALL GET_KEY ;key pressed? JC BACK2 ;yes: keep checking DJNZ R3,BACK3 ;no: repeat 50 times POP ACC ;recover hex code RET ;return ;GET_KEY - get keypad status ; - returns C = 0 if no key is pressed ; - returns C = 1 and hex code in Accumulator if GET_KEY: MOV A,#0FEH ;start with column 0 by grounding it MOV R6,#4 ;use R6 as column counter TEST: MOV P1,A ;activate (ground) current column MOV R7,A ;save ACC (accumulator) MOV A,P1 ;read back port ANL A,#0F0H ;isolate rows by masking columns CJNE A,#0F0H,KEY_HIT ;any row active? MOV A,R7 ;no: move to RL A ;ground next column DJNZ R6,TEST CLR C ;no key pressed SJMP EXIT ;return with C = 0 KEY_HIT: MOV R7,A ;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:
Lab Work
MOV A,#4 CLR C SUBB A,R6 MOV R6,A MOV A,R7 SWAP A MOV R5,#4 AGAIN: RRC A JNC DONE INC R6 INC R6 INC R6 INC R6 DJNZ R5,AGAIN DONE: SETB C MOV A,R6 EXIT: RET END
Lab #10
;prepare to calculate column ;clear carry flag ;column number = 4 - R6 ;save result in R6 ;restore rows scan result ;put this in low nibble ;use R5 as counter ;rotate-right in carry ;done when C = 0 ;add 4 ;until the row is detected ;R6 finally ;holds the hexcode of pressed ;repeat until all rows checked ;C = 1 (key press confirmed!) ;save key hex code in A
Group Members: