# Pic and LCD Screen

Joined Mar 6, 2012
3
I'm having problems getting my LCD screen to initialize properly I was wondering if anybody could tell me what I'm doing wrong.

I'm using a PIC18F2520
here is the datasheet for the LCD I'm using
http://www.sparkfun.com/datasheets/LCD/st7066.pdf

I'm trying to use 4 bit mode my pin setup is
Enable RB6
R/W RB5
RS RB4
Data7-4 RB3-0

This is my first time ever using a pic and I'm not that familiar with embedded systems in general. I have used this website as my reference for how to initalize the lcd screen http://joshuagalloway.com/lcd.html.

Rich (BB code):
void write_lcd_4(unsigned char hex){
//set enable to low
PORTBbits.RB6 = 0;
//set r/s to zero for command
PORTBbits.RB4 = 0;
//put in high byte
PORTBbits.RB0 = getBit(hex, 0);
PORTBbits.RB1 = getBit(hex, 1);
PORTBbits.RB2 = getBit(hex, 2);
PORTBbits.RB3 = getBit(hex, 3);
//set enable high
PORTBbits.RB6 = 1;
//kill time
Delay10KTCYx(3);
//set enable to low for signal send
PORTBbits.RB6 = 0;
Delay10KTCYx(15);
}

void write_lcd(unsigned char hex){
//set enable to low
PORTBbits.RB6 = 0;
//set r/s to zero for command
PORTBbits.RB4 = 0;
//put in high byte
PORTBbits.RB0 = getBit(hex, 4);
PORTBbits.RB1 = getBit(hex, 5);
PORTBbits.RB2 = getBit(hex, 6);
PORTBbits.RB3 = getBit(hex, 7);
//set enable high
PORTBbits.RB6 = 1;
//kill time
Delay10KTCYx(3);
//set enable to low for signal send
PORTBbits.RB6 = 0;
Delay10KTCYx(15);
//put in low byte
PORTBbits.RB0 = getBit(hex, 0);
PORTBbits.RB1 = getBit(hex, 1);
PORTBbits.RB2 = getBit(hex, 2);
PORTBbits.RB3 = getBit(hex, 3);
/************************************************/
//set enable high
PORTBbits.RB6 = 1;
//kill time
Delay10KTCYx(3);
//set enable to low for signal send
PORTBbits.RB6 = 0;
Delay10KTCYx(15);
}

void lcd_init(void){
Delay10KTCYx(60);
write_lcd_4(0x03);
write_lcd_4(0x03);
write_lcd_4(0x03);
write_lcd_4(0x02);
//function set
write_lcd(0x28);
//turn off
write_lcd(0x08);
//display clear
write_lcd(0x01);
//entry mode
write_lcd(0x06);
//turn back on
write_lcd(0x09);
}

#### spinnaker

Joined Oct 29, 2009
7,815
What makes you think it is not initializing properly?

Do you have a pot on your contrast pin? Is it adjusted properly?

Have you stepped through your code outputting bits to the LCD and confirming the bits are being seen as expected on the LCD? (basically confirming that you have everything wired correctly).

Joined Mar 6, 2012
3
The screen weather I attempt to initialize or not starts out with all of the "squares" on the lcd screen just filled in black. When I attempt to initialize it remains that way and one of the commands during my attempt to initialize is a clear, which should make them all go away and the screen just be blank. I am 100% positive it is wired up the way I state at the way I state it is at the beginning of my post.

When it comes to the contrast pin I don't really know anything about that, as I said first time using a pic and new in general to this. I do know that I can run other code on the chip and use the LED light for output so the chip does work so it is being set up correctly.

#### MrChips

Joined Oct 2, 2009
19,160
Many times people ignore the contrast pin and cannot figure out why the LCD does not work.
If the contrast voltage is set to an extreme, you may end up with nothing or all "squares".

Joined Mar 6, 2012
3
With my board the voltage to the lcd can either be 5v or 3.3v I believe I forget which one I have it set on right now (its on the one a EE grad student told me to put it on), and I have switched it out of frustration before just to see if it would initialize then and that still didn't change the result.

#### t06afre

Joined May 11, 2009
5,936
Are you 100% sure your timing is correct. from your setup I am not sure about it. Take a look here http://www.taoli.ece.ufl.edu/teaching/4744/labs/lab7/LCD_V1.pdf
One thing that may be confusing is the text on page one
7 DB4 I/O data bit, not used in 4 bit mode
8 DB5 I/O data bit, not used in 4 bit mode
9 DB6 I/O data bit, not used in 4 bit mode​
10 DB7 I/O data bit, not used in 4 bit mode
That do not make much sense to me. I think it is a typo. And the text should have been something like this​
7 DB4 I/O data bit, D3:0 not used in 4 bit mode
8 DB5 I/O data bit, D3:0 not used in 4 bit mode
9 DB6 I/O data bit, D3:0 not used in 4 bit mode​
10 DB7 I/O data bit, D3:0 not used in 4 bit mode
Then you have LCD working. You should replace coding like this​
Rich (BB code):
write_lcd_4(0x03);
write_lcd_4(0x03);
write_lcd_4(0x03);
write_lcd_4(0x02);​
It will work. But it is kind of clumsy way of doing things. Try to use Mask Operators instead. This may be of some help
http://www.cprogramming.com/tutorial/bitwise_operators.html

#### spinnaker

Joined Oct 29, 2009
7,815
The screen weather I attempt to initialize or not starts out with all of the "squares" on the lcd screen just filled in black. When I attempt to initialize it remains that way and one of the commands during my attempt to initialize is a clear, which should make them all go away and the screen just be blank. I am 100% positive it is wired up the way I state at the way I state it is at the beginning of my post.

When it comes to the contrast pin I don't really know anything about that, as I said first time using a pic and new in general to this. I do know that I can run other code on the chip and use the LED light for output so the chip does work so it is being set up correctly.
Actually no where did you state it is wired correctly. But that is irrelevant. Unless you have actually toggle each bit and confirmed that it toggles on the corresponding LCD pin then you can't be 100% sure everything is connected and working correctly.

But your issue is probably contrast. You need a 10K trimmer resistor. Tie one end of the trimmer to ground, the over to your supply and the wiper to the contrast pin on the LCD.