The LED (including multisegment LEDs) are useful in indicating that an application is running, is connected or is waiting, etc. They do not display all the ASCII characters, special characters, and graphics. An LCD can display all the above characters easily. The interfacing (hardware + software) is relatively easy because refreshing of all the characters is done automatically by refreshing the controller circuit present in the LCD modules. They are commonly available in 16 × 1, 20 × 1, 20 × 2, 20 × 4, and 40 × 2 sizes (the first number indicates characters in a line and a second number is a number of lines in a display module).


Advantages of using LCD as a Display Device

LCDs provide a better user interface as they can display ASCII messages
Lower power consumption
Display information is updated at a high speed because it has an inbuilt refresh controller


Pin Description for LCD

LCD modules usually have 14 pins. These pins are described in Table 




LCD Commands

The table shows a list of commands (instructions) recognized by the LCD controller with a description of how each command is formed using the different combinations of LCD signals along with the typical execution time of each command.

The LCD commands are used to select various display functions like cursor positioning, blinking, character size, cursor shift, data format (4-bit or 8-bit). For example, to set entry mode as shift cursor left, make I/D = 0 and S = 0. Therefore, the command will be 04H and this command will be given to LCD through data lines D7-D0. Refer to Table to understand how the various commands are formed.


Initialization of the LCD using the Internal Reset Circuit

An Internal Reset Circuit (IRC) will automatically initialize the LCD when it is powered and VCC reaches the full value (4.5 V) within 10 ms. The following commands/instructions are executed during the initialization process. The Busy Flag (BF) remains high (busy state) until the process is completed. The LCD will remain in a busy state (BF = 1) for 10 ms after VCC rises to 4.5 V.
DL = 1: 8-bit interface
N = 0: Messages are displayed in one line
F = 0 : 5 × 7 dots—font size of character
D = 0 : Display OFF
C = 0 : Cursor is OFF
B = 0 : Blink OFF
I/D = 1: Displayed addresses are automatically incremented by 1
S = 0 : Display shift off
If the internal power supply does not reach 4.5 V within 10 ms, the display will not operate normally. In this case, the display can be initialized through the software.

8-bit Mode

In this mode, all the eight data line pins (D7-D0) are used for giving data/commands. Figure 18.9 shows the simplified
connections of the LCD module with the 8051 microcontrollers.

Any port of the 8051 is connected with the data lines of LCD. (Remember that data lines of the LCD need not be always connected to the data bus of the 8051). Three pins from other ports are connected with the control signals: Register Select, Read/Write, and Enable.

The LCD can accept and execute different commands. When it is executing the commands, i.e. performing internal activities, data should not be sent to it otherwise the data will be overwritten. To indicate that it is busy doing internal activities, it makes the bit D7 high (busy flag). So, a program must monitor D7 before giving the next command or data to the LCD. To avoid monitoring the busy flag, one can put a reasonably larger delay between the two consecutive commands/data writes to make sure that the LCD has finished an internal operation and is not busy. But this approach is not efficient and wastes the time. The only advantage of this approach is that it saves one I/O pin of the microcontroller because R/W can be permanently grounded.


code in c language

#include<reg51.h>
sbit RS=P1^0 ;
sbit RW=P1^1 ;
sbit E=P1^2 ;
void COMMAND (unsigned char);
void DATADISPLAY (unsigned char);
void DELAY (void);
void main()
{
    DELAY();
                        // initialization of LCD by the software
    DELAY();
                         // this part of the program may be skipped
    COMMAND(0x38) ;
    DELAY();
    COMMAND(0x38) ;
    DELAY();
    COMMAND(0x38) ;
    DELAY();
    COMMAND(0x38) ;
                        // LCD command for LCD 2 lines 5*7 matrix
    COMMAND(0x0F) ;
                        // display on, cursor on with blinking
    COMMAND(0x06) ;
                        // shift cursor right
    COMMAND(0x01) ;
                        // clear display
    COMMAND(0x80) ;
                        // cursor at line 1, position 0
    DATADISPLAY(‘H’) ;
                        // send data to LCD
    DATADISPLAY(‘I’) ;
}

void COMMAND(unsigned char cmd)
{
    P2 = cmd ;
    // send command
    RS = 0;
    // RS=0 for command
    RW = 0;
    // R/W=0 for write command
    E = 1;
    // high to low pulse for write
    DELAY();
    E = 0;
    DELAY();
    // wait before giving the next command
}

void DATADISPLAY(unsigned char data)
{
    P2 = data;
    // send data
    RS = 1;
    // RS=1 for data
    RW = 0;
    // R/W=0 for write command
    E = 1;
    // high to low pulse for write
    DELAY();
    E = 0;
    DELAY();
    // wait before giving the next command
}
void DELAY(void)
{
    unsigned int i ;
    for(i=0; i<7000; i++);
}

code in assembly language

//P2.0-P2.7 are connected to LCD data pins D0-D7
//P1.0 is connected to RS pin of LCD
//P1.1 is connected to R/W pin of LCD
//P1.2 is connected to E pin of LCD
ORG 0000H
LCALL WAIT
                            // initialization of LCD by software
LCALL WAIT
                            // this part of program is not mandatory but
MOV A, #38H
                                    // recommended to use because it will
LCALL COMMAND
                            // guarantee proper initialization even when
LCALL WAIT
                                            // power supply reset timings are not met
MOV A, #38H
LCALL COMMAND
LCALL WAIT
MOV A, #38H
LCALL COMMAND
                                    // initialization complete
MOV A, #38H
                                    // initialize LCD, 8-bit interface, 5X7 dots/character
LCALL COMMAND
                                        // send command to LCD
MOV A, #0FH
                                    // display on, cursor on with blinking
LCALL COMMAND
                                        // send command to LCD
MOV A, #06
                                        // shift cursor right
LCALL COMMAND
                        // send command to LCD
MOV A, #01H
                            // clear LCD screen and memory
LCALL COMMAND
                                            // send command to LCD
MOV A, #80H
                                                            // set cursor at line 1, first position
LCALL COMMAND
                                    // send command to LCD
MOV A, #’H’
                                                    // H to be displayed
LCALL DISPLAY
                                       // send data to LCD for display
MOV A, #’I’
                                         // I to be displayed
LCALL DISPLAY
                                // send data to LCD for display
HERE:
SJMP HERE
                            // wait indefinitely
COMMAND:
                            // command write subroutine
MOV P2, A
                        // place command on P1
CLR P1.0
                    // RS = 0 for command
CLR P1.1
                    // R/W = 0 for write operation
SETB P1.2
                    // E = 1 for high pulse
LCALL WAIT
                        // wait for some time
CLR P1.2
                            // E = 0 for H-to-L pulse
LCALL WAIT
                            // wait for LCD to complete the given command
RET
DISPLAY:
                            // data write subroutine
MOV P2, A
                            // send data to port 1
SETB P1.0
                            // RS = 1 for data
CLR P1.1
                            // R/W = 0 for write operation
SETB P1.2
                            // E = 1 for high pulse
LCALL WAIT
                                // wait for some time
CLR P1.2
                                // E = 0 for H-to-L pulse
LCALL WAIT
                                // wait for LCD to write the given data
RET
WAIT:
MOV R6, #30H
                            // delay subroutine
THERE:
MOV R5, #0FFH
//
HERE1:
DJNZ R5, HERE1
//
DJNZ R6, THERE
RET

Proteus simulation with modify code to print "welcome to the tech factory" in c language 



code

#include<reg51.h>
#include<string.h>
#include<stdio.h>
sbit rs = P2^0;
sbit rw = P2^1;
sbit en = P2^2;
void lcd_data(char[]);
void lcd_ok(bit);
void lcd_code(char);
void lcd_init();
#define lcd_port P1
void delay(unsigned int);
void delay1(unsigned int);
void uart_init()
{
 TMOD=0x20;
 SCON=0x50;
 TH1=0xfd;
 TR1=1;
}

void main()
{
uart_init();
lcd_init();
lcd_code(0x01);
lcd_data("   Welcome To    0");
lcd_code(0xC0);
lcd_data("*the tech factory*0"); 
}

void lcd_data(char ch[])
{
int index1;
for(index1 = 0; ch[index1] != '0'; index1++)
{

lcd_port = ch[index1];
lcd_ok(1);
}
return;
}

void lcd_ok(bit mybit)
{
if(mybit)
{
rs = 1;
}
else
{
rs = 0;
}
rw = 0;
en = 1;
delay(1);
en = 0;
return;
}

void delay1(unsigned int itime)
{
unsigned int i,j;
for(i = 0; i < itime; i++)
for(j = 0; j < 500; j++);
return;
   
}

void delay(unsigned int time)
{
int i = 0;
for(; time > 0; time--)
for(; i < 353; i++);
return;
}

void lcd_init()
{
lcd_code(0x38);
lcd_code(0x01);
lcd_code(0x0c);
lcd_code(0x80);
return;
}

void lcd_code(char ch)
{
 
lcd_port = ch;
lcd_ok(0);
return; 
}

Post a Comment

Previous Post Next Post