Interfacing 4×4 Matrix Keypad with 8051 Microcontroller

Keypads: Input Devices for Embedded Projects

Keypads are widely used input devices in various electronics and embedded projects. They are used to take inputs in the form of numbers and alphabets, and feed the same into the system for further processing. In this tutorial, we will interface a 4×4 matrix keypad with the 8051 microcontroller.

Understanding the 4×4 Matrix Keypad

Before interfacing the keypad with the microcontroller, first, we need to understand how it works. A matrix keypad consists of a set of push buttons that are interconnected. In our case, using a 4×4 matrix keypad, there are 4 push buttons in each of four rows. The terminals of the push buttons are connected according to the diagram (assumed to be present in the original context). In the first row, one terminal of all 4 push buttons is connected together, and the other terminals of the 4 push buttons represent each of the 4 columns. The same configuration applies to each row. Thus, we get 8 terminals to connect with a microcontroller.

Note: First, we need to interface an LCD module to display the data fed through the KEYPAD. Please review the article on “LCD Interfacing with 8051 Microcontroller” before interfacing the KEYPAD.

Keypad Interfacing with 8051

As shown in the circuit diagram (assumed), to interface the Keypad, we need to connect the 8 terminals of the keypad to any port (8 pins) of the microcontroller. For instance, we have connected the keypad terminals to Port 1 of the 8051.

Whenever any button is pressed, we need to determine the location of the button, meaning the corresponding Row and Column number. Once we get the location of the button, we can print the corresponding character.

Determining the Pressed Button Location

Now, the question is: how to get the location of the pressed button? I will explain this in the following steps, and you should also look at the provided code:

  1. First, we set all the Rows to Logic level 0 and all the Columns to Logic level 1.
  2. Whenever we press a button, the column and row corresponding to that button get shorted. This action forces the corresponding column to Logic level 0 because that column becomes connected (shorted) to the row, which is at Logic level 0. Thus, we determine the column number (See the main() function logic).

Now we need to find the Row number. For this, we create four functions corresponding to each column. For example, if any button in column one is pressed, we call the function row_finder1() to find the row number.

In the row_finder1() function, we reverse the logic levels: now all Rows are 1 and Columns are 0. The Row of the pressed button should now read 0 because it has become connected (shorted) to the column whose button is pressed, and all columns are at logic 0. Therefore, we scan all rows looking for a logic 0.

  • Whenever we find a Row at logic 0, we know that is the row of the pressed button. We now have the column number (obtained in step 2) and the row number, and we can print the number of that button using the lcd_data function.

The same procedure follows for every button press, and we use a continuous loop (like while(1)) to constantly check whether a button is pressed or not.

Example 8051 Assembly Language Program (ALP)

Here is an example 8051 Assembly Language Program (ALP) for scanning a 4×4 keypad:


; Keypad scanning program for 8051

ORG 0000H

; Define keypad rows and columns
KEYPAD_PORT EQU P1
ROWS EQU P1.0 ; Row 0
R1 EQU P1.1 ; Row 1
R2 EQU P1.2 ; Row 2
R3 EQU P1.3 ; Row 3
COLS EQU P1.4 ; Column 0
C1 EQU P1.5 ; Column 1
C2 EQU P1.6 ; Column 2
C3 EQU P1.7 ; Column 3

; Define keypad layout
KEYPAD_LAYOUT DB 0x01, 0x02, 0x03, 0x0A
             DB 0x04, 0x05, 0x06, 0x0B
             DB 0x07, 0x08, 0x09, 0x0C
             DB 0x0E, 0x00, 0x0F, 0x0D

MAIN:
  ; Initialize keypad port
  MOV KEYPAD_PORT, #0FFH

SCAN_KEYPAD:
  ; Scan rows
  MOV A, #0FEH ; Select row 0
  MOV KEYPAD_PORT, A
  CALL CHECK_COLS
  MOV A, #0FDH ; Select row 1
  MOV KEYPAD_PORT, A
  CALL CHECK_COLS
  MOV A, #0FBH ; Select row 2
  MOV KEYPAD_PORT, A
  CALL CHECK_COLS
  MOV A, #0F7H ; Select row 3
  MOV KEYPAD_PORT, A
  CALL CHECK_COLS
  SJMP SCAN_KEYPAD

CHECK_COLS:
  ; Check columns
  JB COLS, NO_KEY_PRESS
  JB C1, NO_KEY_PRESS
  JB C2, NO_KEY_PRESS
  JB C3, NO_KEY_PRESS

  ; Get row and column numbers
  MOV R0, KEYPAD_PORT
  ANL A, #0F0H
  SWAP A
  MOV R1, A
  MOV A, KEYPAD_PORT
  ANL A, #0FH
  ORL A, R1

  ; Get key value from keypad layout
  MOV DPTR, #KEYPAD_LAYOUT
  MOVC A, @A+DPTR
  MOV R2, A

  ; Display key value (optional)
  ; ...

NO_KEY_PRESS:
  RET
END