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

Comparison Chart Between 8051, 8052, 8031and 8751

8051 is a popular 8-bit microcontroller and has been used in many applications since Intel introduced it. Many 8051 architecture are produced by Triscend, Intel, Atmel, Philips, Infineon (Siemens), ISSI, and Max Corp. Today, 8051 microcontrollers may not be popular but, the 8051 architecture is still popular and employed in thousands of embedded applications. This 8-bit architecture has been different segments such as 8052, 8051, 8751 and 8031 . 8052 is the super-set of 8051 and 8031 is the memory-less microcontroller hence, it has interfaced with external ROM. Whereas, 8751 chip has only 4Kbytes of on chip UV-EPROM. Everyone knows about the general 40-pin microcontroller i.e. 8051 introduced by Intel in 1980s and consists of serial communication pins, Timer, Interrupts, RAM, ROM. It has 4 ports and each port has 8 pins, total 32 pins and other 8 pins for other purposes. 8052 is the super-set of 8051 that consists of 8K bytes of internal RAM (4K in 8051), 256 by...

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 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(...