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 / Replies / here’s working example of

here’s working example of

|

Microcontroller › 8051 › CHECK OUT MY C PROGRAM ON LCD AS THERE IS LITTLE MISTAKE › here’s working example of

April 11, 2011 at 9:46 pm #5945
romel emperado
Participant

here’s working example of code I am using..

 

 

// 4 bit example

#include<reg51.h>

#define LCD P1
#define LCD_EN        0x80
#define LCD_RS        0x20
                    
//LCD Commands       

#define LCD_SETMODE       0x04
#define LCD_SETVISIBLE       0x08
#define LCD_SETFUNCTION       0x28
#define LCD_SETDDADDR       0x80

void delayms(unsigned char);
void delayus(unsigned char);
void lcd_init();
void lcd_reset();
void lcd_cmd (char);
void lcd_data(unsigned char);
void lcd_str (unsigned char*);

void  main (void)
{
lcd_init();
lcd_cmd(0x80);            // ist line address
lcd_str(”     i love  “);
lcd_cmd(0xC0);              // 2nd line address
lcd_str(“romel”);
while(1);
}

void delayus(unsigned char delay){
    while(delay–);
}

void delayms(unsigned char delay){
    while(delay–)
        delayus(149);
}

void lcd_reset()
{
    LCD = 0xFF;
    delayms(40);
    LCD = 0x03+LCD_EN;
    LCD = 0x03;
    delayms(40);
    LCD = 0x03+LCD_EN;
    LCD = 0x03;
    delayms(5);
    LCD = 0x03+LCD_EN;
    LCD = 0x03;
    delayms(5);
    LCD = 0x02+LCD_EN;
    LCD = 0x02;
    delayms(5);
}

void lcd_init ()
{
    lcd_reset();
    lcd_cmd(LCD_SETFUNCTION);                    // 4-bit mode – 1 line – 5×7 font.
    lcd_cmd(LCD_SETVISIBLE+0x04);                // Display no cursor – no blink.
    lcd_cmd(LCD_SETMODE+0x02);                   // Automatic Increment – No Display shift.
    lcd_cmd(LCD_SETDDADDR);                      // Address DDRAM with 0 offset 80h.
 }

 void lcd_cmd (char cmd)
{
    LCD = ((cmd >> 4) & 0x0F)|LCD_EN;
    LCD = ((cmd >> 4) & 0x0F);

    LCD = (cmd & 0x0F)|LCD_EN;
    LCD = (cmd & 0x0F);

    delayus(250);
    delayus(250);
}

void lcd_data (unsigned char dat)
{
    LCD = (((dat >> 4) & 0x0F)|LCD_EN|LCD_RS);
    LCD = (((dat >> 4) & 0x0F)|LCD_RS);
   
    LCD = ((dat & 0x0F)|LCD_EN|LCD_RS);
    LCD = ((dat & 0x0F)|LCD_RS);

    delayus(250);
    delayus(250);
}

void lcd_str (unsigned char *str)
{
    while(*str){
        lcd_data(*str++);
    }
}

RSS Recent Posts

  • Phone Charger 5v to 12v May 14, 2026
  • Difference between TTL, RS232 and RS485 May 14, 2026
  • Wall Radiator Heating: Efficiency and Placement Questions May 14, 2026
  • Assistance locating a 'trail' camera gadget, please ? May 14, 2026
  • Projector focus circuit May 14, 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