Saturday, 6 December 2014

Write an ALP program for 4x4 matrix keypad

ROW0 BIT P3.0
ROW1 BIT P3.1
ROW2 BIT P3.2
ROW3 BIT P3.3

COL0 BIT P3.4
COL1 BIT P3.5
COL2 BIT P3.6
COL3 BIT P3.7

KEYPAD_LUT SEGMENT CODE
RSEG KEYPAD_LUT
KCODE0:	DB 1,2,3,4
KCODE1:	DB 5,6,7,8
KCODE2:	DB 9,+,-,*

INIT_ROWS:
CLR ROW0
CLR ROW1
CLR ROW2
clr row3
RET

INIT_COL:
SETB COL0
SETB COL1
SETB COL2
SETB COL3
RET

COLSCAN:
SETB C
ANL C,COL0
ANL C,COL1
ANL C,COL2
ANL C,COL3
RET

CSEG AT 0
MAIN:
KEYSCAN:
ACALL INIT_COL
ACALL INIT_ROWS
SCAN_COLS:
ACALL COLSCAN
JNC KEYPRESSED
SJMP SCAN_COLS

KEYPRESSED:
CLR ROW0
SETB ROW1
SETB ROW2
SETB ROW3
ACALL COLSCAN
JNC ROW_0

SETB ROW0
CLR ROW1
SETB ROW2
SETB ROW3
ACALL COLSCAN
JNC ROW_1

SETB ROW0
SETB ROW1
CLR ROW2
SETB ROW3
ACALL COLSCAN
JNC ROW_2

SETB ROW0
SETB ROW1
SETB ROW2
SETB ROW3
ACALL COLSCAN
JNC ROW_2


ROW_0:
MOV DPTR,#KCODE0
SJMP COLCHECK


ROW_1:
MOV DPTR,#KCODE1
SJMP COLCHECK


ROW_2:
MOV DPTR,#KCODE2
SJMP COLCHECK

COLCHECK:
MOV R3,#0
JNB COL0,MAPKEY
INC R3
JNB COL1,MAPKEY
INC R3
JNB COL2,MAPKEY

MAPKEY:
MOV A,R3
MOVC A,@A+DPTR
MOV P2,A

IS_KEY_PRESSED:
ACALL COLSCAN
JNC IS_KEY_PRESSED
SJMP MAIN
END