Interfacing of keypad with 8051 microcontroller.

Interfacing of keypad with 8051 microcontroller.

A keypad interfacing with 8051 microcontrollers is interesting. A keypad is an organized matrix of switches in rows and columns and it comes in the various form like a numeric keypad, alphanumeric keypad. It also comes in different size like 4×3, 4×4, etc.

In real life, the keypad has a lot of application like calculator, electronic lock, mobile phone, and many more electronic appliances. In this article, we will learn the interfacing of the keypad with 8051 micro-controller.

 

Note:  Numeric keypad contains mostly numeric numbers.

keypad

Connection of keypad with 8051 microcontroller

In below image, I have described the connection of keypad with 8051 microcontrollers. Here I have connected the column and rows of keypad corresponding to lower nibble of port-2 and the upper nibble of port-2.

interfacing of keypad



Algorithm to write a sample program for Keypad Interfacing

Here we will create an interface between 8051 microcontrollers and a 4×3 numeric keypad. Here I am not considering the switch debouncing.

There are following steps to write a program for keypad INTERFACING
  • Firstly you have to connect pin of rows and columns of the keypad with microcontroller pins and make the row pins as output and column pins as input.

NOTE: Whenever in keypad any of the keys pressed then corresponding row and column get connected and status of the row will be reflected in the corresponding column.

  • Make all row pins high except the row1 pin and check the status of col1,col2, and col3. If anyone of them become low i.e. any one of the keys pressed in row1. If there is no column low in row1 i.e. no key pressed on the keypad.

keypad

e.g.
Suppose in the above scenario col1 become low when row1 is the low i.e. first key of col1 would be pressed on the keypad.

  • Just repeat the above scenario for row2, row3, and row4 and check the status of col1,col2, and col3.In the below image, I have made the row2 pins low and check the status of col1,col2, and col3 pins.

 

In the below image, I have made the row3 pins low and check the status of col1,col2, and col3 pins.

keypad

In the below image, I have made the row4 pins low and check the status of col1,col2, and col3 pins.

keypad

 

If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.

Your free trial is waiting

 

Sample program to describe the Interfacing of a keypad with 8051 microcontroller

In the below program, I am describing keypad interfacing with 8051. The below sample program reads the user input and display it on the 16×2 LCD. You can find here more about the interfacing of LCD and 8051 micro-controller.

working of keypad

 

/*Hardware
    --------
    At89c51 @ 12MHz external crystal.



     LCD

         VSS -  GND
         VDD - +5V
         VEE - connect with ground

         RS -  P1.0
         RW -  P1.1
         E  -  P1.2

         LCD_D0 - P3.0
         LCD_D1 - P3.1
         LCD_D2 - P3.2
         LCD_D3 - P3.3

         LCD_D4 - P3.4
         LCD_D5 - P3.5
         LCD_D6 - P3.6
         LCD_D7 - P3.7

        LED+ - +5V
        LED- - GND

      KEYPAD

         COL_1 -   P2.0
         COL_2 -   P2.1
         COL_3 -   P2.2

         ROW_1 -   P2.4
         ROW_2 -   P2.5
         ROW_3 -   P2.6
         ROW_4 -   P2.7

   */

#include<reg51.h>
#include<string.h>


// macro
#define COL1 0
#define COL2 1
#define COL3 2

#define MAX_ROW 3

//LCD
#define LCD P3 /*Attached Lcd on Port*/

sbit rs = P1^0; /*Configure RS Pin*/
sbit rw = P1^1; /*Configure R/W pin*/
sbit e  = P1^2; /*Configure Enable pin*/

//KEYPAD
#define KEYPAD P2

sbit COL_1 =  P2^0;
sbit COL_2 =  P2^1;
sbit COL_3 =  P2^2;

sbit ROW_1 =  P2^4;
sbit ROW_2 =  P2^5;
sbit ROW_3 =  P2^6;
sbit ROW_4 =  P2^7;


/*Function to write command on Lcd*/
void LcdCommand(const char cCommand);

/*Function to write command on Lcd*/
void LcdData(const char cData);
/*Function to display message on Lcd*/
void DisplayMessage(const char *pszMessage);

/*Function To Initialize Lcd*/
void LcdInit(void);

/*Function to Provide delay*/
void Delay(unsigned int);

/*Reset The Row*/
void DoResetRow(short siRowNumber);

/*Check Columnn*/
char Check_Column(void);

/*Read Col Status*/
char ReadColStatus(void);

//main
int  main()
{
    char ucData=0;
    unsigned char cLen =0;

    KEYPAD = 0x0F; //Make Row O/p & Col I/p
    LCD    = 0x00;  //Make Lcd O/p
    rs = rw = e =0; //O/p

    LcdInit(); //initialize the lcd

    LcdCommand(0x80); /*Address of DDRAM*/

    DisplayMessage("Enter Number:");

    LcdCommand(0xc0); /*Address of DDRAM*/

    while(1)
    {

        if(cLen <= 15)
        {

            ucData = ReadColStatus(); /*Read column Status*/
            LcdData(ucData);
            cLen++;

        }
        else
        {

            LcdCommand(0x01); /*Clear the lcd*/
            LcdCommand(0x80); /*Address of DDRAM*/
            DisplayMessage("Enter Number:");
            LcdCommand(0xc0); /*Address of DDRAM*/
            cLen=0;
        }
    }
    return 0;
}

/*Function to write command on Lcd*/
void LcdCommand(const char cCommand)
{

    rs = 0;
    rw = 0;
    e  = 1;
    LCD = cCommand;
    Delay(1);
    e=0;

}

/*Function to write command on Lcd*/
void LcdData(const char cData)
{

    rs = 1;
    rw = 0;
    e  = 1;
    LCD = cData;
    Delay(1);
    e=0;

}

/*Function to Display message on Lcd*/
void DisplayMessage(const char *pszMessage)
{

    while(*pszMessage!='\0')   //Till Null character
    {

        LcdData(*pszMessage);
        pszMessage++;
    }
}

/*Function to Provide Delay*/
void Delay(unsigned int i)
{
    int j,k;
    for(j=0; j<i; j++)
        for(k=0; k<1275; k++);
}

/*Initialise the LCD_DATA*/
void LcdInit(void)
{

    LcdCommand(0x01);
    Delay(2);
    LcdCommand(0x38);
    Delay(2);
    LcdCommand(0x06);
    Delay(2);
    LcdCommand(0x0c);
    Delay(2);
}

char ReadColStatus(void)
{

    char ucData='p';

    while('p' == ucData)
    {

        ucData = Check_Column();

    }
    return ucData;
}

char Check_Column(void)
{

    short siColNumber=0;
    const unsigned char ucaKeyPad[4][3] =           //Key Pad 4x3
    {
        {'1','2','3'},
        {'4','5','6'},
        {'7','8','9'},
        {'*','0','#'}
    };

    for(siColNumber = 0; siColNumber <= MAX_ROW; siColNumber++)
    {

        DoResetRow(siColNumber); /*Reset the row*/

        if(COL_1 == 0)
        {
            Delay(30);
            return ucaKeyPad[siColNumber][COL1];
        }
        if(COL_2 == 0)
        {
            Delay(30);
            return ucaKeyPad[siColNumber][COL2];
        }
        if(COL_3 == 0)
        {
            Delay(30);
            return ucaKeyPad[siColNumber][COL3];
        }
    }
    return 'p';
}

void DoResetRow(short siRowNumber)
{

    ROW_1=ROW_2=ROW_3=ROW_4= 1; /*Make all row high*/

    switch(siRowNumber)
    {
    case 0:
        ROW_1 =0;
        break;
    case 1:
        ROW_2 =0;
        break;
    case 2:
        ROW_3 =0;
        break;
    case 3:
        ROW_4 =0;
        break;
    }
}

 

Recommended Post