Interfacing Liquid Crystal Display(LCD)
In this chapter you will able to use the liquid crystal display and how character are displayed in it and its basic operation . after that in later chapter we will discuss about reading and displaying the temperature of the surrounding in the LCD .before starting or doing the actual programme lets discuss about basic introduction and how LCD display works .
A liquid-crystal display (LCD) is a flat-panel display or other electronic visual display that uses the light-modulating properties of liquid crystals. Liquid crystals do not emit light directly.
LCDs are available to display arbitrary images (as in a general-purpose computer display) or fixed images with low information content, which can be displayed or hidden, such as preset words, digits, and 7-segment displays as in a digital clock. They use the same basic technology, except that arbitrary images are made up of a large number of small pixels, while other displays have larger elements.
LCDs are used in a wide range of applications including computer monitors, televisions, instrument panels, aircraft cockpit displays, and signage. They are common in consumer devices such as DVD players, gaming devices, clocks, watches, calculators, and telephones, and have replaced cathode ray tube (CRT) displays in nearly all applications. They are available in a wider range of screen sizes than CRT and plasma displays, and since they do not use phosphors, they do not suffer image burn-in. LCDs are, however, susceptible to image persistence.
The LCD screen is more energy-efficient and can be disposed of more safely than a CRT can. Its low electrical power consumption enables it to be used in battery-powered electronic equipment more efficiently than CRTs can be. It is an electronically modulated optical device made up of any number of segments controlling a layer of liquid crystals and arrayed in front of a light source (backlight) orreflector to produce images in color or monochrome. Liquid crystals were first discovered in 1888 .By 2008, annual sales of televisions with LCD screens exceeded sales of CRT units worldwide, and the CRT became obsolete for most purposes.
In this chapter we are interfacing HD44780 LCD which is 16*2 dot-matrix liquid crystal display controller and driver LSI displays alpha numerics ,Japanese kana characters, and symbols. It can be configured to drive a dot-matrix liquid crystal display under the control of a 4- or 8-bit microprocessor. A 16x2 LCD means it can display 16 characters per line and there are 2 such lines. In this LCD each character is displayed in 5x7 pixel matrix. This LCD has two registers.
- Command/Instruction Register- stores the command instructions given to the LCD. A command is an instruction given to LCD to do a predefined task like initializing, clearing the screen, setting the cursor position, controlling display etc.
- Data Register - stores the data to be displayed on the LCD. The data is the ASCII value of the character to be displayed on the LCD.
Before starting your project you should learn basic operation of the 16*2 LCD . Please refer your LCD model manufacturer data sheet . But commen characteristic of all LCD s are similar operations .
Please remember the following command code to program a LCD
|Hex Code||Command to LCD Instruction Register|
|1||Clear screen display|
|E||Display ON, Cursor ON|
|80||Force the cursor to the beginning of the 1st line|
|C0||Force cursor to the beginning of the 2nd line|
|30||Function set :8-bit,1 line ,5*7 dots|
|38||Function set :8-bit,2 line ,5*7 dots|
|20||Function set: 4-bit,1 line ,5*7 dots|
|28||Function set :4-bit ,2 line ,5*7 dots|
|08||Display off cursor off (clear display without clearing DDRAM content|
|0C||Display on cursor off|
|0F||Display on cursor blinking|
|18||Shift entire display left|
|1C||Shift entire display right|
|10||Move cursor left by one character|
|14||Move cursor right by one character|
|80+add||Set DDRAM address or cursor position on display|
|38||Use 2 lines and 5x7 matrix|
the above commands of lcd are based on model HD44780 this may be vary with different manufacturer . so better you should use your own model of lcd data sheet .
And the pin configuration of the 16*2 LCD is given below .
|2||VCC||Main power supply||+5 V|
|3||VEE||Power supply to control contrast||Contrast adjustment by providing a variable resistor through VCC|
|4||RS||Register Select||RS=0 to select Command Register
RS=1 to select Data Register
|5||R/W||Read/write||R/W=0 to write to the register
R/W=1 to read from the register
|6||EN||Enable||A high to low pulse (minimum 450ns wide) is given when data is sent to data pins|
|7||DB0||To display letters or numbers, their ASCII codes are sent to data pins (with RS=1). Also instruction command codes are sent to these pins.|
|10||DB3||8-bit data pins|
|15||Led+||Backlight VCC||+5 V|
|16||Led-||Backlight Ground||0 V|
now connect your circuit as shown in circuit diagram below
make sure pin number 16 of lcd is connected to ground , and pin number 10 of microcontroller is connect to VCC and that of 11 should be connect to Ground for proper operation.
Ok now write the following code
This code might not run on atmega-16 on proteus deu to the lack of simulation memory problem . you can replace the microcontroller with atmega32 or higher !
Now observe the result of your code . what you have written in your code is explained in figure below .
i).Initialize your LCD with some command that I have written above .
ii) Give some command to the LCD so that it can understand what you have intend to do.
iii) Finally send the data to the LCDto do these job we have to make separate function for the above operations to make easy to send desired data to the LCD. The function void init_LC(void) is used to initialize the lcd in proper mode and some basic operation of the lcd
LCD_cmd(0x38); // initialization of 16X2 LCD in 8bit mode
LCD_cmd(0x01); // clear LCD
LCD_cmd(0x0E); // cursor ON
LCD_cmd(0x80); // ---8 go to first line and --0 is for 0th position
After initializing the lcd in init_LCD() function .Look there is another function inside this function which is LCD_cmd(character ) function . this function is used to give the actual command to the LCD written in initialization part .
void LCD_cmd(unsigned char cmd)
After this define a another function to write actual data (ie character ) . for this we should create a function void LCD_write(unsigned char data).
Functions makes the program quite simple and short and easy to understand . so you should have habit of making function and writing separate header file for each of your complex project . we will discuss about the header file for the LCD in later on this chapter .
void LCD_write(unsigned char data)
ctrl = (1<<rs)|(0<<rw)|(1<<en);
ctrl = (1<<rs)|(0<<rw)|(0<<en);
You probably thinking about the line of code like this (1<<rs)|(0<<rw)|(1<<en) .
What is this code do ? well that is explained below. Just do not give up.
- Data pin8 (DB7(refer datasheet)) of the LCD is busy flag and is read when R/W = 1 & RS = 0. When busy flag=1, it means that LCD is not ready to accept data since it is busy with the internal operations. Therefore before passing any data to LCD, its command register should be read and busy flag should be checked.
- To send data on the LCD, data is first written to the data pins with R/W = 0 (to specify the write operation) and RS = 1 (to select the data register). A high to low pulse is given at EN pin when data is sent. Each write operation is performed on the positive edge of the Enable signal.
- To send a command on the LCD, a particular command is first specified to the data pins with R/W = 0 (to specify the write operation) and RS = 0 (to select the command register). A high to low pulse is given at EN pin when data is sent.
this much for the displaying character . what about displaying whole string (or a bunch of word or sentence ) in the LCD ? well in that case you should make another function using pointer .Inside the function you should make a array to display whole character . one word is made by using one-one character ,right ? so To send string on LCD:
- Make a string pointer variable
- Pass the starting address of string to that pointer variable
iii. Pass the string pointer value to the LCD_write function
- Increment the pointer value and go to step (iii.) till the value of pointer reaches NULL character.
Ok lets begin writing the code for the displaying text on the LCD.
that conclude our LCD interfacing in 8 bit mode . sometimes there is sortage of pins in the LCD in bigger project in that case you need to reduce the pin by some technique in this section we will discuss about the LCD operation in 4 bit mode .To operate the lcd on 4 bit mode lets modify our circuit as shown in below . use only 4 data wire instead !