8 bit lcd

Thread Starter

abeychandran

Joined Feb 7, 2012
17
i got a program for 8 bit lcd and its working
but i'd like to know , how its working
so anybody could explain the program




#include<htc.h>

#define RS RB2
#define RW RB3
#define EN RB4

void LCD_delay(void)
{
int t;
for(t=0;t<250;t++);
}

void delay_ms(unsigned char delay)
{
unsigned int i,j;
for(i=0;i<=delay;i++)
{
for(j=0;j<=1000;j++);
}
}

void LCD_command(unsigned char command)
{
unsigned char temp;
RS = 0;

temp =command & 0xff;
PORTD &= 0x00;
PORTD |= temp;
EN = 1;
for(temp=0;temp<5;temp++);
EN = 0;
LCD_delay();
}

void LCD_data(unsigned char data)
{
unsigned char temp;
RS=1;

temp = data & 0xff;
PORTD &= 0x00;
PORTD |= temp;
EN = 1;
for(temp=0;temp<5;temp++);
EN = 0;
LCD_delay();
}

void initialise_LCD(void)
{
ADCON0 = 0x00;//Disable ADC ports
ADCON1 = 0x06;//Disable ADC ports

TRISD = 0x00;
PORTD = 0x00; //DATA or COMMAND i/p to LCD

TRISE = 0x00;
PORTE = 0x00; //CONTROL i/p to LCD

EN=0;
RW=0;

delay_ms(40);
LCD_command(0x38);
delay_ms(20);
LCD_command(0x38);
delay_ms(2);
LCD_command(0x38);
delay_ms(1);

LCD_command(0x07);//increment, disp shift
LCD_command(0x0c);//off cursor, display on
LCD_command(0x14);//Display shift,shift to left i.e. scroll
LCD_command(0x02);//Command return the cursor to home
LCD_command(0x01);//clear LCD
delay_ms(1);
}

void clear_LCD()
{
LCD_command(0x01);
delay_ms(1);
}

void display_LCD(char line_number,char position, const char *message)
{
int a;
if(line_number==1)
{
a=0x80+position;
LCD_command(a);
}
else if(line_number==2)
{
a=0xC0+position;
LCD_command(a);
}
while(*message!=0)
{
LCD_data(*message);
delay_ms(15);
message++;
}
}

void main(void)
{
__CONFIG(0x193A);
initialise_LCD();
while(1)
{
display_LCD(1,8,"WELCOME TO ALL OF YOU PEOPLE...");
display_LCD(2,4,"welcome");
delay_ms(1);
}
}
 

spinnaker

Joined Oct 29, 2009
7,830
I think the code is fairly self explanatory look at the datasheet for details on what each of those commands do.

As far as your issue there are a few problems that cause the LCD not to display.

1. The contrast is not connected correctly. Where is your schematic.

2. The Pic is not connected correctly to the LCD. You can troubleshoot this by stepping through your code. When you place data on your Pic latches, confirm that the proper inputs change on the LCD. You can use a scope, a logic probe, logic analyzer or even a voltmeter.

3. Your delays are not correct. Make sure the delays you see in the code are really delaying that amount of time. What usually causes this is an improper configuration on the clock frequency setting for the delay function.
 

be80be

Joined Jul 5, 2008
2,072
Your missing #define LCD_DATA

And the #include "lcd.h"

Which you need to save the lcd.h to your project
And edit it to match your hardware set up you also need the lcd.c
saved to your project you copy them from sample folder

Your code looks like your trying to use the library files and write your on to

The delay_ms needs frequency set
Unless specified elsewhere, 4MHz system frequency is assumed
 
Last edited:
Top