NPEDUCATIONS, Electronic circuits development, electronics tutorials, microcontroller tutorials and projects, advanced microcontroller (ARM) based tutorials and projects, Embedded c development, Embedded c for ARM cortex M, Intel IoT based projects, IoT projects, CC3200 launch pad projects, MSP430 Launchpad tutorials and projects, Tiva C launch pad tutorials and projects, 8051 tutorials and projects, sensor interfacing with microcontroller tutorials, data communications and networking tutorials, peripheral interfacing with microcontroller, led message scrolling display, arduino based tutorials and projects, intel galileo based tutorials and project, ADC interfacing tutorials, LED Blinking, LCD interfacing, Embedded System tutorials and projects, B.tech projects, M.tech projects, online Embedded C training

Interfacing 4x3 keypad with 8051 Microcontroller


Basically, the 4x3 keypad contains push buttons that are arranged in four rows and three columns produce twelve characters as shown in the figure1.Sometimes this called as “4x3 switch matrix” due to the arrangement of switches in a matrix form. The internal construction of these keypads includes metal dome contacts and conductive rubber. Ok! Construction of keypad is out of scope the tutorial. In this tutorial I am only concentrating on interfacing of keypad with 8051 microcontroller.

1. CONNECTIONS:

The three column lines of the keypad as shown in the figure 1 are connected to the PORT 1 upper pins (P1.0 – COL1, P1.1 – COL2, P1.2 – COL3)  and the four row lines are connected to PORT1 lower pins (P1.4 – ROW1, P1.5 – ROW2, P1.6 – ROW3, P1.7 – ROW7). Three resistors of 10k are connected between the column lines and power supply, to make the column lines are always high. Here one thing should be clear, that the column lines connected to the microcontroller should act as input lines and the row lines acts as output lines.

2. WORKING:

2.1 INITIALIZATION:

Remember that, the column lines of keypad are connected to port1 pins and these pins should be configured as input by placing logic high (‘LOGIC 1’) during port initialization. Similarly, the row lines of keypad are connected to port1 pins and configured them as output by placing logic zero (‘LOGIC 0’) during port initialization.

ROW1 = 0; //MAKE ALL ROW LINES OF KEY TO ZERO
ROW2 = 0; // TO MAKE THEM AS OUTPUT LINES
ROW3 = 0;
ROW4 = 0;                
COL1 = 1; // MAKE ALL COL'S AS HIGH
COL2 = 1; // TO MAKE THEM AS INPUT LINES
COL3 = 1;



2.2 SCANNING MECHANISM:

The scanning mechanism starts by making the first row (ROW1) of keypad to LOW (LOGIC ‘0’) and all column lines should be high (LOGIC ‘1’). Now check any one of the column lines goes low. If any column line goes low means that particular button is pressed, otherwise nothing is pressed.
Now the question in our mind is “which button is pressed”. This can be identified by checking which column is goes low.

If ROW1 = 0, COL1 = 0, remaining all lines are high means BUTTON1 is pressed.
If ROW1 = 0, COL2 = 0, remaining all lines are high means BUTTON2 is pressed.
If ROW1 = 0, COL3 = 0, remaining all lines are high means BUTTON3 is pressed.
If ROW2 = 0, COL1 = 0, remaining all lines are high means BUTTON4 is pressed.

Likewise we can identify the remaining buttons also and the logic is shown in the figure1. This can be done by repeatedly, that why use a infinite loop in the routine “key()” – check out the c program.
Figure1 shows the keypad connection and its logic to interface with MCU.



3. PROGRAM DESCRIPTION:

Firstly create an 2-D array (look up table) of numbers like below. This array holds the numbers represented by the keypad. Off course you can change the data as you need, means you define characters, special symbols, numbers, etc..,

char keypad[4][3]=     {         
'1','2','3',
                                                '4','5','6',
                                                '7','8','9',
                                                '*','0','#'
 };          //look up table

Now in the subroutine “key()”, first make first row lines to zero one and scan for the column status. And next make the next row and scan for the column and so on.

ROW1 = 0; //MAKE FIRST ROW IN KEYPAD TO ZERO
ROW2 = 1;
ROW3 = 1;
ROW4 = 1;                
COL1 = 1; // MAKE ALL COL'S AS HIGH
COL2 = 1;
COL3 = 1;

If any one of the column line is low then wait until it back to logic high by continuously checking the col line (while(COL1 == 0)). If col line goes high then place column and row values to get the correct value from the keypad array. Finally place a break statement to get out of the loop, otherwise it stay in infinite loop.

if (COL1 == 0){while(COL1 == 0);row=1;col=1;break;}.

For LCD interface I encourage you to read the tutorial to interface 16x2 LCD with 8051.



Circuit Diagram of 4x3 keypad interface with 8051 along with 16x2 LCD.


Figure2 shows the circuit diagram for 4x3 keypad interface with 8051 along with LCD


4. Keypad interface code:

#include
#include
#include "LCD.h"

sbit COL1=P1^0;
sbit COL2=P1^1;
sbit COL3=P1^2;

sbit ROW1=P1^4;
sbit ROW2=P1^5;
sbit ROW3=P1^6;
sbit ROW4=P1^7;

unsigned char ROW=0,COL=0;
char value='\0', temp=0;
char keypad[4][3]=	{	'1','2','3',
				'4','5','6',
				'7','8','9',
				'*','0','#' };	  //look up table

char key(void);
void main(void)
{	
	P2 = 0x00;
        ROW1 = 0; //MAKE ALL ROWs IN KEYPAD TO ZERO
        ROW2 = 0;
	ROW3 = 0;
	ROW4 = 0;		
	COL1 = 1; // MAKE ALL COL'S AS HIGH
	COL2 = 1;
	COL3 = 1;

       LCD_init();

	while(1)
	{
		send_cmd(0x01);
		delayms(50);
		send_cmd(0x80);
		delayms(50);
		send_data("Enter keys....");
		delayms(600);
		send_cmd(0x01);
		delayms(50);
		send_cmd(0x80);
		delayms(50);
		while(1)		
		{
			value =  key();
			delayms(50);				
			send_char(value);
			delayms(100);
		 }
	}

} 

char key()
{
	
	while(1)
	{

		ROW1 = 0; //MAKE FIRST ROW IN KEYPAD TO ZERO
		ROW2 = 1;
		ROW3 = 1;
		ROW4 = 1;		
		COL1 = 1; // MAKE ALL COL'S AS HIGH
		COL2 = 1;
		COL3 = 1;
		if(COL1 == 0){while(COL1 == 0);ROW=1;COL=1;break;}  
		if(COL2 == 0){while(COL2 == 0);ROW=1;COL=2;break;}  
		if(COL3 == 0){while(COL3 == 0);ROW=1;COL=3;break;}  
		ROW1 = 1; 
		ROW2 = 0; //MAKE SECOND ROW IN KEYPAD TO ZERO
		ROW3 = 1;
		ROW4 = 1;		
		COL1 = 1; // MAKE ALL COL'S AS HIGH
		COL2 = 1;
		COL3 = 1;
		if(COL1 == 0){while(COL1 == 0);ROW=2;COL=1;break;}  
		if(COL2 == 0){while(COL2 == 0);ROW=2;COL=2;break;}  
		if(COL3 == 0){while(COL3 == 0);ROW=2;COL=3;break;}
		ROW1 = 1; 
		ROW2 = 1; 
		ROW3 = 0;//MAKE THIRD ROW IN KEYPAD TO ZERO
		ROW4 = 1;		
		COL1 = 1; // MAKE ALL COL'S AS HIGH
		COL2 = 1;
		COL3 = 1;
		if(COL1 == 0){while(COL1 == 0);ROW=3;COL=1;break;}  
		if(COL2 == 0){while(COL2 == 0);ROW=3;COL=2;break;}  
		if(COL3 == 0){while(COL3 == 0);ROW=3;COL=3;break;}
		ROW1 = 1; 
		ROW2 = 1; 
		ROW3 = 1;
		ROW4 = 0;//MAKE 4TH ROW IN KEYPAD TO ZERO		
		COL1 = 1; // MAKE ALL COL'S AS HIGH
		COL2 = 1;
		COL3 = 1;
		if(COL1 == 0){while(COL1 == 0);ROW=4;COL=1;break;}  
		if(COL2 == 0){while(COL2 == 0);ROW=4;COL=2;break;}  
		if(COL3 == 0){while(COL3 == 0);ROW=4;COL=3;break;}
		delayms(500);
	}

	return	keypad[ROW-1][COL-1];
}

0 comments :

Post a Comment

Thanks for visiting NPEDUCATIONS. We will contact you soon

0 comments :

Post a Comment

Thanks for visiting NPEDUCATIONS. We will contact you soon

If you really like this tutorial, Don't forget to give the comment or please subscribe to the RSS feed by submitting your E-mail or like our Facebook page.
Related Posts Plugin for WordPress, Blogger...

 
Powered by Blogger