Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC18 with LCD gibberish

Status
Not open for further replies.

gstavrev

New Member
Hello,
It seems that all I have are problems ;). I just got an 8-bit interface working with an LCD and I'm trying to convert it to 4-bit. I think I have the initialization down and I'm trying to print messages. This is my LCDdata code to write letters.

Code:
void LCDdata(unsigned char value)
{
char temp = 0xf0 & value;  // get the upper 4 bits
temp = temp >> 4;  // shift it right 4 times
ldata = temp;          // put it on the pins
rs = 1;
rw = 0;
en = 1;
MSDelay(10);              // delay 10
en = 0;

temp = 0x0f & value;  // get the lower 4 bits
ldata = temp;          // put it on the pins
rs = 1;
rw = 0;
en = 1;
MSDelay(10);              // delay 10
en = 0;
}

I guess the code sort of works, but I can't find the problem. It prints gibberish. When I tell it to print the string "K.-ds>$" ( just because the bits are more random) it doesn't transfer correctly.

K.-ds>$ in hex turns out to be "4b 2e 2d 64 73 3e 24"
my display prints gibberish = "b2 e2 d6 47 33 e2"

It seems that the first 4 bits of the first letter don't get through. Is it a delay problem? Can someone help me fix this?

---
I am using the PICkit2 with PIC18f4525 and Microchip C18 compiler along with 2x16 LCD that seems to be compatible to the Hitachi 47780.
--

Thanks

-George
 
Last edited:
If you had the 8 bit interface working and you have not changed your hardware then you are writing the nibbles to the wrong bits of the port, they should go to D7.

If this is the case try moving the shift (and changing direction) into the second write.

ie, the code becomes,
Code:
void LCDdata(unsigned char value)
{
    char temp = 0xf0 & value;	// get the upper 4 bits
    ldata = temp;          	// put it on the pins
    rs = 1;
    rw = 0;
    en = 1;
    MSDelay(10);              	// delay 10
    en = 0;
    
    temp = 0x0f & value;  	// get the lower 4 bits
    temp = temp [COLOR="Red"]<<[/COLOR] 4;  		// shift it [COLOR="Red"]left[/COLOR] 4 times
    ldata = temp;          	// put it on the pins
    rs = 1;
    rw = 0;
    en = 1;
    MSDelay(10);              	// delay 10
    en = 0;
}

Mike.
 
Thanks Mike. That makes sense, but I had already changed the hardware. I just found a fix by inserting just one 4-bit signal before writing anything. This seems to offset the flow and it works fine now.
 
That sounds familiar. Are you (OP) aware that the LCD is in 8-bit interface mode during the first few transfers during the 4-bit initialization procedure? You only send 4-bits for the first few commands while it's in 8-bit mode then you send two 4-bit nybbles at a time after you put the LCD into 4-bit interface mode. It's easy to be out-of-sync by one 4-bit nybble if you don't keep track...

Here's an excerpt from another thread;

~~~

If the OP's LCD_command() function is writing full 8 bits as two nybbles, could that be a problem during 4-bit initialization sequence?

I use a special command just for sending a single 4-bit nybble during the 4-bit initialization procedure;

Mike

Code:
void InitLCD()              //
{
    delay_ms(15000);        // 4-bit initialization procedure
    PutLCD(nyb,3);          // wr 1 nybble (8 bit mode, includes 160 us delay)
    delay_us(4100);         //
    PutLCD(nyb,3);          // wr 1 nybble (8 bit mode, includes 160 us delay)
    PutLCD(nyb,3);          // wr 1 nybble (8 bit mode, includes 160 us delay)
    PutLCD(nyb,2);          // wr 1 nybble, switch to 4 bit mode
                            // now write 2 nybbles every time
    PutLCD(cmd,0x28);       // 4 bit mode, 2 lines, 5x7 font
    PutLCD(cmd,0x08);       // display off, cursor off, blink off
    PutLCD(cmd,0x01);       // clear display (includes 1.55 msec delay)
    PutLCD(cmd,0x06);       // cursor inc, shift off
    PutLCD(cmd,0x0C);       // display on, leave cursor off
}
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top