Skip to main content

Keypad Matrix implementation with 8051 microcontroller

Keypads are a part of HMI or Human Machine Interface and play really important role in a small embedded system where human interaction or human input is needed. Martix keypads are well known for their simple architecture and ease of interfacing with any microcontroller. In this part of tutorial we will learn how to interface a 4x4 matrix keypad with 8051 microcontroller. Also we will see how to program then in Embedded C language.

Normally the port pin is high but when a switch is pressed the controller pin gets a Low signal and we come to know that a switch has been pressed. One end of switch is connected to the port pin whereas the other end is connected to the Ground.

Interfacing:

In case of matrix Keypad, both the ends of switches are connected to the port Pin. Over here we have considered a 4x4 matrix keypad i.e. four rows and three columns. So in all twelve switches have been interfaced using just seven lines.

The adjoining figure shows the diagram of a matrix keypad and how it is interfaced with the controller.
 
Figure 2: Connection with ports


As you can see no pin is connected to ground, over here the controller pin itself provides the ground. We pull one of the Column Pins low & check the row pins if any of the Pin is low then we come to know which switch is pressed.

Suppose we make column 1 pin low and while checking the rows we get Row 3 is low then we come to know switch 7 has been pressed.

Algorithm:

1. Start.
2. Make All Pins High.
3. Make Column 1 pin low.
4. Check if Row 1 is low, if yes then Switch 1 has been pressed.
5. Check if Row 2 is low, if yes then Switch 4 has been pressed.
6. Check if row 3 is low if yes then Switch 7 has been pressed.
7. Check if row 4 is low if yes then Switch 10 has been pressed.
8. Make Column 1 Pin high & Column 2 Pin Low.
9. Check if Row 1 is low, if yes then Switch 2 has been pressed.
10. Check if Row 2 is low, if yes then Switch 5 has been pressed.
11. Check if row 3 is low if yes then Switch 8 has been pressed.
12. Check if row 4 is low if yes then Switch 11 has been pressed.
13. Make Column 2 Pin high & Column 3 Pin Low.
14. Check if Row 1 is low, if yes then Switch 3 has been pressed.
15. Check if Row 2 is low, if yes then Switch 6 has been pressed
16. Check if row 3 is low if yes then Switch 9 has been pressed.
17. Check if row 4 is low if yes then Switch 12 has been pressed.
18. Make column 3 pin high.
19. Stop.

Please make sure at any time only one column Pin should remain Low.

Program to implement switch matrix

4*4 switch matrix
#include"REG52.h"

sbit R1=P1^0;
sbit R2=P1^1;
sbit R3=P1^2;
sbit R4=P1^3;
sbit C1=P1^4;
sbit C2=P1^5;
sbit C3=P1^6;
sbit C4=P1^7;

unsigned char switch1(void);
void main()
{
unsigned char sw;

P2=0;
while(1)
{
sw= switch1();
if(sw!=0) P2=sw; } }
unsigned char switch1(void)
{ 
R1=R2=R3=R4=1; 
C1=C2=C3=C4=0;

if((R1==0)||(R2==0)||(R3==0)||(R4==0))

{ 
R1=R2=R3=R4=1; 
C1=C2=C3=C4=1;

C1=0;
if (R1==0) return(1);
if (R2==0) return(2);
if (R3==0) return(3);
if (R4==0) return(4);
C1=1; 

C2=0;
if (R1==0) return(5);
if (R2==0) return(6);
if (R3==0) return(7);
if (R4==0) return(8);
C2=1; 

C3=0;
if (R1==0) return(9);
if (R2==0) return(10);
if (R3==0) return(11);
if (R4==0) return(12);
C3=1; 

C4=0;
if (R1==0) return(13);
if (R2==0) return(14);
if (R3==0) return(15);
if (R4==0) return(16);
C4=1; 
}

else return (0); }

Comments

Popular posts from this blog

Buzzer interface with 8051 microcontroller

Buzzer  is a electronic device that converts the electronic signal into buzzing noise, that is applied to it. It can be used as electronic bell or as quiz buzzer in many applications around us. Here, i world like to discuss the interfacing of a small buzzer with 8051 microcontroller and how different projects can be constructed. Buzzer Interfacing: This project shows the interface with AT89S52 microcontroller to a buzzer. When a push button is pressed, the buzzer will get ON and OFF ( number of times set in the code ) and then stops. Circuit Diagram: - The port P1 of the microcontroller is connected to buzzer. This type of connection is possible, if the current requirements of the buzzer is not more than 20mA. The output is in current source mode so that buzzer will turn ON when the output of the port is logic LOW. Switch is connected to port P3 which remains at logic HIGH by pull up resistor.  Code: #include "REG52.h" #define buz P1 sbit SW=P3^0; long int i; voi

Different ways to generate delays in 8051

The delay length in 8051 microcontroller depends on three factors: The crystal frequency the number of clock per machine the C compiler. The original 8051 used 1/12 of the crystal oscillator frequency as one machine cycle. In other words, each machine cycle is equal to 12 clocks period of the crystal frequency connected to X1-X2 pins of 8051. To speed up the 8051, many recent versions of the 8051 have reduced the number of clocks per machine cycle from 12 to four, or even one. The frequency for the timer is always 1/12th the frequency of the crystal attached to the 8051, regardless of the 8051 version. In other words, AT89C51, DS5000, and DS89C4x0 the duration of the time to execute an instruction varies, but they all use 1/12th of the crystal's oscillator frequency for the clock source. 8051 has two different ways to generate time delay using C programming, regardless of 8051 version. The first method is simply using Loop   program function in which Delay() function i

How to Switch OFF Citizen Calculator

You might be confused on seeing no OFF button on your calculator. Actually, these are general calculator available at low cost in the market. They process simple calculations like division, subtraction, Addition, Multiplication. But, how will you OFF that calculator having no option on them. There is always a trick which OFF them. It depends on the company calculator and their series to which they belong. I can tell you about Citizen Calculator CT-500 (as you can see in the image), how can you switch OFF it and can save battery too. If you leave calculator idle for 6-7 minutes, it will goes OFF automatically. My calculator goes OFF in 6.50 minutes from the point of leaving it idle. But, this is not the power saving trick for these types of calculator having not much backup battery. To save the power of your normal calculator, here is the trick to switch OFF it. Press ' /(divide sign)', 'x(multiply sign)', '%(percentage sign)', 'autoreplay option(