Keypad Switch Interfacing with 8051

What is a Keypad?

A keypad is an input device consisting of a set of buttons or keys arranged in a grid pattern, typically used for entering numbers, characters, or control commands into an electronic system.

Image of Keypad:-

How does Keypad work?

The keypad in the image is a 4*3 matrix keypad which means it has 3 columns and 3 rows.

Now I want to print the number on the 1st row 2nd column then keep the 1st row low and the 2nd,3rd, and  4th row high. This means now only 1st row is working. Now we have three columns. If we want to enter the 2nd column then set the column low.

For eg:-  

R1=0;

R2=R3=,R4=1;

if (C2==0)

{

   The working or the character you want.

}

Why use a Keypad not switches?

The 8051 microcontroller has limited I/O pins (32 total). Connecting 16 individual switches would use up half of these pins, leaving little room for other peripherals. To save pins, we use a 4x4 matrix keypad, which offers 16 keys but requires only 8 pins (4 rows + 4 columns).

Task 1:- Use a 4*3 keypad, and from the first row turn on 4 LEDs ,and second row turn them off.

 Code:-

#include<reg51.h>

sbit R1=P2^1;

sbit R2=P2^2;

sbit R3=P2^3;

sbit R4=P2^4;

sbit C1=P2^5;

sbit C2=P2^6;

sbit C3=P2^7;

sbit LED1=P3^0;

sbit LED2=P3^1;

sbit LED3=P3^2;

void main()

{

LED1=1;

LED2=1;

LED3=1;

while(1)

{

R1=0;

R2=R3=R4=1;

if(C1==0)

{

LED1=0;

while(C1==0);

}

if(C2==0)

{

LED2=0;

while(C1==0);

}

if(C3==0)

{

LED3=0;

while(C1==0);

}

R2=0;

R1=R3=R4=1;

if(C1==0)

{

LED1=1;

while(C1==0);

}

if(C2==0)

{

LED2=1;

while(C1==0);

}

if(C3==0)

{

LED3=1;

while(C1==0);

}

}

}

Output: