8051 – Keypad Interfacing

Hi all…. Today we will see 8051 Keypad Interfacing. Before that i would suggest you to go through this link to know about the Keypad characteristics. And i assumed that you know LCD interfacing. Other wise you can see that LCD interfacing tutorials in our website. Let’s start…

8051 Keypad Interfacing.

Components Required

  • 4×4 Keypad or 3×4 Keypad (Here we will discuss both codes)
  • LCD Module (To print the Keys pressed)
  • 8051 Microcontroller

4×4 Matrix Keypad Interfacing

Circuit Diagram

LCD

  • RS  –  P3.5
  • RW  – P3.6
  • EN  –  P3.7
  • Data Lines – P2

Keypad

  • R1 – P1.0
  • R2 – P1.1
  • R3 – P1.2
  • R4 – P1.3
  • C1 – P1.4
  • C2 – P1.5
  • C3 – P1.6
  • C4 – P1.7

Code 

This code might be looking like big. But concept wise it is very easy. Please go through this code.

#include<reg51.h>

#define lcd P2

sbit rs=P3^5;
sbit rw=P3^6;
sbit en=P3^7;

sbit r1=P1^0;
sbit r2=P1^1;
sbit r3=P1^2;
sbit r4=P1^3;
sbit c1=P1^4;
sbit c2=P1^5;
sbit c3=P1^6;
sbit c4=P1^7;

                  
void lcd_init();
void cmd(unsigned char );
void dat(unsigned char );
lcd_string(unsigned char *);
void delay(unsigned int );
void keypad(void);


void main()
{
    lcd_init();
    while(1) {
        cmd(0x80);
        lcd_string("Enter the key:");
        cmd(0xc7);
        keypad();
    }
}

void keypad()
{
    c1=c2=c3=c4=1;
    r1=0;r2=1;r3=1;r4=1;
    if(c1==0){
        while(c1==0);
        dat('7');
    } else if(c2==0) {
        while(c2==0);
        dat('8');
    } else if(c3==0) {
        while(c3==0);
        dat('9');
    } else if(c4==0) {
        while(c4==0);
        dat('/');
    }

    r1=1;r2=0;r3=1;r4=1;
    if(c1==0){
        while(c1==0);
        dat('4');
    } else if(c2==0) {
        while(c2==0);
        dat('5');
    } else if(c3==0) {
        while(c3==0);
        dat('6');
    } else if(c4==0) {
        while(c4==0);
        dat('*');
    }

    r1=1;r2=1;r3=0;r4=1;
    if(c1==0){
        while(c1==0);
        dat('1');
    } else if(c2==0) {
        while(c2==0);
        dat('2');
    } else if(c3==0) {
        while(c3==0);
        dat('3');
    } else if(c4==0) {
        while(c4==0);
        dat('-');
    }

    r1=1;r2=1;r3=1;r4=0;
    if(c1==0){
        while(c1==0);
        cmd(0x01);
    } else if(c2==0) {
        while(c2==0);
        dat('0');
    } else if(c3==0) {
        while(c3==0);
        dat('=');
    } else if(c4==0) {
        while(c4==0);
        dat('+');
    }
}


void lcd_init()
{
        cmd(0x38);
        cmd(0x0e);
        cmd(0x06);
        cmd(0x01);
}

void cmd(unsigned char x)
{
        lcd=x;
        rs=0;
        rw=0;
        en=1;
        delay(1000);
        en=0;
}

void dat(unsigned char y)
{
        lcd=y;
        rs=1;
        rw=0;
        en=1;
        delay(1000);
        en=0;
}

lcd_string(unsigned char *s)
{
    while(*s)
        dat(*s++);
}

void delay(unsigned int z)
{
  unsigned int i;
  for(i=0;i<=z;i++);
}

Code Explanation

I assumed that you already know about the LCD interfacing.  Now look this line in keypad function,

      c1=c2=c3=c4=1;
    r1=0;r2=1;r3=1;r4=1;
    if(c1==0){
        while(c1==0);
        dat('7');
    } else if(c2==0) {
        while(c2==0);
        dat('8');
    } else if(c3==0) {
        while(c3==0);
        dat('9');
    } else if(c4==0) {
        while(c4==0);
        dat('/');
    }

In this code i’m taking row as a output and column as a input. 

  1. In first line I’m assigning high to all column. (c1=c2=c3=c4=1;)
  2. Then i’m assigning first row to zero and keeps remaining row as high. (r1=0;r2=1;r3=1;r4=1;)
  3. Then i’m checking first column is zero or not. If it is zero then i should wait until that button depressed. Then i can know the pressed key.
  4. If not i’m checking next column. Like that i’m checking all rows and column.
  5. If no keys pressed in the row1, then i’m making row2 as zero. Remaining rows are high. Then follow the above steps.

Output

//embetronicx.com/wp-content/uploads/2017/07/relay-working-uln2803.gif

3×4 Matrix Keypad Interfacing

Circuit Diagram

LCD

  • RS  –  P3.5
  • RW  – P3.6
  • EN  –  P3.7
  • Data Lines – P2

Keypad

  • R1 – P1.0
  • R2 – P1.1
  • R3 – P1.2
  • R4 – P1.3
  • C1 – P1.4
  • C2 – P1.5
  • C3 – P1.6

Code

#include<reg51.h>

#define lcd P2

sbit rs=P3^5;
sbit rw=P3^6;
sbit en=P3^7;

sbit r1=P1^0;
sbit r2=P1^1;
sbit r3=P1^2;
sbit r4=P1^3;
sbit c1=P1^4;
sbit c2=P1^5;
sbit c3=P1^6;

                  
void lcd_init();
void cmd(unsigned char );
void dat(unsigned char );
lcd_string(unsigned char *);
void delay(unsigned int );
void keypad(void);


void main()
{
    lcd_init();
    while(1) {
        cmd(0x80);
        lcd_string("Enter the key:");
        cmd(0xc7);
        keypad();
    }
}

void keypad()
{
    c1=c2=c3=1;
    r1=0;r2=1;r3=1;r4=1;
    if(c1==0){
        while(c1==0);
        dat('1');
    } else if(c2==0) {
        while(c2==0);
        dat('2');
    } else if(c3==0) {
        while(c3==0);
        dat('3');
    }

    r1=1;r2=0;r3=1;r4=1;
    if(c1==0){
        while(c1==0);
        dat('4');
    } else if(c2==0) {
        while(c2==0);
        dat('5');
    } else if(c3==0) {
        while(c3==0);
        dat('6');
    }

    r1=1;r2=1;r3=0;r4=1;
    if(c1==0){
        while(c1==0);
        dat('7');
    } else if(c2==0) {
        while(c2==0);
        dat('8');
    } else if(c3==0) {
        while(c3==0);
        dat('9');
    }

    r1=1;r2=1;r3=1;r4=0;
    if(c1==0){
        while(c1==0);
        dat('*');
    } else if(c2==0) {
        while(c2==0);
        dat('0');
    } else if(c3==0) {
        while(c3==0);
        dat('#');
    }
}


void lcd_init()
{
        cmd(0x38);
        cmd(0x0e);
        cmd(0x06);
        cmd(0x01);
}

void cmd(unsigned char x)
{
        lcd=x;
        rs=0;
        rw=0;
        en=1;
        delay(1000);
        en=0;
}

void dat(unsigned char y)
{
        lcd=y;
        rs=1;
        rw=0;
        en=1;
        delay(1000);
        en=0;
}

lcd_string(unsigned char *s)
{
    while(*s)
        dat(*s++);
}

void delay(unsigned int z)
{
  unsigned int i;
  for(i=0;i<=z;i++);
}

Code Explanation

Same as 4×4 Matrix keypad.

Output

[ Please find the output image Here ]

Tasks

Please try these tasks. If you have any doubt please ask us by commenting below.

  1. Whatever i’m pressing in the Keypad that character should displayed in the Serial terminal.
  2. Connect 8 LEDs into Port 0. If i press 0, No LEDs will be ON. If i press 1, 1 LED should be ON. If i press 2, 2 LEDs should ON. Like that upto 8 numbers you write the program.
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
1
0
Would love your thoughts, please comment.x
Ads Blocker Image Powered by Code Help Pro
Ad Blocker Detected!!!

Dear User,

Thanks for visiting the site. Unfortunately, we have noticed that you are using an Ad Blocker extension in your browser.

We charge advertisers instead of the audience. We depend on Ad revenue to keep creating quality content for you. We guarantee clean ads. 

HERE IS HOW YOU CAN KEEP READING!

Click the AdBlock icon on your browser and click Enabled on this site to Disable AdBlock in embetronicx.com or whitelist our website.

If you still want to hide the ads, please become a Premium member. If you are a premium member already, then please log in.

Thank you for understanding.

If you are seeing this even after disabling all the ad blockers, Please report us the False Positive to [email protected]

Refresh