7b. 8x3 keypad interface, display the row and column of the key pressed

vtu mp 7b
Q) Scan a 8 x 3 keypad for key closure and to store the code of the key pressed in a memory location or display on screen. Also display row and column numbers of the key pressed.

This program sense a key being pressed on the 8x3 keypad interface and display the row and column number of the button pressed.

Algorithm:
Step 1: Initialize the control word 90H and store it in the control register.
Step 2: Initialize the row number and column number to 1 of the keyboard interface.
     Initialize the number of columns to 3 and number of rows to 8.
Step 3: Initialize the pointer to point to the keyboard layout
Step 4: Rotate the contents AL to 1 bit to enable the first column
Step 5: Check whether the key pressed is in the present column.
            If, key pressed is not present in the column
            Increment the column number to point to next column
            Add 8 to the pointer to point to next row in the keyboard layout.
            Else
            If the key pressed is in the present column then Check in which row of   that column the key has been pressed
        Increment the row number to point to next row
        Increment the pointer
         Else
         Call some delay before display. Display the row number, column number and the key pressed on the screen.
Step 6: Stop.

CODE:


Explanation: 
.MODEL SMALL
DISP_MSG MACRO M
    MOV AH,09H
    LEA DX,M
    INT 21H
ENDM

One data and one code segment. DISP_MSG macro displays a message using LEA, 09H and INT 21H.


.DATA
    PA EQU 0e800H
    PB EQU 0e801H
    PC EQU 0e802h
    CR EQU 0e803h
    M1 DB 10,13,"ENTERED KEY IS:$"
    M2 DB 10,13,"ROW NUMBER IS:$"
    M3 DB 10,13,"COL NUMBER IS:$"
    ;M4 DB 10,13,"PRESS 'C' FROM SYSTEM KEYBOARD TO CONTINUE:$"
    M5 DB 10,13,"PRESS ANY KEY FROM KB_INTERFACE$"
    ROW DB ?
    COL DB ?
    CW DB 90H

Data segment:
Contains port addresses, control word, messages to be displayed, and ROW and COL values set to nothing initially.
CW in this program is 90H. (exceptional case, port A is the input).

.CODE
    MOV AX,@DATA
    MOV DS,AX
    MOV AL,CW
    MOV DX,CR
    OUT DX,AL
    DISP_MSG M5

Code segment:
Initialize data segment and activate the keyboard interface.(first 5 lines).
Display M5 message using MACRO.


START:    MOV AL,80H
    MOV ROW,1   
    MOV COL,1   
    MOV CH,0   
    MOV BL,3   

START is a label.
Move 80H, bit pattern to enable each rows. (left rotating this number and OUT with Port C enables the corresponding row)
Set COL, ROW initially to 1.
CH is the key code of the character pressed. Initially 0.
BL is the row counter set to 3. (total 3 rows in the keypad interface).


NXTROW:    ROL AL,1
    MOV BH,AL
    MOV DX,PC
    OUT DX,AL   
    MOV CL,8   
    MOV DX,PA
    IN AL,DX

NXTROW is a label.
ROL AL,1 will rotate the bit pattern by 1 bit to left and this will give you row pattern of 1 st row. OUT it through port C to enable first row.
CL is set to 8, this is column counter(each column has 8 keys).
Get the row’s 8 bit value  through Port A using IN command. Now the pattern is in AL.


NXTCOL:    ROR AL,1
    JC DISPLAY
    INC CH
    INC COL
    DEC CL
    JNZ NXTCOL
    MOV COL,1
    INC ROW
    MOV AL,BH
    DEC BL
    JNZ NXTROW

NXTCOL is the label.
Rotate the AL by 1 bit to right and check carry flag (carry flag contains right most bit after rotation to right). If carry is 1. I.e., TRUE, This means key was pressed. Go to DISPLAY and display the result.
Otherwise, Increment COL to check the next column. Decrease the CL (column counter). If not zero, check next column by jumping to NXT_COL label. Also increment the CH value (the key code value).
If 8 checks are made in the first row and no key was pressed, you need to go to the next row by incrementing ROW value, decrementing row counter BL and resetting COL to 1 in the new row. Do this till Row counter becomes 0. (that is 3 times).

START1:    JMP START

Repeat this instructions till user presses a character.

DISPLAY:DISP_MSG M1
MOV DH,CH
MOV DL,CH
AND DL,0F0H
MOV CL,4
SHR DL,CL
CMP DL,0AH
JB DIGIT
ADD DL,07H
DIGIT:
ADD DL,30H
CALL DISP_CHAR
MOV DL,DH
AND DL,0FH
CMP DL,0AH
JB DIGIT1
ADD DL,07H
DIGIT1:
ADD DL,30H
CALL DISP_CHAR

If a key was pressed, we jumped to label DISPLAY in the above code remember?
Here display M1 message using DISP_MSG macro.
Move CH (key code of the key pressed) to DL also take a backup in DH.
For each digit in DH, Compare with 0AH. If less, directly add 30H. Otherwise add 07H and then 30H. (nothing but conversion to ASCII).
Then call DISP_CHAR to display the entered key(called two times to display two characters  of the digit).
[Note: Extraction of two digits from DH is similar to this program]


ADD ROW,30H
    ADD COL,30H
     DISP_MSG M2
    MOV DL,ROW
    CALL DISP_CHAR
    DISP_MSG M3
    MOV DL,COL
    CALL DISP_CHAR
    MOV AH,4CH
    INT 21H


Now we need to display the row and column pressed. Since these are always less than 10, directly add 30H without any comparison with 0AH. Move them to DL and call DISP_CHAR to display, of course along with corresponding messages which are displayed using DISP_MSG macro.
Finally, terminate using 4CH and INT 21H.

DISP_CHAR PROC
    MOV AH,2
    INT 21H
    RET
DISP_CHAR ENDP
END

DISP_CHAR is procedure to display using 02H service with INT 21H.
And here, the code ends.

OUTPUT:
Seen in the keypad interface.

He is a simple passionate tech freak who himself is an engineering student at Canara Engineering college. He likes App Development, Web designing, Blogging, Youtubing, Debugging and also is a CodeGeek!

Sharing is sexy!

Related Articles

Share your views about this article!