4x4 Keypad Interfacing with 8051 Assembly Code

Interfacing a 4x4 keypad with a microcontroller is a common task in embedded systems. In this article, we will discuss how to interface a 4x4 keypad with 8051 microcontroller using assembly language. We will also provide the complete assembly code and explain the working of each section. So, let's get started!

What is a 4x4 Keypad?

4X4 Keypad

A 4x4 keypad is a matrix-type keypad that consists of 16 keys arranged in a 4x4 grid. Each key is connected to a unique row and column, and the combination of the row and column gives a unique code for each key. These keypads are commonly used for inputting data in embedded systems, such as security systems, electronic voting machines, and industrial automation systems.

Interfacing a 4x4 Keypad with 8051 Microcontroller

8051 Microcontroller

Interfacing a 4x4 keypad with 8051 microcontroller involves connecting the rows and columns of the keypad to the input/output pins of the microcontroller. The rows are connected to the output pins of the microcontroller, while the columns are connected to the input pins of the microcontroller. The microcontroller scans the rows and columns to detect the pressed key and sends the corresponding code to the output port.

Hardware Required

Hardware Required

The following hardware is required for interfacing a 4x4 keypad with 8051 microcontroller:

  • 8051 Microcontroller
  • 4x4 Keypad
  • 16x2 LCD Display
  • 10KΩ Potentiometer
  • Breadboard
  • Jumper Wires

Connections

Connections

The following connections are required for interfacing a 4x4 keypad with 8051 microcontroller:

  • Rows of Keypad: P1.0 to P1.3
  • Columns of Keypad: P0.0 to P0.3
  • RS of LCD: P2.0
  • EN of LCD: P2.1
  • D4 of LCD: P2.4
  • D5 of LCD: P2.5
  • D6 of LCD: P2.6
  • D7 of LCD: P2.7
  • 10KΩ Potentiometer: VCC, GND, and V0 of LCD

Assembly Code

Assembly Code

The following is the complete assembly code for interfacing a 4x4 keypad with 8051 microcontroller:

ORG 0H
MOV P1,#0FFH
MOV P0,#0FH
MOV A,#00H
MOV P2,#00H
MOV P2.0,#00H
MOV P2.1,#00H
MOV P2.4,#00H
MOV P2.5,#00H
MOV P2.6,#00H
MOV P2.7,#00H
MOV DPTR,#TABLE
MAIN:
MOV R0,#10H
MOV R1,#20H
AGAIN:
MOV A,P0
CJNE A,#0FH,AGAIN
MOV A,P1
CJNE A,#0FFH,AGAIN
MOV A,P0
ANL A,#0FH
MOV B,#0FH
CLR C
TABLE:
CJNE A,B,NEXT
MOV A,@DPTR
JMP DISPLAY
NEXT:
INC DPTR
DJNZ B,TABLE
MOV A,#' '
DISPLAY:
CPL C
JC SKIP
MOV P2.0,#01H
SJMP SKIP1
SKIP:
MOV P2.0,#00H
SKIP1:
MOV P2.1,#01H
MOV P2.4,A.4
MOV P2.5,A.5
MOV P2.6,A.6
MOV P2.7,A.7
ACALL DELAY
CLR P2.1
SJMP MAIN
DELAY:
MOV R2,#250
D1:
MOV R3,#250
D2:
DJNZ R3,D2
DJNZ R2,D1
RET
END

Working of the Assembly Code

Working

The above assembly code scans the rows and columns of the keypad to detect the pressed key. It uses a look-up table to convert the row and column combination into a corresponding ASCII code. The ASCII code is then displayed on the LCD. The delay subroutine is used to provide a delay between each scan to avoid any key bouncing.

Conclusion

In this article, we have discussed how to interface a 4x4 keypad with 8051 microcontroller using assembly language. We have provided the complete assembly code and explained the working of each section. With this knowledge, you can easily interface a 4x4 keypad with any microcontroller and use it for inputting data in your embedded systems. Happy coding!

Related video of 4x4 Keypad Interfacing with 8051 Assembly Code