Interfacing 4x4 Matrix Keypad | AT89C51


I am using the AT89C51 to interface the 4x4 Matrix Keypad and display the Input on a 16x2 LCD Display.The tools I will be using are :

  • Keil uVision 5
  • Proteus 8 Professional

The LCD is operated in 8 bit mode to display the key that is pressed. The keypad is connected to 
Port-1 and the LCD is connected to Port-2 [Data Pins : D0-D7]. 
Initially the keypad is connected such that Row pins are LOW [ 0 ] and Column pins are HIGH [ 1 ]. Whenever a key is pressed, it will short the circuit and gives the Column a LOW. Then a function is called to check which ROW that key belongs to. For this purpose, we use four user defined functions to determine the ROW number.


NOTE : Here the key should be pressed down for 2 seconds to display the input on the screen. This can be debugged along the way.


_______________________________________________________________________________

Code : Embedded C
IDE : Keil uVision 5
Simulator : Proteus 8 Professional
uController : AT89C51


/* Created  by mbedsyst */

#include<reg51.h>

sbit rs = P3^0;
sbit rw = P3^1;
sbit en = P3^2;

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

void lcdcmd (unsigned char);
void lcddat (unsigned char);
void lcdinit ();
void rowfinder1 ();
void rowfinder2 ();
void rowfinder3 ();
void rowfinder4 ();

void delay( unsigned int count)
{
unsigned int i;
while(count)
{
i = 115;
while(i>0)
{
i--;
}
count--;
}
}
void lcdinit ()
{
lcdcmd (0x38); // 5X7 matrix crystal
delay(100);
        lcdcmd (0x01); // clear screen
delay(100);
lcdcmd (0x10); // cursor blinking
delay(100);
lcdcmd (0x0C); // display on
delay(100);
lcdcmd (0x80); // Force cursor to 1x1 position
delay(100);
}
void lcdcmd (unsigned char val)
{
P2 = val;
rs = 0;
rw = 0;
en = 1;
delay(10);
en = 0;
}
void lcddat (unsigned char val)
{
P2 = val;
rs = 1;
        rw = 0;
en = 1;
delay(10);
en = 0;
}
lcd_dataa (unsigned char *disp)    // function to send string to LCD
{
int x;
for(x=0;disp[x]!=0;x++)
{
lcddat (disp[x]);
}
}
void rowfinder1 ()
{
R1=R2=R3=R4 = 1;
C1=C2=C3=C4 = 0;
if(R1==0) lcddat('7');
if(R2==0) lcddat('4');
if(R3==0) lcddat('1');
if(R4==0) lcddat('C');
}
void rowfinder2 ()
{
R1=R2=R3=R4 = 1;
C1=C2=C3=C4 = 0;
if(R1==0) lcddat('8');
if(R2==0) lcddat('5');
if(R3==0) lcddat('2');
if(R4==0) lcddat('0');
}
void rowfinder3 ()
{
R1=R2=R3=R4 = 1;
C1=C2=C3=C4 = 0;
if(R1==0) lcddat('9');
if(R2==0) lcddat('6');
if(R3==0) lcddat('3');
if(R4==0) lcddat('=');
}
void rowfinder4 ()
{
R1=R2=R3=R4 = 1;
C1=C2=C3=C4 = 0;
if(R1==0) lcddat('/');
if(R2==0) lcddat('*');
if(R3==0) lcddat('-');
if(R4==0) lcddat('+');
}
void main()
{
P2 = 0x00; // output declaration, data lines d0-d7 connect
lcdinit();
while (1)
{

                lcdinit();
                delay(20);
C1=C2=C3=C4 = 1;
R1=R2=R3=R4 = 0;

if(C1==0) rowfinder1 ();
else if(C2==0) rowfinder2 ();
else if(C3==0) rowfinder3 ();
else if(C4==0) rowfinder4 ();
delay(1000);
lcdinit();

        }
}
__________________________________________________________________________


Popular posts from this blog

Cifradopro: A baremetal Hardware Security Module using the STM32L4S5 Cortex-M4 MCU

SignGlove: Bridging the Communication Gap for Paralyzed Patients

Capturing images using the Digital Camera Interface | STM32L4 | DCMI | CMSIS