# Interfacing with an LCD Screen

#### HarG

Joined Nov 20, 2012
18
Hello everyone,

I'm trying to operate a 2-line LCD HD44780. I am just wondering if I need to set EN high to low for every command I send? Also if I just write the initialisation piece, will I notice if it has worked on the display (like a flashing cursor) or will it stay blank until an actual command has beed sent?

Thanks

#### ErnieM

Joined Apr 24, 2011
7,991
I am just wondering if I need to set EN high to low for every command I send?
Yes it needs to toggle low then high for every command.

I just write the initialisation piece, will I notice if it has worked on the display (like a flashing cursor) or will it stay blank until an actual command has beed sent?
Stays blank till initialized. If you have a contrast control you sometimes see half the rows show as complete blocks with a very misaligned contrast on an uninitialized display.

#### MrChips

Joined Oct 2, 2009
19,160
E should go high then low.

#### HarG

Joined Nov 20, 2012
18
Thank you. That was very quick and very helpful.

Cheers guys

#### HarG

Joined Nov 20, 2012
18
Hi guys.

Been having a go with it and can't seem to get it going. I think I have about got it but could you just take a quick gander at my code to make sure I'm not hugely mistaken?

I set a delay of 2ms (as its just larger than the longest time for the display to do a function) instead of using the busy flag. I can't see the problem really. I have the 8 data bits in Port C of an 18F45K20.

Rich (BB code):
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#pragma config FOSC = INTIO67, FCMEN = OFF, IESO = OFF, PWRT = OFF, BOREN = OFF
#pragma config BORV = 18, WDTEN = OFF, WDTPS = 1, MCLRE = ON, HFOFST = ON
#pragma config LPT1OSC = OFF, PBADEN = OFF, CCP2MX = PORTC, STVREN = OFF
#pragma config LVP = OFF,  XINST = OFF, CP0 = OFF, CP1 = OFF, CP2 = OFF
#pragma config CP3 = OFF, CPB = OFF, CPD = OFF, WRT0 = OFF, WRT1 = OFF
#pragma config WRT2 = OFF, WRT3 = OFF, WRTB = OFF, WRTC = OFF, WRTD = OFF
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
#pragma config EBTRB = OFF
#define LCD_data PORTC
#define RS   RB5
#define RW   RB6
#define EN   RB7
#define _XTAL_FREQ 16000000

void PIC_init()
{
TRISC=0;
TRISB=0;
}
void LCD_busy()
{
__delay_ms(2);
}

void LCD_init()
{
LCD_data = 0x30;
EN   = 1;              //Enable H->L
EN   = 0;
__delay_ms(4.5);
LCD_data = 0x30;
EN   = 1;
EN   = 0;
__delay_us(300);
LCD_data = 0x30;
EN   = 1;
EN   = 0;

LCD_data = 0x30;       //Function set
RS   = 0;
RW   = 0;
EN   = 1;
EN   = 0;
LCD_busy();
LCD_data = 0x08;       //Display off
RS   = 0;
RW   = 0;
EN   = 1;
EN   = 0;
LCD_busy();
LCD_data = 0x01;       //Clear LCD
RS   = 0;
RW   = 0;
EN   = 1;
EN   = 0;
LCD_busy();
LCD_data = 0x03;       //Entry mode - increment, shift left
RS   = 0;
RW   = 0;
EN   = 1;
EN   = 0;
LCD_busy();
}

void main(void)
{
__delay_ms(20);
PIC_init();
LCD_init();
RS = 1;
RW = 0;
LCD_data = 0b00110000;  //'0' ASCII
EN = 1;
EN = 0;
LCD_busy();
RS = 0;
LCD_data = 0b00001111;  //display on, cursor on, blink on
EN = 1;
EN = 0;
}

#### spinnaker

Joined Oct 29, 2009
7,815
Instead of us looking at your code, you should first verify that everything is connected the way it should be and pins are changing state as expected.

You can do this by single stepping through your code and measuring the pins on the LCD with a scope, logic probe or voltmeter. You can make a logic probe with an led and resistor if you don't have one.

One of the biggest problems of an LCD not working is improper connections.

Many compilers have LCD libraries and there really is not need to write one. Also many out there on the internet.

#### HarG

Joined Nov 20, 2012
18
Thank you spinnaker. I will give it a go.

#### ErnieM

Joined Apr 24, 2011
7,991
Check the delays you use. I did notice you immediately bring EN hi then lo. It may need a bit longer dwell, as well as setup times for the data.

#### t06afre

Joined May 11, 2009
5,936
Here is some code that work on a 18F25K22. It use 4 bit mode but it should be no problem using 8 bit mode. See if it can help you

#### Attachments

• 13.8 KB Views: 24

#### spinnaker

Joined Oct 29, 2009
7,815
4 bit mode is easier anyway. Less connections to screw up.

#### t06afre

Joined May 11, 2009
5,936
This is how it was connected in 4 bit mode if it can help you

#### Attachments

• 12.9 KB Views: 21

#### HarG

Joined Nov 20, 2012
18
Thank you very much guys. I will have another go tomorrow and let you know how it goes.

Once again thank you.

#### HarG

Joined Nov 20, 2012
18
Hi guys,

I had a bit of a go with it tonight. I changed the 8 data lines to LEDs to try and see what is going on. They seem to be doing the right thing but unfortunately they are continuously looping.

I was under the impression that if I did not put a while(1) command in the main program it would only run once? It seems to be continuously running through my main program.

Any thoughts?

#### MrChips

Joined Oct 2, 2009
19,160
If your main program is being repeated check to see if the watch-dog timer is active and hence restarting your program after a predetermined time-out.

#### ErnieM

Joined Apr 24, 2011
7,991
Hi guys,

I had a bit of a go with it tonight. I changed the 8 data lines to LEDs to try and see what is going on. They seem to be doing the right thing but unfortunately they are continuously looping.

I was under the impression that if I did not put a while(1) command in the main program it would only run once? It seems to be continuously running through my main program.

Any thoughts?
The opposite is true, though it depends on how and where you insert the while(1)