EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / Microcontroller Lcd display with keypad

Microcontroller Lcd display with keypad

|

Microcontroller › 8051 › Microcontroller Lcd display with keypad

  • This topic has 0 replies, 1 voice, and was last updated 14 years, 2 months ago by lgy.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • January 5, 2012 at 6:13 pm #1761
    lgy
    Participant

    Hi i need help on my Lcd and keypad . I have a 4X4 keypad and a lcd .

     

    Please help as Im struggling on the codes .Im able to display the keys to the lcd screen but i wanna let the user key in 4 digit and an alphabet ( the alphabet will act as a Enter ) and it will store the “passcode”.

     

    this is my code for now .

     

    #include <p18F4550.h>
    #include <delays.h>
    #include “lcd.h” // Include file is located in the project directory
     
    // Include this when using Bootloader Program ================================
    #pragma udata
     
    extern void _startup (void);        // See c018i.c in your C18 compiler dir
    #pragma code _RESET_INTERRUPT_VECTOR = 0x000800
    void _reset (void)
    {
        _asm goto _startup _endasm
    }
    #pragma code
     
    #pragma code _HIGH_INTERRUPT_VECTOR = 0x000808
    void _high_ISR (void)
    {
        ;
    }
     
    #pragma code _LOW_INTERRUPT_VECTOR = 0x000818
    void _low_ISR (void)
    {
        ;
    }
    #pragma code
     
    #pragma code
     
    // additional codes ends here ===============================================================
     
    // Your program declarations start here:====
     
    #define LCD_RS PORTDbits.RD6    //  Register Select on LCD
    #define LCD_EN PORTDbits.RD4    //  Enable on LCD controller
    #define LCD_WR PORTDbits.RD5    //  Write on LCD controller
     
    #define KEY_DA PORTBbits.RB5   //  74922 DA output
    #define KEY_PORT PORTB // RB3 to RB0 has keypad data
     
    unsigned char key,msgindex,outchar,lcdindex;
    char Message1 [ ] = “Enter PIN number :  “;  // Defining a 20 char string
     
    //— Function for writing a command byte to the LCD in 4 bit mode


     
    void lcd_write_cmd(signed char cmd)
    {
        unsigned char temp2;
        LCD_RS = 0; // Select LCD for command mode
        Delay10TCYx(4); // 40us delay for LCD to settle down
        temp2 = cmd;
        temp2 = temp2 >> 4; // Output upper 4 bits, by shifting out lower 4 bits
        PORTD = temp2 & 0x0F; // Output to PORTD which is connected to LCD
     
        Delay1KTCYx(10); // 10ms -Delay at least 1 ms before strobing
        lcd_strobe();
     
    Delay1KTCYx(10); // 10ms – Delay at least 1 ms after strobing
     
        temp2 = cmd; // Re-initialise temp2
        PORTD = temp2 & 0x0F; // Mask out upper 4 bits
     
        Delay1KTCYx(10); // 10ms – Delay at least 1 ms before strobing
        lcd_strobe();
     
        Delay1KTCYx(10); // 10ms – Delay at least 1 ms before strobing
     
    }
     
     
    //—- Function to write a character data to the LCD


     
    void lcd_write_data(char data)
    {
        char temp1;
     
        LCD_RS = 1; // Select LCD for data mode
        Delay10TCYx(4); // 40us delay for LCD to settle down
     
        temp1 = data;
        temp1 = temp1 >> 4;
        PORTD = temp1 & 0x0F;
        Delay1KTCYx(10);
    LCD_RS = 1;
        Delay1KTCYx(10); //_-_ strobe data in
     
        lcd_strobe();
        Delay1KTCYx(10);
     
        temp1 = data;
        PORTD = temp1 & 0x0F;
    Delay1KTCYx(10);
        LCD_RS = 1;
        Delay1KTCYx(10); //_-_ strobe data in
     
        lcd_strobe();
        Delay1KTCYx(10);
     
    }
     
     
    //– Function to generate the strobe signal for command and character


     
    void lcd_strobe(void) // Generate the E pulse
    {
     
        LCD_EN = 1; // E = 0
        Delay1KTCYx(1); // 1ms delay for LCD_EN to settle
          LCD_EN = 0; // E = 1
        Delay1KTCYx(1); // 1ms delay for LCD_EN to settle
     
    }
     
     
     
    //—- Function to initialise LCD module


    void lcd_init(void)
    {
        TRISD = 0x00;
        PORTD = 0x00; // PORTD is connected to LCD data pin
        LCD_EN = 0;
        LCD_RS = 0; // Select LCD for command mode
        LCD_WR = 0; // Select LCD for write mode
     
        Delay10KTCYx(250); // Delay a total of 1 s for LCD module to
        Delay10KTCYx(250); //
        Delay10KTCYx(250); //
        Delay10KTCYx(250); // finish its own internal initialisation
     
        /* The data sheets warn that the LCD module may fail to initialise properly when
           power is first applied. This is particularly likely if the Vdd
           supply does not rise to its correct operating voltage quickly enough.
     
           It is recommended that after power is applied, a command sequence of
           3 bytes of 3xh be sent to the module. This will ensure that the module is in
           8-bit mode and is properly initialised. Following this, the LCD module can be
           switched to 4-bit mode.
        */
     
        lcd_write_cmd(0x33);
        lcd_write_cmd(0x32);
     
        lcd_write_cmd(0x28); // 001010xx ?Function Set instruction
        // DL=0 :4-bit interface,N=1 :2 lines,F=0 :5×7 dots
     
        lcd_write_cmd(0x0E); // 00001110 ?Display On/Off Control instruction
        // D=1 :Display on,C=1 :Cursor on,B=0 :Cursor Blink on
     
        lcd_write_cmd(0x06); // 00000110 ?Entry Mode Set instruction
        // I/D=1 :Increment Cursor position
        // S=0 : No display shift
     
        lcd_write_cmd(0x01); // 00000001 Clear Display instruction
     
        Delay10KTCYx(20); // 20 ms delay
     
    }
     
    //


    Function to obtained wait for key press and returns its ASCII value
     
    char getkey(void)
    { char keycode;
        const unsigned char lookup[] = “123F456E789DA0BC “;
     
        while (KEY_DA==0); //wait for key to be pressed
        keycode=KEY_PORT &0x0F; //read from encoder at portB,mask upper 4 bits
     
        while (KEY_DA==1); //wait for key to be released
        return(lookup[keycode]); //convert keycode to its ascii value for LCD
    }
     
    // —- Main Program


     
    void main(void)
    {
    // Do not remove these as well=============
    ADCON1 = 0x0F; // Configure PORTA to be digital I/O
    CMCON = 0x07;
    // ========================================
    // Your MAIN program Starts here: =========
     
    lcd_init(); // Initialise LCD module
     
    LCD_RS = 1; // Select LCD for character data mode
    Delay1KTCYx(1); // 1 ms delay
     
    while(1)
    {
    lcd_write_cmd(0x80); // Move cursor to line 1 position 1
     
    for (msgindex = 0; msgindex < 20; msgindex++) //for 20 char LCD module
    {
    outchar = Message1[msgindex];
     
    lcd_write_data(outchar); // write character data to LCD
    }
     
      
      lcd_write_cmd(0xC0); // Move cursor to line 2 position 1
     
    for (lcdindex = 0; lcdindex < 20; lcdindex++) //for 20 number
        { 
    key=getkey(); // waits and get an ascii key number when pressed
     
           lcd_write_data(key); //display on LCD
     
        }
     
    lcd_write_cmd(0x01); // 00000001 Clear Display instruction
     
    Delay1KTCYx(250); // Delay for 1 s before proceeding to repeat
    Delay1KTCYx(250);
    Delay1KTCYx(250);
    Delay1KTCYx(250);
     
    }
    }
     
     
    Im sorry if its too long and messy as this is my first post in this forum . Your help will be much appreciated.
  • Author
    Posts
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • cpap change over circuit March 14, 2026
  • Fender rumble 100 base amp, power, light and overdrive light flash repeatedly when power turned on no output March 13, 2026
  • Phone to op amp circuit? March 13, 2026
  • Arduino standalone minimal March 13, 2026
  • Project boxes March 13, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise