dolphinlabs
...
Doplhin labs
News

4X4 Keyboard Matrix Interface


·         Program Statement: Interfacing of 4X4 matrix Keypad with 89C51 microcontroller and display the output on LCD.

Connections:     P3.0=RS,P3.1=RW,P3.2=EN , PORT0 =LCD data pins

                                    P2.0-P2.3=Rows , P1.0-P1.3=Columns

 

Hardware Connections for 8051 Board:-

1.      Give supply to 8051 board with the help of adapter

2.       Connect +5V pin of 8051 microcontroller to +5V supply and GND pin of 8051 microcontroller to GND pin of supply.

3.      Connect +5V pin of RS232 interface to +5V supply and GND pin of RS232 interface to GND pin of supply.

4.      Connect R2OUT pin of RS232 interface to RXD pin of 8051 and T2IN pin of RS232 interface to TXD pin of 8051.

5.      Connect +5V pin of LCD interface to +5V supply pin and GND pin of LCD interface to GND pin of supply.

6.      Connect RS pin of LCD to P3.0 pin of microcontroller.

7.      Connect RW pin of LCD to P3.1 pin of microcontroller.

8.      Connect EN pin of LCD to P3.2 pin of microcontroller.

9.      Connect data pins of LCD D0-D7 to P0.0-P0.7 pins of microcontroller by using bus.

10.  Connect COL1 COL2 COL3 COL4 of keypad to P1.0 P1.1 P1.2 P1.3 pins of microcontroller respectively and ROW1 ROW2 ROW 3 ROW4 of keypad to P2.0 P2.1 P2.2 and P2.3 pins of microcontroller.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Circuit Diagram:

 

 

 

 

 

 

 

 

 

 

 

Program Code:

 

#include<reg51.h>

#define COL P2                                            //Define PORTs

#define ROW P1

sbit rs=P3^0;                                                         //RS=P3.0

sbit rw=P3^1;                                                        //R/W=P3.1

sbit en=P3^2;                                                        //EN=P3.2

void delay(unsigned int value);

void lcddata(unsigned char);

void lcdcmd(unsigned char);

unsigned char keypad[4][4]={'0','1','2','3',

                          '4','5','6','7',

                                  '8','9','A','B',

                                 'C','D','E','F'};

void main()

{

                unsigned char colloc,rowloc;

                COL=0xFF;                                     //Make P2 an input port

                lcdcmd(0x38);                                //2 lines and 5x7 matrix

                lcdcmd(0x0e);                                //Display on, cursor  blinking

                lcdcmd(0x01);                               //Clear display screen

                lcdcmd(0x06);                              //Shift cursor to right

                lcdcmd(0x80);                              //Force cursor to beginning of  1st line

 

                while(1)

                {

                                do

                                {

                                                ROW=0x00;         //Ground all rows

                                                colloc=COL;         //Read the columns

                                                colloc&=0x0F;     //Mask used bits

                                }while(colloc!=0x0F);         //check until all keys released

                                do

                                {

                                do

                                {

                                                delay(20);             //call delay

                                                colloc=COL;         //See if any key pressed

                                                colloc&=0x0F;     //Mask unused bits

                                }while(colloc==0x0F);        //Keep checking for key press

                                                delay(20);             //call delay for debounce

                                                colloc=COL;         //read columns

                                                colloc&=0x0F;    

                                }while(colloc==0x0F);        //wait for keypress

                                while(1)

                                {

                                                ROW=0xFE;       //Ground row 0

                                                colloc=COL;         //read columns

                                                colloc&=0x0F;    //mask unused bits

                                if(colloc!=0x0F)                   //Column detected

                                {

                                                rowloc=0;              //save  row location

                                                break;

                                }

                                                 ROW=0xFD;       //Ground row 1

                                                colloc=COL;         //Read columns

                                                colloc&=0x0F;

                                if(colloc!=0x0F)                   //column detected

                                {

                                                rowloc=1;              //save  row location

                                                break;

                                }

                                                ROW=0xFB;        //Ground row 2

                                                colloc=COL;         //Read columns

                                                colloc&=0x0F;

                                if(colloc!=0x0F)                   //column detected

 

                                {

                                                rowloc=2;              //save  row location

                                                break;

                                }

                                                ROW=0xF7;         //Ground row 3

                                                colloc=COL;         //Read columns

                                                colloc&=0x0F;     //mask unused bits

                                                rowloc=3;              //save  row location

                                                break;

                                }

                                if(colloc==0x0E)                  //Check column and send result to the LCD

                                lcddata(keypad[rowloc][0]) ;

                                else if(colloc==0x0D)

                                lcddata(keypad[rowloc][1]) ;

                                else if(colloc==0x0B)

                                lcddata(keypad[rowloc][2]) ;

                                else lcddata (keypad[rowloc][3]);

                                 }

                                 }

                                  void lcddata(unsigned char  x)

                                  {

                                  P0=x;                                   //Send data on PORT P0

                                  rs=1;                                     //RS=1 for data

                                  rw=0;                                    //R/W=0

                                  en=1;                                    //EN=1

                                  delay(200);                         //Call  delay

                                  en=0;                                    //EN=0

                                  }

                                  void lcdcmd(unsigned char  y)

                                  {

                                  P0=y;

                                  rs=0;                                     //RS=0 for command

                                  rw=0;

                                  en=1;

                                  delay(200);                      //call delay

                                  en=0;

                                  }

                                  void delay(unsigned int value)

                                  {

                                  unsigned int i,j;

                                  for(i=0;i<255;i++)

                                  for(j=0;j<value;j++);

                                  }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Get SOCIAL







SAFE & SECURE SHOPPING

visa card master card maestro card
Maverick IT Industries Pvt. Ltd.