4x4 Keypad interfacing with 8051

4x4 Keypad

4x4 Keypad

Introduction

The keypad is used as an input device to read the key pressed by the user and to process it.

4x4 keypad consists of 4 rows and 4 columns. Switches are placed between the rows and columns. A keypress establishes a connection between the corresponding row and column between which the switch is placed.

To read the keypress, we need to configure the rows as outputs and columns as inputs.

Columns are read after applying signals to the rows in order to determine whether or not a key is pressed and if pressed, which key is pressed.

For more information about the keypad and how to use it, refer to the topic 4x4 Keypad in the sensors and modules section.

 

Example

Here, we are going to interface the 4x4 keypad with AT89S52 (8051) and will display the pressed key on LCD16x2.

 

Interfacing Diagram

Keypad Interfacing with 8051

 

Keypad interfacing with 8051

Program

/*
	4x4 Keypad Interfacing with 8051(AT89s52)
	http://www.electronicwings.com
*/


#include<reg52.h>
#include<stdio.h>
#include<string.h>
#include "LCD_8_bit.h"		/* 16x2 LCD header file*/

#define keyport P1

sbit RS		= P3^5;		/* RS(register select) for LCD16x2 */
sbit RW 	= P3^6;		/* RW(Read/write) for LCD16x2 */
sbit ENABLE 	= P3^7;		/* EN(Enable) pin for LCD16x2*/

unsigned char keypad[4][4] =	{{'7','8','9','/'},
				{'4','5','6','x'},
				{'1','2','3','-'},
				{' ','0','=','+'} };

unsigned char colloc, rowloc;


unsigned char key_detect()
{
	keyport=0xF0;			/*set port direction as input-output*/
	do
	{
		keyport = 0xF0;				
		colloc = keyport;
		colloc&= 0xF0;	/* mask port for column read only */
	}while(colloc != 0xF0);		/* read status of column */
	
	do
	{
		do
		{
			delay(20);	/* 20ms key debounce time */
			colloc = (keyport & 0xF0);	/* read status of column */
		}while(colloc == 0xF0);	/* check for any key press */
		
		delay(1);
		colloc = (keyport & 0xF0);
	}while(colloc == 0xF0);
			
	while(1)
	{
		/* now check for rows */
		keyport= 0xFE;											/* check for pressed key in 1st row */
		colloc = (keyport & 0xF0);
		if(colloc != 0xF0)
		{
			rowloc = 0;
			break;
		}

		keyport = 0xFD;									/* check for pressed key in 2nd row */
		colloc = (keyport & 0xF0);
		if(colloc != 0xF0)
		{
			rowloc = 1;
			break;
		}
			
	keyport = 0xFB;			/* check for pressed key in 3rd row */
	colloc = (keyport & 0xF0);
	if(colloc != 0xF0)
	{
		rowloc = 2;
		break;
	}

	keyport = 0xF7;			/* check for pressed key in 4th row */
	colloc = (keyport & 0xF0);
	if(colloc != 0xF0)
	{
		rowloc = 3;
		break;
	}	
}
	
	if(colloc == 0xE0)
	{
		return(keypad[rowloc][0]);
	}		
	else if(colloc == 0xD0)
	{
		return(keypad[rowloc][1]);
	}	
	else if(colloc == 0xB0)
	{
		return(keypad[rowloc][2]);
	}	
	else
	{
		return(keypad[rowloc][3]);
	}	
}


int main(void)
{
	LCD_Init();
	LCD_String_xy(1,0,"Press a key");
	
	while(1){
		LCD_Command(0xc0);
		LCD_Char(key_detect());		/* Display which key is pressed */
	}		
}

 


Components Used

4x4 Matrix Keypad
4x4 Matrix Keypad
1
8051 AT89c51
8051 AT89c51
1

Downloads

Keypad Interfacing with 8051 Download
Ad