# having problem in Interfacing lcd hardware to 8051 kit

Thread Starter

#### bobparihar

Joined Jul 31, 2014
93
i have a 8051 developing kit from robosoft labs which have different interfacing units like led's, 7seg, lcd,motor etc..
when i connect lcd (16*2) to the kit it dosent getting on.. lcd shows no light
here is the code

Rich (BB code):
#include<reg51.h>
sbit rs=P1^0;
sbit rw=P1^1;
sbit en=P1^2;
unsigned int i,j;
void dat(unsigned char);     //data fxn
void cmd(unsigned char);   // command fxn
void main()
{
cmd(0x38);  // display on cursor on
cmd(0x0E);    // display on cursor blinking
cmd(0x06);   // increment cursor to right
cmd(0x01);  // clear display screen
cmd(0x80);  //  force cursor to start from first line first position
dat('h');    // data to data pins from p3

while(1);   //hold
}

void cmd(unsigned char x)
{
P3=x;
rs=0;     //indicating  command
rw=0;
en=1;
for(i=0;i<5000;i++);   //delay
en=0;
}
void dat(unsigned char y)
{
P3=y;
rs=1;   // indicating data
rw=0;
en=1;
for(i=0;i<5000;i++);
en=0;
}

i have connected all data pins and control pins(rs, rw,en) to their correct position.. but still im not gettiong the lcd aty on position.. so i connected vcc and gnd to the lcd pins externally.. still not getting lcd on.. and i dont think that lcd is not working because i did replace 5 lcd with same procedure

plz help

Last edited by a moderator:

#### shteii01

Joined Feb 19, 2010
4,644
A couple of problems:
* no delays between the cmd()
* no returns in user defined functions

#### ErnieM

Joined Apr 24, 2011
8,294
First off, there needs to be a long delay (10mS) on power up before using the device.

Next, each command needs 37 uS delay beofer the next, with a few exceptions (like clear or home) that need 1.52 mS.

The best way to work out a new coding scheme is to insert huge delays (up to 1/2 a second) between everything. Once you see the "h" you can start working these down to "whatever works faster."

I don't see any need for an explicit return in your void C functions.

#### joeyd999

Joined Jun 6, 2011
4,465
The best way to work out a new coding scheme is to insert huge delays (up to 1/2 a second) between everything. Once you see the "h" you can start working these down to "whatever works faster."
Ummm....that's not exactly the way an engineer would approach the problem...

#### ErnieM

Joined Apr 24, 2011
8,294
Ummm....that's not exactly the way an engineer would approach the problem...
Hey, I *AM* an engineer...

If I had the data sheet for the exact model display used by bobparihar I could give a better answer, but I have seen wildly different specs on these necessary delays depending on I don't know what, but probably different revs or manufactures of the drive IC.

As "much longer" will work 100% it is one place to start. I've hinted at the target delays (that in my project I would toss in some extra "just in any case".

#### THE_RB

Joined Feb 11, 2008
5,438
If the compiler is optimising, it might simply ignore this line;
for(i=0;i<5000;i++); //delay
because it doesn't do anything.

You should use the compilers delay() functions, check the compiler help file.

Also, if using delays you need one to set the E high period, then another delay afterward to allow the LCD to execute the command. You are not doing the second delay which is the most important one!

The LCD datasheet (Hitachi 44780) shows the necessary minimum delays.

#### takao21203

Joined Apr 28, 2012
3,702
I dont use these Hitachi LCDs anymore- takes too long to wire them, 3.3v is difficult even if I have one's with charge pump, adapting delays is too messy as well.

Recently I tried a Nokia5110 and got it working very quick, also serial TFTs, very easy. I should offer my remaining character LCDs on the fleamarket but...they are cheap new, its probably not worth it. They all have been soldered to.

#### THE_RB

Joined Feb 11, 2008
5,438
It's not so bad connecting 6 signal wires, 2 power wires and a 10k trimpot.

But I get your point. I designed a small PCB for a 28pin PIC and LCD header, and had a heap made in a batch. Worked out about a dollar a PCB after postage and setup etc.

Now I can whip up a 28pin PIC + text LCD project very quickly.

Thread Starter

#### bobparihar

Joined Jul 31, 2014
93
Is this is correct Erniem?
Rich (BB code):
#include<reg51.h>
sbit rs=P3^0;
sbit rw=P3^1;
sbit en=P3^2;
unsigned int i,j;
void dat(unsigned char); // data fxn
void cmd(unsigned char); // command fxn
void delay(unsigned int); //delay fxn for 1ms
void main()
{
delay(10);
cmd(0x38);
delay(1);
cmd(0x0E);
delay(1);
cmd(0x06);
delay(1);
cmd(0x01);
delay(1);
cmd(0x80);
delay(1);
dat('h');
delay(1);

while(1);

}

void cmd(unsigned char x)
{
P2=x;
rs=0;
rw=0;
en=1;
for(i=0;i<500;i++);
en=0;
}
void dat(unsigned char y)
{
P2=y;
rs=1;
rw=0;
en=1;
for(i=0;i<500;i++);
en=0;
}
void delay(unsigned int z)
{
for(i=0;i<z;i++)
{
for(i=0;i<1275;i++);

}
}

this works correct on simulation( proteus) but i cant even get thye cursor blinking on hardware

#### ErnieM

Joined Apr 24, 2011
8,294
So try making all the delays 10x longer.

#### klangst

Joined Jul 5, 2013
56
I am very new to all of this and this in theory should not be a problem with a development board, but all unused data pins from the lcd should go to ground in my understanding. I have seen issues where the lcd would not work and it was due to unused data pins being left unconnected.

#### shteii01

Joined Feb 19, 2010
4,644
I am very new to all of this and this in theory should not be a problem with a development board, but all unused data pins from the lcd should go to ground in my understanding. I have seen issues where the lcd would not work and it was due to unused data pins being left unconnected.
Can you tell us which LCD data pins/lines are not being used?

#### klangst

Joined Jul 5, 2013
56
Can you tell us which LCD data pins/lines are not being used?
In 4-bit mode, D0, D1, D2 and D3 are connected to ground. I was not clear if this is being used in 4-bit or 8-bit mode. I say I am very new to this and am learning by reading and posting.

#### shteii01

Joined Feb 19, 2010
4,644
In 4-bit mode, D0, D1, D2 and D3 are connected to ground. I was not clear if this is being used in 4-bit or 8-bit mode. I say I am very new to this and am learning by reading and posting.
Is OP using 4 bit mode?

#### ErnieM

Joined Apr 24, 2011
8,294
Is OP using 4 bit mode?
His posted code has no indication of driving a 4 bit display.

Aside: when using displays in 4 bit mode I will leave the unused data pins just float. They do have internal (pull up I believe) resistors to keep them from truly floating if unconnected.

Last edited:
Thread Starter

#### bobparihar

Joined Jul 31, 2014
93
thanks everyone here..i had fixed the error.. now my lcd showing correct output

iam using the following code for shifting the string given to lcd
Rich (BB code):
while(1)
{
cmd(0x1c);  // shift entire output to the right
delay(20);   // delay for 20ms
}
i did acheive the shifting, but the shifting is not smooth.. means the animation is not smooth...i did changed the delay sevral time but im not getting what i want..
anyone? plz help

Last edited by a moderator:

#### shteii01

Joined Feb 19, 2010
4,644
thanks everyone here..i had fixed the error.. now my lcd showing correct output

iam using the following code for shifting the string given to lcd
Rich (BB code):
while(1)
{
cmd(0x1c);  // shift entire output to the right
delay(20);   // delay for 20ms
}
i did acheive the shifting, but the shifting is not smooth.. means the animation is not smooth...i did changed the delay sevral time but im not getting what i want..
anyone? plz help
You could try reading busy flag instead of using delay.

However, it is possible that your lcd is too slow no matter what you do, in this case the only thing you can do to get faster/better animation is to use a faster lcd.