How to interface LCD and KEYPAD-PHONE with 8051

Posted in: embedded by musiitwa joseph 2016-01-14 at 03:42:07

image for How to interface LCD and KEYPAD-PHONE with 8051

The 8051 microcontroller has a LCD connected to it and a keypad. When a key is pressed on the keypad, the number is displayed on the LCD but in words.

The Keypad has fours rows and three columns. Each row is first activate by sending a zero and then each column is checked. An intersection of a zero on a row and column determines the number which has been pressed.

The 16x2 LCD has 2 lines, 16 characters can be displayed on each row at time make a total of 32 characters. Each character is displayed in a matrix of 5x7 pixels. It has the following pins:

  • VCC - ground pin
  • VDD - power supply
  • VEE - control brightness
  • E - enable pin used for controlling the signal
  • R/W - read/write
  • RS - Register Select
  • D0 - D7 - data pins

To send commands R/W should be zero and RS should zero (R/W = 0, RS = 0) and to send data R/W should be zero and RS should one (R/W = 0, RS = 1)

#include<reg51.h>
//----------LCD    PINS ---------------
sbit en = P1^0;
sbit rw = P1^1;
sbit rs = P1^2;

//----------KEYPAD PINS -----------
sbit r1 = P2^0;
sbit r2 = P2^1;
sbit r3 = P2^2;
sbit r4 = P2^3;
sbit c1 = P2^4;
sbit c2 = P2^5;
sbit c3 = P2^6;

//-----function calls
void delay();
void enable();
void send_commands();
void display(char *word_ptr);
void set_line(int line);
void pressed(char *str_ptr);

void main(){
    en =0;     //enable
    rs = 0;    //register select
    rw = 0;    //read write

    send_commands();        
    set_line(1);            
    display("Press any button");

    while(1){
    
        r1 = r2 = r3 = r4 = c1 = c2 = c3 = 1;

        r1=0;
        r2=r3=r4=1;        

        if(c1 == 0){
            pressed("One");
        }else if(c2 == 0){
             pressed("Two");
        }else if(c3 == 0){
            pressed("Three");
        }

        r2=0;
        r1=r3=r4=1;

        if(c1 == 0){
            pressed("Four");
        }else if(c2 == 0){
            pressed("Five");
        }else if(c3 == 0){
            pressed("Six");
        }

        r3=0;
        r1=r2=r4=1;

        if(c1 == 0){
            pressed("Seven");
        }else if(c2 == 0){
            pressed("Eight");
        }else if(c3 == 0){
            pressed("Nine");
        }

        r4=0;
        r1=r2=r3=1;

        if(c1 == 0){
            pressed("Star");
        }else if(c2 == 0){
            pressed("Zero");
        }else if(c3 == 0){
            pressed("Hash");
        }
    }

}

/*---------- function for delaying -----------*/
void delay(){
int i;
  for(i=0;i<10000;i++);

}

/*------- function for latching ---------------*/
void enable(){
   en = 1;
   delay();
   en = 0;
   delay();
}

/*-------- function for sending commands ------*/
void send_commands(){
    rw = 0, rs = 0; //sending commands
    enable();
    P3 = 0x38; //set 8bit, 2 lines and character font
    enable();
    P3= 0x0E; //turns on display and set a solid cursor
    enable();
    P3 = 0x06; //cursor increment
    enable();
    P3= 0x01; //clear the screen
    enable();
}

/*------------- function to set line --------------*/
void set_line(int line){
    rs=0, rw=0;
    
      if(line == 1){
        P3=0x80;//line one first position
    }else{
        P3=0xC0; //line two first position
    }
    enable();
}

/*-------- function for displaying a string ------*/
void display(char *word_ptr){
    rw = 0, rs = 1; //sending data
    while(*word_ptr){
           P3 = *word_ptr++;
        enable();
    }
}

/*---------- function for pressed button ------*/
void pressed(char *str_ptr){
       send_commands();
    set_line(1);
    display("You Pressed:");
    set_line(2);
    display(str_ptr);
}

Tags:
c

Search

Comments(1)


Nice

from: Kato J.B at: 2016-01-19 , 12:52:15 AM


Leave a comment

Fields with * are required
Your email address will not be displayed
3 + 5 =