Today we are talking about interfacing a 4×3 keypad with 8051 microcontrollers in assembly language programming for the 8051 microcontrollers. The 8051 microcontroller is one of the simplest to use the 4×3 keypad or any other matrix keypad like 4×3 4×4 or any other matrix keypad. By calling the matrix keypad what I actually mean is the keypad which is made in terms of matrix-based switches. So every key on that keypad is basically the combination of rows and collumns. So to use that keypad with a 4×3 matrix, all we need to do is to use the 7 IO pins of the microcontroller and we are done.

If you like to do the traditional way of interfacing the 4×3 = 12 keys with any of the microcontrollers you need one IO Pin per switch so 12 IO pins will be used to read 12 keys. By doing the previous work and the learning of the microcontrollers you may already be aware that in Embedded systems the iO pins are very costly due to the low pin counts on most of the microcontrollers or the microprocessors. And sometimes it also becomes very costly and sometimes the requirement of the design doesn’t even allow to add external IO expanders ICs with the microcontroller or anything external shift registers as we did in one of our previous Arduino based 7 segment interfacing with the shift registers.

Here is the full assembly language code of the matrix 4×3 keypad interfacing with the 8051 microcontrollers.

;================================================
;
;	THIS PROGRAM WILL READ 4X3 KEYPAD AND 
;	DISPLAY OUTPUT ON P0 AS BINARY OUTPUT
;
;=================================================


KEYPAD_PORT	EQU	P1
LEDS_PORT	EQU	P0

;================================================	

	ORG		 00H 

;===============================================
KEYSCAN:	
	MOV	KEYPAD_PORT,#00001111B
	MOV	A,KEYPAD_PORT
	CJNE	A,#00001111B,KEYSCAN

K2:	
	ACALL	DELAY1		;CHECK IF A KEY IS PRESSED
	MOV	A,KEYPAD_PORT
	CJNE	A,#00001111B,OVER
	SJMP	K2

OVER:	
	ACALL	DELAY1
	MOV	A,KEYPAD_PORT
	CJNE	A,#00001111B,OVER1
	SJMP	K2

OVER1:	
	MOV	KEYPAD_PORT,#11101111B	;TO CHECK IF PRESSED KEY IS FROM ROW0
	MOV	A,KEYPAD_PORT
	CJNE	A,#11101111B,ROW_0

	MOV	KEYPAD_PORT,#11011111B
	MOV	A,KEYPAD_PORT
	CJNE	A,#11011111B,ROW_1

	MOV	KEYPAD_PORT,#10111111B
	MOV	A,KEYPAD_PORT
	CJNE	A,#10111111B,ROW_2

	MOV	KEYPAD_PORT,#01111111B
	MOV	A,KEYPAD_PORT
	CJNE	A,#01111111B,ROW_3
	SJMP	K2
	
ROW_0:	
	MOV	DPTR,#KCODE0
	SJMP	FIND

ROW_1:	
	MOV	DPTR,#KCODE1
	SJMP	FIND

ROW_2:	
	MOV	DPTR,#KCODE2
	SJMP	FIND

ROW_3:	
	MOV	DPTR,#KCODE3
	
FIND:	
	RRC	A
	JNC	MATCH
	INC	DPTR
	SJMP	FIND

MATCH:	
	CLR	A
	MOVC	A,@A+DPTR
	CPL	A
	MOV	LEDS_PORT,A
	SJMP	KEYSCAN

;======== DELAYS ================================

DELAY1:	
	MOV	R7,#255
	DJNZ	R7,$
	RET	
	
;======== CODES =================================
				
KCODE0:	DB	0FFH, 01H, 02H, 03H		
KCODE1:	DB	0FFH, 04H, 05H, 06H
KCODE2:	DB	0FFH, 07H, 08H, 09H
KCODE3:	DB	0FFH, 0AH, 00H, 0BH	
	
END
	
	Code language: PHP (php)

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.