8051 – Keypad Interfacing

Hi all…. Today we will see 8051 Keypad Interfacing. Before that i would suggest you to go through this link to know about the Keypad characteristics. And i assumed that you know LCD interfacing. Other wise you can see that LCD interfacing tutorials in our website. Let’s start…

8051 Keypad Interfacing.

Components Required

  • 4×4 Keypad or 3×4 Keypad (Here we will discuss both codes)
  • LCD Module (To print the Keys pressed)
  • 8051 Microcontroller

4×4 Matrix Keypad Interfacing

Circuit Diagram

design 8051 - Keypad Interfacing

LCD

  • RS  –  P3.5
  • RW  – P3.6
  • EN  –  P3.7
  • Data Lines – P2

Keypad

  • R1 – P1.0
  • R2 – P1.1
  • R3 – P1.2
  • R4 – P1.3
  • C1 – P1.4
  • C2 – P1.5
  • C3 – P1.6
  • C4 – P1.7

Code 

This code might be looking like big. But concept wise it is very easy. Please go through this code.

Code Explanation

I assumed that you already know about the LCD interfacing.  Now look this line in keypad function,

In this code i’m taking row as a output and column as a input. 

  1. In first line I’m assigning high to all column. (c1=c2=c3=c4=1;)
  2. Then i’m assigning first row to zero and keeps remaining row as high. (r1=0;r2=1;r3=1;r4=1;)
  3. Then i’m checking first column is zero or not. If it is zero then i should wait until that button depressed. Then i can know the pressed key.
  4. If not i’m checking next column. Like that i’m checking all rows and column.
  5. If no keys pressed in the row1, then i’m making row2 as zero. Remaining rows are high. Then follow the above steps.

Output

output1 8051 - Keypad Interfacing

3×4 Matrix Keypad Interfacing

Circuit Diagram

design-2 8051 - Keypad Interfacing

LCD

  • RS  –  P3.5
  • RW  – P3.6
  • EN  –  P3.7
  • Data Lines – P2

Keypad

  • R1 – P1.0
  • R2 – P1.1
  • R3 – P1.2
  • R4 – P1.3
  • C1 – P1.4
  • C2 – P1.5
  • C3 – P1.6

Code

Code Explanation

Same as 4×4 Matrix keypad.

Output

output2 8051 - Keypad Interfacing

Tasks

Please try these tasks. If you have any doubt please ask us by commenting below.

  1. Whatever i’m pressing in the Keypad that character should displayed in the Serial terminal.
  2. Connect 8 LEDs into Port 0. If i press 0, No LEDs will be ON. If i press 1, 1 LED should be ON. If i press 2, 2 LEDs should ON. Like that upto 8 numbers you write the program.