# Programming LCD to 8051

#### Parth786

Joined Jun 19, 2017
642
The 16×2 LCD has command register is use to hold the commands. The 16×2 LCD module has a set of commands each meant for doing a particular job with the display.

http://pdf1.alldatasheet.com/datasheet-pdf/view/127934/ETC1/JHD162A.html

Initializing LCD:
Basic command to initialize LCD to the 8051

The common steps needs for initializing the LCD display
• Send 38H to the 8 bit data line for initialization
• Send 0FH for making LCD ON, cursor ON and cursor blinking ON.
• Send 06H for incrementing cursor position.
• Send 01H for clearing the display and return the cursor.
C:
void LCD_initializing(void)
{
LCD_Command(0x38);
Delay(20);
LCD_Command(0x0f);
Delay(20);
LCD_Command(0x01);
Delay(20);
LCD_Command(0x81);
Delay(20);
}
I don't understand two things. Why do we need to give delay between two commands?. How much delay need to give ?

#### Parth786

Joined Jun 19, 2017
642
it's in the data sheet
Sorry but I am not getting exact time in datasheet there are different time values I think minimum is 20 ms in page number 3 and there is not brief explanation for delay. I want to understand why we give delay between two commands.

#### Raymond Genovese

Joined Mar 5, 2016
1,653
Sorry but I am not getting exact time in datasheet there are different time values I think minimum is 20 ms in page number 3 and there is not brief explanation for delay. I want to understand why we give delay between two commands.
When you are sending commands to the LCD, you are actually sending commands to another microprocessor that is controlling the LCD.

These commands take time to execute. For example,"Send 01H for clearing the display and return the cursor."

While the LCD controller is executing a command like that, you (the 8051) has to wait or subsequent commands will not be accepted and executed by the LCD controller.

I don't know from memory about the JHD162A but it probably uses some variant of the HD44780. A more thorough data sheet on the controller itself will typically provide minimum wait times for various commands. I think that you are seeing those minimum times represented in the software fragments/examples.

Take a look at table 6 in this PDF for the HD4478 as an example.

Make sense?

Last edited:

#### be80be

Joined Jul 5, 2008
2,069
It is like the HD44780
The first link i posted has code for it and list the time needed.
Code:
Example
#include <reg51.h>
#include <intrins.h>
LCD 的
21*/
RS
sbit rw=0xa1;
/*P2.1 LCD 的R/W 22*/
LCD 的
25*/
sbit cs=0xa4;
/*P2.4
E
sfr lcdbus=0x80;
/*p0LCD 数据 D0=P0.0*/
unsigned int sys10mscounter;
unsigned char syslimitcounter;
char path1[8]={0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f};/*自定义符号
sbit dc=0xa0;
/*P2.0
char path2[8]={0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00};/*自定义符号
char pats1[8]={0x15,0x15,0x15,0x15,0x15,0x15,0x15,0x15};/*自定义符号
char pats2[8]={0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a};/*自定义符号

2*/

2*/
void soft_nop(){}
void soft_10ms()/***********12MHZ 提供10MS 软件延时************/
{
register int i;
for(i=0;i<711;i++);
}
void soft_20ms()/***********12MHZ 提供20MS 软件延时************/
{
soft_10ms();
soft_10ms();
}
void hard_10ms(unsigned int delaytime) /*基于10MS 的硬件延时*/
{
sys10mscounter=delaytime;
while(sys10mscounter);
}
unsigned char data lcdcounter;
bit lcdusing1,lcdusing2;
bit lcd_checkbusy()/*检查LCD 忙*/
{
register lcdstate;
dc=0;
/*dc=1为数据,=0 为命令.*/
rw=1;
/*rw=1为读,=0 为写.*/
cs=1;
/*cs=1选通.*/
soft_nop();
lcdstate=lcdbus;
cs=0;
return((bit)(lcdstate&0x80));
}
void lcd_wrcmd(unsigned char lcdcmd) /*写LCD 命令*/
{
lcdusing1=1;
while(lcd_checkbusy());
lcdbus=lcdcmd;
dc=0;
/*dc=1为数据,=0 为命令.*/
rw=0;
cs=1;
soft_nop();
/*rw=1为读,=0 为写.*/
/*cs=1选通.*/
cs=0;
lcdbus=0xff;
lcdusing1=0;
}
void lcd_moveto(char position) /*移动光标到指定位.0-79*/
{ register cmd=0x80;
lcdcounter=position;
if (position > 59)
position += 0x18;
else
{
if (position > 39)position -= 0x14;
else
{
if (position > 19)position += 0x2c;
}
}
cmd=cmd|position;
lcd_wrcmd(cmd); } void lcd_wrdata(char lcddata) /*在当前显示位置显示数据*/ { char i;
lcdusing2=1;
while(lcd_checkbusy());
if(lcdcounter==20){
lcd_moveto(20);
while(lcd_checkbusy());
}
if(lcdcounter==40){
lcd_moveto(40);
while(lcd_checkbusy());
}
if(lcdcounter==60){
lcd_moveto(60);
while(lcd_checkbusy());
}
if(lcdcounter==80){
lcd_moveto(0);
while(lcd_checkbusy());
lcdcounter=0;
} /*为通用而如此*/
lcdcounter++;
lcdbus=lcddata;
dc=1; /*dc=1为数据,=0 为命令.*/
rw=0; /*rw=1为读,=0 为写.*/
cs=1; /*cs=1选通.*/
soft_nop();
cs=0;
lcdbus=0xff;
lcdusing2=0; } void lcd_string(char *strpoint) /*在当前显示位置显示LCD 字符串*/
{ register i=0;
while(strpoint[i]!=0){
cd_wrdata(strpoint[i]);
i++;
}
} void lcd_init()/*初始化*/
{
lcd_wrcmd(0x38);
lcd_wrcmd(0x0c);
lcd_wrcmd(0x06);
lcd_wrcmd(0x01);
lcdcounter=0;
/*设置8 位格式,2 行,5*7*/
/*整体显示,关光标,不闪烁*/
/*设定输入方式,增量不移位*/
/*清除显示*/
}
void lcd_cls()/*清除显示*/ { lcd_wrcmd(0x01);
lcdcounter=0; } void timer0(void) interrupt 1 /*T0 中断*/ { TH0=0xd8; /*12M,10ms*/
TL0=0xf6;
TR0=1;
if(sys10mscounter!=0)sys10mscounter--; /*定时器10ms*/
if(syslimitcounter!=0)syslimitcounter--; /*定时器10ms*/
}
main()
{
unsigned char j;
IE=0;P0=0xff;P1=0xff;P2=0xff;P3=0xff; /*初始化T*/
lcd_init();soft_20ms();
TMOD=0x51;
TH0=0xd8; /*12M,10ms*/
TL0=0xf6;
TR0=1;ET0=1;EA=1;
while(1)
{
/*全黑横一横二竖一竖二U Q ABCD... */
lcd_init(); /*全黑*/
for(j=0;j<80;j++){lcd_wrdata(0xff);}
hard_10ms(50);
lcd_init(); /*横一可参考自行设计符号*/
lcd_wrcmd(0x40);
for(j=0;j<8;j++)lcd_wrdata(path1[j]);
for(j=0;j<100;j++)lcd_wrdata(0);
hard_10ms(50);
lcd_init(); /*横二*/
lcd_wrcmd(0x40);
for(j=0;j<8;j++)lcd_wrdata(path2[j]);
for(j=0;j<100;j++)lcd_wrdata(0);
hard_10ms(50);
lcd_init(); /*竖一*/
lcd_wrcmd(0x40);
for(j=0;j<8;j++)lcd_wrdata(pats1[j]);
for(j=0;j<100;j++)lcd_wrdata(0);
hard_10ms(50);
lcd_init(); /*竖二*/
lcd_wrcmd(0x40);
for(j=0;j<8;j++)lcd_wrdata(pats2[j]);
for(j=0;j<100;j++)lcd_wrdata(0);
hard_10ms(50);
lcd_init();
lcd_string("UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
UUUUUUUUUUUUUUUUUUUUUUUU
UUUUU"); hard_10ms(50); lcd_init();
lcd_string("QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ
QQQQQQQQQQQQQQQQQQQQQQQQ
QQQQQ"); hard_10ms(50); lcd_init();
lcd_string("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwx
yz0123456789+-!
#\$%&?"); hard_10ms(50); }
}
Think I got it all

Last edited:

#### be80be

Joined Jul 5, 2008
2,069
I did it from scratch for a 16f505 lot's of fun there I just used the datasheet took me week to get it right but I was learning xc8.
And mplab-x

#### Parth786

Joined Jun 19, 2017
642
these are basics steps
• STEP1: Initialization of LCD.
• STEP2: Sending command to LCD.
• STEP3: Writing the data to LCD.
C:
/* Function to send command instruction to LCD */
void LCD_Command(unsigned char cmd)
{
port = cmd;
RS = 0;
RW = 0;
EN = 1;
Delay(2);
EN = 0;
}

/*Function to send display dato LCD */
void LCD_Data(unsigned char Data)
{
port = Data;
RS = 1;
RW = 0;
EN = 1;
Delay(2);
EN = 0;
}

/* function for delay */
void Delay(unsigned int wait)
{
unsigned i , j ;
for( i = 0; i < wait; i++ )
for( j = 0; j < 1200; j++);
}

/* Function to prepare the LCD */
void LCD_init()
{
LCD_Command(0x38);
Delay(20);
LCD_Command(0x0f);
Delay(20);
LCD_Command(0x01);
Delay(20);
LCD_Command(0x81);
Delay(20);
}
I have still doubt on delay.I am not finding exact reason to use of delay

#### Raymond Genovese

Joined Mar 5, 2016
1,653
It is like the HD44780
The first link i posted has code for it and list the time needed./--/
Sorry, but I disagree. The data sheet that you linked to is the same document as the datasheet linked to by the TS. I am not referring to the AC electrical characteristics (Table 12 in either of the two links).

I am referring to the controller's instruction execution times, again, as given in table 6 of the HD4478 document that I linked.

Maybe, I missed it but I do not see any controller instruction execution times in the document that the TS linked or that you linked.

That was my point and integral to the explanation.

#### be80be

Joined Jul 5, 2008
2,069
Nope you talking about the same thing I posted pic of the timing
but i posted the wrong one i fixed it
Raymond Genovese thanks for pointing that out.
JHD162A data sheet just a little different
But the HD4478 will work with it from what google said LOL
The data sheet for HD4478 is easier to read

Last edited:

#### Parth786

Joined Jun 19, 2017
642
I am struggling in embedded program. I can write simple array string program but I am having problem in embedded program with basics
C:
#include<stdio.h>
int main (void)
{
int i;
char array[15] =  {"Hello Friends"};
for(i = 0; i < 15;  i++)
{
printf("print array element : %c \n", array[i]);
}
return 0;

}
I get following output of this program
print array element : H
print array element : e
print array element : l
print array element : l
print array element : o
print array element :
print array element : F
print array element : r
print array element : i
print array element : e
print array element : n
print array element : d
print array element : s
print array element :
print array element :

I want to use same basic knowledge in embedded c programming
C:
#include<reg51.h>

#define port P1           /* Data pins connected to port P1 */

sbit RS = P2^0;           /* RS pin connected to pin 0 of port P2 */
sbit RW = P2^1;           /* RW pin connected to pin 1 of port P2 */
sbit EN = P2^2;           /* EN pin connected to pin 2 of port P2 */

/* Function to send command to LCD */
void LCD_Command(unsigned char cmd)
{
port = cmd;
RS = 0;
RW = 0;
EN = 1;
Delay(2);
EN = 0;
}
/*Function to send data to LCD */
void LCD_Data(unsigned char Data)
{
port = Data;
RS = 1;
RW = 0;
EN = 1;
Delay(2);
EN = 0;
}

/* function for delay */
void Delay(unsigned int wait)
{
unsigned i,j ;
for(i = 0; i < wait; i++)
for(j = 0; j < 1200; j++);
}

/* Function to prepare the LCD */
void LCD_init()
{
LCD_Command(0x38);
Delay(20);
LCD_Command(0x0f);
Delay(20);
LCD_Command(0x01);
Delay(20);
LCD_Command(0x81);
Delay(20);
}

void string (void)
{
unsigned int i=0;

unsigned char string[15]= "Hello Friend's";

for(i=0; i<15; i++)
{

}

}
void main(void)
{
LCD_init();
while (1)
{
string ();
}
}
Have you noticed that in my embedded program I am facing problem in string function. I don't understand how to make this function for proper working. so please help me

Last edited:

#### Raymond Genovese

Joined Mar 5, 2016
1,653
I am struggling in embedded program. I can write simple array string program but I am having problem in embedded program with basics
/---/

Have you noticed that in my embedded program I am facing problem in string function. I don't understand how to make this function for proper working. so please help me
@Parth786 could you at least acknowledge the responses referencing your questions "I don't understand two things. Why do we need to give delay between two commands?. How much delay need to give ?" in your original post?

What I am noticing is that you seem to be jumping into the next thing without so much as a peep with regard to your question and the attempts at answering your question. Not sure, but maybe that basic way of responding (or skipping the responding) could be adding to you "struggles" - yes?

#### Parth786

Joined Jun 19, 2017
642
What I am noticing is that you seem to be jumping into the next thing without so much as a peep with regard to your question and the attempts at answering your question. Not sure, but maybe that basic way of responding (or skipping the responding) could be adding to you "struggles" - yes?
@Raymond Genovese
When a first topic completed then I started another I wanted to write a program for lcd from the beginning.But I had doubt on delay.I got the answer in your post #4 that command need some time to execute. so I didn't go in too deep and I started new question

Post #7 and Post #10 are more important for me then original question. I think these can be clear more basics. I have mention both program c and embedded c and I was waiting for further response

#### Raymond Genovese

Joined Mar 5, 2016
1,653
@Raymond Genovese
When a first topic completed then I started another I wanted to write a program for lcd from the beginning.But I had doubt on delay.I got the answer in your post #4 that command need some time to execute. so I didn't go in too deep and I started new question

Post #7 and Post #10 are more important for me then original question. I think these can be clear more basics. I have mention both program c and embedded c and I was waiting for further response
OK fair enough and I am glad that you acknowledged the efforts of folks to answer the question that you originally asked.

Your post #7 still asks about the delays, so that is still confusing as to whether you are understanding that your last line in post #7 is
"I have still doubt on delay.I am not finding exact reason to use of delay"

I don't understand what you are actually asking about in post #10 - should it be in another thread? Could you be a little clearer about your question in post #10? Do you not want to send a LF or CR/LF after every character printed?

If that is your question, then take a look at your printf statement and remove the '/n' in:
printf("print array element : %c \n", array);
and see what the output looks like.