Saturday, 6 December 2014

3x3 Matrix Keypad ALP Program

;$INCLUDE(KEYPADHEAD.ASM)
;$include(reg51.h)
$include(DELAY100.ASM)
ROW0 BIT P3.0
ROW1 BIT P3.1
ROW2 BIT P3.2

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

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

INIT_ROWS:
CLR ROW0
CLR ROW1
CLR ROW2
RET

INIT_COL:
SETB COL0
SETB COL1
SETB COL2
RET

COLSCAN:
SETB C
ANL C,COL0
ANL C,COL1
ANL C,COL2
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
ACALL COLSCAN
JNC ROW_0

SETB ROW0
CLR ROW1
SETB ROW2
ACALL COLSCAN
JNC ROW_1

SETB ROW0
SETB ROW1
CLR ROW2
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
RET
;SJMP MAIN
;END
CSEG AT 0
MAIN:
ACALL KEYSCAN
;MOV P1,A
;IS_KEY_PRESSED:
;ACALL COLSCAN
;JNC IS_KEY_PRESSED
SJMP MAIN
END