LCD Interfacing 8 Bit   
LCD is the most common way to let your controller speak its desires :-). So lets find out how can we make it happen...

Schematic Diagram -



>> This circuit consists of a Microcontroller and a LCD.

>>This LCD is operating with an 8-bit data bus. So totally 11 data lines are required (8 Data lines and 3 control lines).

>>The 8 bit data lines are connected to the Port1 and the 3 control lines to the Port3.5-Port3.7.

>> The EN line is called "Enable." This control line indicates to the LCD that we are sending it data. To send data to the LCD, the EN should be low (0) and then set the other two control lines and/or put data on the data bus. When the other lines are completely ready, bring EN high (1) and wait for the minimum amount of time required by the LCD datasheet (this varies from LCD to LCD), and end by bringing it low (0) again.

>> The RS line is the "Register Select" line. When RS is low (0), the data is to be treated as a command or special instruction (such as clear screen, position cursor, etc.). When RS is high (1), the data being sent is text data, which should be displayed on the screen. For example, to display the letter "T" on the screen we would set RS high.

>>The RW line is the "Read/Write" control line. When RW is low (0), the information on the data bus is being written to the LCD. When RW is high (1), the program is effectively querying (or reading) the LCD. Only one instruction ("Get LCD status") is a read command. All others are written commands so RW will almost always be low.







/*******************************************************************
Example - LCD driving using 8 bit bus.
All connections are according to the text given above.
Any Queries - devesh@electroons.com
********************************************************************/


#include <reg51.h>//Define 8051 Registers
#include <stdio.h> //Define I/O Functions
#define DATA P1 //Define DATA to Port1
//Define control pins
sbit RS = P3^5; //Register Select
sbit RW = P3^6; //LCD Read/Write
sbit lcd_e = P3^7; //LCD Enable
code unsigned char msg[] = ("electroons.com"); //Display the message
code unsigned char msg1[] = (" LCD Demo ");
//----------------------------------
// LCD Functions
//----------------------------------

void lcd_init(void);
void lcd_cmd(unsigned char);
void lcd_display(unsigned char);
void DelayMs(int);
//----------------------------------
// LCD command Function
//----------------------------------

void lcd_cmd(unsigned char cmnd)
{
DATA = cmnd;
RS = 0;
RW = 0;
lcd_e = 1;
DelayMs(35);
lcd_e = 0;
}
//----------------------------------
// LCD Data Function
//----------------------------------

void lcd_display(unsigned char dat)
{
DATA = dat;
RS = 1;
RW = 0;
lcd_e = 1;
DelayMs(35);
lcd_e = 0;
}
//----------------------------------
// LCD Delay Function
//----------------------------------

void DelayMs(int k)
{
unsigned int a;
for(a=0;a<=k;a++);
}
//----------------------------------
// LCD Initialization
//----------------------------------

void lcd_init(void)
{
unsigned char i;
lcd_cmd(0x38); //2x16 Character 5x7 dot
DelayMs(15); //matrix LCD,8-bit format
lcd_cmd(0x0c); //Display On, cursor off
DelayMs(15);
lcd_cmd(0x06); //Shift Cursor to right
DelayMs(15);
lcd_cmd(0x01); //Clear display screen
DelayMs(15);
//-------------------------------------------
// First Line Message Display
//-------------------------------------------

lcd_cmd(0x80); //First Line Initialization
DelayMs(35);
i=0;
while(msg[i]!='\0')
{
lcd_display(msg[i]);
i++;
}
DelayMs(50);
//-------------------------------------------
// Second Line Message Display
//-------------------------------------------

lcd_cmd(0xc0); //Second Line Initialization
DelayMs(35);
i=0;
while(msg1[i]!='\0')
{
lcd_display(msg1[i]);
i++;
}
DelayMs(50);
while(1);
}
//----------------------------------
// LCD Main Program
//----------------------------------

void main(void)
{
lcd_init(); //LCD Initialization
DelayMs(50);
while(1); //Loop Forever
}



devesh@electroons.com
Best viewed at Firefox 1024x768 resolution