Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help me fix this program code for LCD

Status
Not open for further replies.

jannogale

Newbie level 4
Joined
Jul 29, 2010
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
UK
Activity points
1,331
the problem that i faced in this programm is that the letter B not appearing on LCD.
I need a help plz.

#define LCD_rs P2_4
#define LCD_rw P2_5
#define LCD_e P2_6
#define LCD_data P2
void main (void)
{
void delay(void);
void LCD_init (void);
void LCD_Control(unsigned char);
void LCD_Data(unsigned char d);

LCD_init();
LCD_Data('B');

while(1)
{
LCD_Data('B');

}

void LCD_init (void)
{
LCD_Control(0x03);
LCD_Control(0x03);
LCD_Control(0x03);
LCD_Control(0x02);
LCD_Control(0x28);

}
void delay(void)
{
int i = 0;
for (i = 0; i < 1000; i++);

}


void LCD_Data(unsigned char d)
{
unsigned char Hbyte;
unsigned char Lbyte;
Lbyte = d & 0xf0;
Hbyte = (d >> 4) & 0x0f;

P2 = 0;
P2 = P2 | Hbyte;
P2 = P2 | 0x50; //E = 1
delay();
P2 = P2 & ~0x50; // E = 0
delay();

P2 = P2 | Lbyte;
P2 = P2 | 0x50;
delay();
P2 = P2 & ~0x50;
delay();

P2 = 0;
}
void LCD_Control(unsigned char d)
{
unsigned char Hbyte;
unsigned char Lbyte;
Lbyte = d & 0xf0;
Hbyte = (d >> 4) & 0x0f;

P2 = 0;
P2 = P2 | Hbyte;
P2 = P2 | 0x40; // E = 1
delay();
P2 = P2 & ~0x40; // E = 0
delay();
P2 = P2 | Lbyte;
P2 = P2 | 0x40;
delay();
P2 = P2 & ~0x40;
delay();
P2 = 0;
}
} End of Main
 

Lcd programming

I am guessing it is a hd44780 type display, configured in 4-bit mode, d4..7 on p2.0..3.
 

Lcd programming

The software init sequence ist not according to the manufacturers suggestions. The four first control writes (03,03,03,02) have to send
as single nibbles, not using LCD_Control(). Also the delay may be unsufficient. Read the datasheet thoroughly!
 

Lcd programming

The four first control writes (03,03,03,02) have to send as single nibbles, not using LCD_Control().

it can be sent using LCD_Control().

Also the delay may be unsufficient.

only if your chip is running much faster than 12Mhz.

Read the datasheet thoroughly!

good advice.
 

Re: Lcd programming

it can be sent using LCD_Control().
Yes, if you write:
Code:
LCD_Control(0x33); 
LCD_Control(0x32);
But the individual delay requirements are different, e.g. 4.1 ms after the first nibble. LCD_Control would be slowed down seriously if you do it like this.
only if your chip is running much faster than 12Mhz
A 8051 derivate clocked at 12 MHz can be either 1 or 12 MIPS fast. Can't be seen from the code.
 

Lcd programming

Yes, if you write:
Code:
LCD_Control(0x33);
LCD_Control(0x32);

no. it is fine as written by the original poster.

But the individual delay requirements are different, e.g. 4.1 ms after the first nibble. LCD_Control would be slowed down seriously if you do it like this.

in fact, that's incorrect: the datasheet just speicifies minimum timing. you can delay longer than the minimum.

A 8051 derivate clocked at 12 MHz can be either 1 or 12 MIPS fast. Can't be seen from the code.

what do you think a reasonable guess is?

Added after 2 minutes:

strategically, you identified exactly what went wrong with this code: the original poster's thorough lack of understanding of the device and its datasheet.

tactically, what you mentioned has zero impact on getting the code to work. There are a few glaring issues here that if mitigated can get the code to work quickly. and you identified none of them.

Added after 35 minutes:

well, I took the code, made three minor changes - none of them mentioned by FvM - and it worked!

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top