LCD Interfacing 4 Bit   
LCD is the most common way to let your controller speak its desires :-). So lets find out how can we make it happen, using only total of 7 pins from your microcontrollers GPIO. So LCD module will cost only single port out of fours...

Schematic Diagram -



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

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

>>The 4 bit data lines and 3 control lines are connected to the Port2.

>> 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.

>>While using LCD in 4 bit data mode it saves 4 bits of our total GPIO lines, that’s why it is most commonly used. MSB of any data or commnad is sent first over 4 bits and then 4 LSB sent by shifting the data byte 4 times left.









/***********************************************************************
Example program For 4 bit LCD interfacing with AT89S52
File name - LCD.h (to be included in main program)
************************************************************************/

#define LCDPORT P2

#define RS P2_0

#define RW P2_1

#define E P2_2

bit status=0

#define lcd_delay 60

void delay(unsigned int j)

{

unsigned int i,k;

for(i=0;i
{

for(k=0;k<100;k++);

}

}

void _lcd_init_write(unsigned char a)

{

RS = 0;

RW = 0;

LCDPORT=a;

E=1;

delay(lcd_delay);

E=0;

}

void lcd_com(unsigned char a){

unsigned char temp;

if(status)
{

status=0;

goto a;

}

RS=0;

a:

RW=0;

temp=a;

temp&=0xF0;

LCDPORT&=0x0F;

LCDPORT|=temp;

E=1;

delay(lcd_delay);

E=0;

temp=a<<4;

temp&=0xF0;

LCDPORT&=0x0F;

LCDPORT|=temp;

E=1;

delay(lcd_delay);

E=0;

}

void lcd_data(unsigned char a){

status=1;

RS=1;

lcd_com(a);

}

void lcd_init(void){

delay(lcd_delay);

_lcd_init_write(0x30);

delay(lcd_delay);

_lcd_init_write(0x30);

delay(lcd_delay);

_lcd_init_write(0x30);

delay(lcd_delay);

_lcd_init_write(0x20);

delay(lcd_delay);

lcd_com(0x28);

delay(lcd_delay);

lcd_com(4);

delay(lcd_delay);

lcd_com(0x85);

delay(lcd_delay);

lcd_com(6);

delay(lcd_delay);

lcd_com(1);

delay(lcd_delay);

}

void lcd_puts(char *aaa)

{

unsigned int i=0;

for(;aaa[i]!=0;i++)lcd_data(aaa[i]);

}





/****************************************************************
Fila name - main.c (demo program for 4 bit mode LCD interfacing)
*****************************************************************/

#include<at89x52.h>

#include<lcd.h>

void main(void)

{

lcd_init();

lcd_com(0x01); // Clear screen

lcd_com(0x80); // Initially at first line

lcd_puts("electroons.com");

lcd_com(0xc0); // Command to goto second line

lcd_puts("LCD 4 Bit");

while(1==1); // loop forever

}


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