Projects › Projects › About LCD 2×16 › hey gana.. reffer to this
hey gana..
reffer to this sample code.. it’s working.. I’ve using this code of my projects.. this is 4bit mode
// 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 emperado”);
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++);
}
}