Hex Keypad Interfacing with 8051:
The Assembly Code:
ORG 0000H ;set origin
MOV P2, #0FFH ;select p2 as output port
LJMP MAIN ;jump to main program
DELAY_1SEC: ;to give delay of 1second using timer0 in mode 1
MOV TMOD, #01H
MOV R1, #14
XY: MOV TH0, #00H
MOV TL0, #00H
SETB TR0
JNB TF0, $
CLR TF0
CLR TR0
DJNZ R1, XY
RET
DELAY: ;to give lcd some time to write
MOV R1, #05H
S2: MOV R0, #0FFH
S1: DJNZ R0, S1
DJNZ R1, S2
RET
WRITE_CMD: ;command subroutine to write command to lcd
CLR P3.7 ;register select bit cleared to choose command register
MOV P1, A ;port 1 acts as input to lcd
SETB P3.6 ;enable pin high to low for write operation
CLR P3.6 ;
LCALL DELAY ;give lcd some time delay
RET ;return to main program
WRITE_CHR: ;data subroutine to write data to lcd
SETB P3.7 ;register select bit set high to choose data register
MOV P1, A ;port 1 acts as input to lcd
SETB P3.6 ;enable pin high to low for write operation
CLR P3.6 ;
LCALL DELAY ;give lcd some time delay
RET ;return to main program
INI_LCD: ;initialise lcd
MOV A, #38H ;two lines 5x8 matrix
LCALL WRITE_CMD ;call command subroutine
MOV A, #0CH ;display on, cursor off
LCALL WRITE_CMD ;call command subroutine
MOV A, 06H ;increment cursor(shift cursor to right)
LCALL WRITE_CMD ;call command subroutine
RET ;return to main program
B1: ;subroutine to type 'e'
MOV A, #'E'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD1
B2: ;subroutine to type '1'
MOV A, #'1'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD2
B3: ;subroutine to type '4'
MOV A, #'4'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD3
B4: ;subroutine to type '7'
MOV A, #'7'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD4
B5: ;subroutine to type '0'
MOV A, #'0'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD5
B6: ;subroutine to type '2'
MOV A, #'2'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD6
B7: ;subroutine to type '5'
MOV A, #'5'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD7
B8: ;subroutine to type '8'
MOV A, #'8'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD8
B9: ;subroutine to type 'f'
MOV A, #'F'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD9
B10: ;subroutine to type '3'
MOV A, #'3'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD10
B11: ;subroutine to type '6'
MOV A, #'6'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD11
B12: ;subroutine to type '9'
MOV A, #'9'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD12
B13: ;subroutine to type 'd'
MOV A, #'D'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD13
B14: ;subroutine to type 'c'
MOV A, #'C'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD14
B15: ;subroutine to type 'b'
MOV A, #'B'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD15
B16: ;subroutine to type 'a'
MOV A, #'A'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD16
MAIN: ;main program
LCALL INI_LCD ;call initialise lcd
KEYPAD: CLR P2.0 ;clear 1st column
JNB P2.4, B1_1 ;check if 'e' pressed
KEYPAD1: JNB P2.5, B2_1 ;check if '1' pressed
KEYPAD2: JNB P2.6, B3_1 ;check if '4' pressed
KEYPAD3: JNB P2.7, B4_1 ;check if '7' pressed
KEYPAD4: SETB P2.0 ;if no key is pressed in column 1 rhen set 1st column
CLR P2.1 ;clear 2nd column
JNB P2.4, B5_1 ;check if '0' pressed
KEYPAD5: JNB P2.5, B6_1 ;check if '2' pressed
KEYPAD6: JNB P2.6, B7_1 ;check if '5' pressed
KEYPAD7: JNB P2.7, B8_1 ;check if '8' pressed
KEYPAD8: SETB P2.1 ;if no key is pressed in column 2 rhen set 2nd column
CLR P2.2 ;clear 3rd column
JNB P2.4, B9_1 ;check if 'f' pressed
KEYPAD9: JNB P2.5, B10_1 ;check if '3' pressed
KEYPAD10: JNB P2.6, B11_1 ;check if '6' pressed
KEYPAD11: JNB P2.7, B12_1 ;check if '9' pressed
KEYPAD12: SETB P2.2 ;if no key is pressed in column 3 rhen set 3rd column
CLR P2.3 ;clear 4th column
JNB P2.4, B13_1 ;check if 'd' pressed
KEYPAD13: JNB P2.5, B14_1 ;check if 'c' pressed
KEYPAD14: JNB P2.6, B15_1 ;check if 'b' pressed
KEYPAD15: JNB P2.7, B16_1 ;check if 'a' pressed
KEYPAD16: SETB P2.3 ;if no key is pressed in column 4 rhen set 4th column
LJMP KEYPAD
STOP: SJMP STOP ;
B1_1: ;subroutine to call b1 to display 'e'
ACALL DEBOUNCE_DELAY
JNB P2.4, KEYPAD1
LJMP B1
B2_1: ;subroutine to call b2 to display '1'
ACALL DEBOUNCE_DELAY
JNB P2.5, KEYPAD2
LJMP B2
B3_1: ;subroutine to call b3 to display '4'
ACALL DEBOUNCE_DELAY
JNB P2.6, KEYPAD3
LJMP B3
B4_1: ;subroutine to call b4 to display '7'
ACALL DEBOUNCE_DELAY
JNB P2.7, KEYPAD4
LJMP B4
B5_1: ;subroutine to call b5 to display '0'
ACALL DEBOUNCE_DELAY
JNB P2.4, KEYPAD5
LJMP B5
B6_1: ;subroutine to call b6 to display '2'
ACALL DEBOUNCE_DELAY
JNB P2.5, KEYPAD6
LJMP B6
B7_1: ;subroutine to call b7 to display '5'
ACALL DEBOUNCE_DELAY
JNB P2.6, KEYPAD7
LJMP B7
B8_1: ;subroutine to call b8 to display '8'
ACALL DEBOUNCE_DELAY
JNB P2.7, KEYPAD8
LJMP B8
B9_1: ;subroutine to call b9 to display 'f'
ACALL DEBOUNCE_DELAY
JNB P2.4, KEYPAD9
LJMP B9
B10_1: ;subroutine to call b10 to display '3'
ACALL DEBOUNCE_DELAY
JNB P2.5, KEYPAD10
LJMP B10
B11_1: ;subroutine to call b11 to display '6'
ACALL DEBOUNCE_DELAY
JNB P2.6, KEYPAD11
LJMP B11
B12_1: ;subroutine to call b12 to display '9'
ACALL DEBOUNCE_DELAY
JNB P2.7, KEYPAD12
LJMP B12
B13_1: ;subroutine to call b13 to display 'd'
ACALL DEBOUNCE_DELAY
JNB P2.4, KEYPAD13
LJMP B13
B14_1: ;subroutine to call b14 to display 'c'
ACALL DEBOUNCE_DELAY
JNB P2.5, KEYPAD14
LJMP B14
B15_1: ;subroutine to call b15 to display 'b'
ACALL DEBOUNCE_DELAY
JNB P2.6, KEYPAD15
LJMP B15
B16_1: ;subroutine to call b16 to display 'a'
ACALL DEBOUNCE_DELAY
JNB P2.7, LOOP
LJMP B16
LOOP: LJMP KEYPAD16
DEBOUNCE_DELAY: ; a key debouncing delay algorithm
MOV TMOD, #10H
MOV TH1, #00H
MOV TL1, #00H
MOV R2, #4
AGAIN: SETB TR1
WAIT: JNB TF1, WAIT
CLR TR1
CLR TF1
DJNZ R2, AGAIN
RET
END ;end of main program