LCD INTERFACING WITH AVR

First we will learn operation modes of LCD and then, describe how to program LCD to an AVR microcontroller using C.

LCD OPERATION-

In recent years the LCD is finding widespread use replacing LEDs (seven-segment LEDs or multi-segment LEDs). This is due the following reasons-

  1. LCDs prices are declining.
  2. The ability to display number, characters and graphics. This is in contrast to LEDs, which are limited in numbers and a few characters.
  3. Ease of programming for characters and graphics.

TYPES OF LCD AND BLOCK DIAGRAM-

LCD can be of different types depending on the number of characters it can display on its screen. Each character which is display on LCD screen is displayed on a pixel box.

The LCD in above diagram is 2×16 LCD because it has 2 rows and in each row it can display 16 characters. So, total characters it can display is 32 (16×2=32). Different types of LCD depends on the number of characters it can display on it screen. Below is the number of different types of LCD-

  1. 2×16 LCD- This LCD has only one row and it can display maximum 16 characters.
  2. 3×16 LCD- This LCD has three rows and it can display 48 (16×3=48) characters.
  3. 4×16 LCD-This LCD has four rows which can display 72 characters on the display screen.

All these LCD works in a similar manner, only the difference in its display capacity of number of characters. Each character displayed inside pixel box has its own address. Given below is the basic diagram of 4×16 LCD which shows address of each pixel box-

The values written inside the square box is the address of each pixel box. Suppose we want the cursor to be moved to pixel box of address “0x89” so, we write command to LCD to move the cursor to “0x89” position. In this way, we are able to write at any character at any place inside LCD screen.

BLOCK DIAGRAM OF LCD-

LCD PIN DESCRIPTIONS-

First we have to discuss difference between command and data.

  • Command is given to LCD to do a specific function. These functions can be as follows-
  1. To clear the screen of LCD.
  2. Move the display cursor to a specific position.
  3. Shift cursor to right while writing data on LCD screen.
  • Data is given to LCD to display it on LCD screen.

So, we will understand to basic terms used in LCD. Now we will see pin descriptions of LCD.

 

The most common LCD used is LM041L. We will study this LCD and its programming in C. This LCD has 14 pins. The function of each pins has been discussed below.

  • VCC, VSS and VEE

Vcc and Vss pins provides +5 volt and ground to LCD respectively.

  • RS , register select

There are two important registers inside the LCD. The RS is used for their selection as follows. If RS=0, the instruction command code register is selected. This instruction command register allows the user to send commands such as clear display, set cursor to home etc. when RS=1, the data register is selected. This data register allows the user to send data to be displayed on the LCD.

  • R/W, read/write

R/W input allows the user to write information to LCD or read information from LCD. R/W=1, when reading; R/W=0, when writing.

  • E, enable

The enable pin used by the LCD to latch information presented on its data pins. When data is supplied to data pins, a high-to-low pulse must be applied to this pin in order for the LCD to latch in the data present on its data pins. This pulse must be a minimum of 450nsec wide. Suppose we have write something on data pins of LCD but LCD will recognize it only when its enable pin has high-to-low pulse.

  • D0-D7

The 8-bit data pins are used to send information to the LCD or read the contents of the LCD’s internal registers.

To display letters and numbers, we send ASCII codes for the letters A-Z, a-z and numbers 0-9 to these pins while making RS=1.

There are also instruction command codes that can be sent to the LCD to clear display or force the cursor to home position. List of these commands are shown below-

     CODE

(HEX CODE)

COMMAND TO LCD INSTRUCTION REGISTER
0x01 Clear Display Screen
0x02 Return Home
0x04 Decrement Cursor (shift cursor to left)
0x06 Increment Cursor (shift cursor to right)
0x05 Shift Display Right
0x07 Shift Display Left
0x08 Display OFF, Cursor OFF
0x0A Display OFF, Cursor ON
0x0C Display ON, Cursor OFF
0x0E Display ON, Cursor Blinking
0x0F Display ON, Cursor Blinking
0x10 Shift Cursor Position to Left
0x14 Shift Cursor Position to Right
0x18 Shift Entire Display to Left
0x1C Shift Entire Display to Right
0x80 Force Cursor to Beginning at 1st Line
0xC0 Force Cursor to Beginning at 2nd Line
0x28 2 lines and 5 x 7 matrix (D4-D7,4-bit)
0x38 2 lines and 5 x 7 matrix (D0-D7,8-bit)

 

There are two modes in which we can learn interfacing of LCD with AVR. These are 8-bit and 4-bit mode. 8-bit mode is easier but it uses 4 more data pins of LCD. Now we will study how to send commands and data to LCD.

SENDING COMMANDS AND DATA TO LCDs-

To send data and commands to LCD we have to follow the following steps-

  1. Initialize the LCD.
  2. Send command to LCD to choose the mode.
  3. Send the character to be shown on the LCD screen.

 

Initializing the LCD-

There are two modes in which we can initialize LCD i.e. 8-bit and 4-bit mode. We make a “lcd_init()” function which initializes LCD in one the mode. Given below is the steps which we have to follow while making “lcd_init()” function-

  1. Inside this function, first select mode in which we want to initializes LCD. Command ‘0x38’ is used to select-bit LCD mode and ‘0x28’ is used to select 4-bit LCD mode.
  2. Send LCD command ‘0x06’ to shift the cursor to right while writing on the LCD screen.
  3. Send LCD command ‘0x0E’ to display on and cursor blinking.

Sending Commands to the LCD-

To send any of the commands from the above table to the LCD, we have to follow the following steps-

  1. Put the command on data pins (D0-D7 in 8-bit mode).
  2. Make RS=0.
  3. Make RW=0.
  4. Send high-to-low pulse on enable pin to enable internal latch of the LCD. Wait for about 100usec to let the LCD module run that command.

Sending data to the LCD-

To send any of the data to be displayed on LCD screen, we have to follow the following steps-

  1. Place data on data pins (D0-D7 in 8-bit mode).
  2. Make RS=1.
  3. Make RW=0.
  4. Send high-to-low pulse on enable pin to enable internal latch of the LCD. Wait for about 100usec to let the LCD module to write that data on the LCD screen.

Now we have studied how to initiate, send command and send data to LCD. So, now we will do an example to learn how to program LCD in 8-bit or 4-bit mode.

 

EXAMPLE- Display “HAIL ELECTRONICS” string on the LCD screen. And RS, RW and ENABLE pin of LCD is connected to PORTC and the data pins of LCD is connected to PORTD.

ANSWER-

The connections of LCD and AVR atmega16/32 is shown in below figure-

We have connected RS, RW and ENABLE pin of LCD with PORTC so, we have to make first three pins of PORTC as output. The data pins of LCD is connected with PORTD, so PORTD should be output PORT. So first make these ports as output then, start programming of LCD. Now to program an LCD, we have to declare certain functions which have their specifics function. These functions are used to initiate LCD, send command to LCD, send data to LCD and display characters on LCD. So now we will study each of these functions.

1.  LCD_INIT() – To initiate LCD we have made “LCD_INIT ()” function. This function is used to select mode of LCD (in this example it is 8-bit mode), move cursor to right while we are writing on LCD screen etc. Given below is the declaration of this function-

In LCD_INIT() function we have used LCD_COMMAND() function which is used to send command to LCD. We will study its declaration below.

2.  LCD_COMMAND() – To send any command to LCD we have declare an “LCD_COMMAND()” function. This function received a character as input argument. This input character is command which this LCD_COMMAND() function received is the command which we want to send to LCD. Previously we have studied how to send commands to LCD, let we revise those steps-

  1. Put the command on data pins. Here data pins is connected to PORTD so, we put command on PORTD.
  2. Make RS=0. Here RS pin of LCD is connected to PINC0 so, we make PINC0 to ‘0’.
  3. Make RW=0. Here RW is connected to PINC1 so, we make PINC1 to ‘0’.
  4. Send high-to-low pulse on enable pin to enable internal latch of the LCD. Wait for about 100usec to let the LCD module run that command. Here ENABLE pin of LCD is connected to PINC2 so, we make high-to-low pulse on PINC2.

Now we will declare LCD_COMMAND() function as follows-

3. LCD_DATA() – This function is used to send the data to LCD. Suppose we want to display ‘A’ on LCD screen so, we send character ‘A’ to this function and this function will display that character on LCD screen. So, this function takes input argument as character which is to be displayed on LCD screen. We have studied how to send data to LCD. Let’s revise those steps here as follows-

  1. Place data on data pins. Here data pins is connected to PORTD so, we put data on PORTD.
  2. Make RS=1. Here RS pin of LCD is connected to PINC0 so, we make PINC0 to ‘1’.
  3. Make RW=0. Here RW is connected to PINC1 so, we make PINC1 to ‘0’.
  4. Send high-to-low pulse on enable pin to enable internal latch of the LCD. Wait for about 100usec to let the LCD module to write that data on LCD screen. Here ENABLE pin of LCD is connected to PINC2 so, we make high-to-low pulse on PINC2.

Now we will declare LCD_DATA() function as follows-

4. LCD_DISPLAY() – In this example, we want to display “HAIL ELECTRONICS” string. Now LCD_DISPLAY() function takes the string to be displayed on LCD and it displays that string on LCD screen.

Now we will declare LCD_DISPLAY() function as follows-

We have to note following points regarding this function-

  • We have passed string in this function.
  • We want to display each character present in the string which we have pass to LCD_DISPLAY() function.
  • In this function, we initiated an while() loop which ends till we have reached the end of the string. We know at the end of string there is NULL character. So, we use this concept in while() loop.
  • Inside this while() loop we passed each character to LCD_DATA() function to be displayed on LCD screen.
  • After passing character to LCD_DISPLAY() function we incremented pointer which pointed to that string. So, that other character is able to be displayed on LCD screen.

 Now we have studied all the functions used in the program to display given string on LCD screen so, we can write complete program code to display string on LCD in 8-bit mode-

PROGRAM CODE-

We have studied how to program LCD in 8-bit mode. Now we will consider another example to program LCD in 4-bit mode.

EXAMPLE- Display “www.hailelectronics.com” string on LCD screen but this string should scroll from right to left on LCD screen. Use 4-bit mode to program LCD. Connect RS, RW and ENABLE pin of LCD with PINC0, PINC1 and PINC2 of PORTC. Also connect data pins of LCD with PINC4, PINC5, PINC6 and PINC7 of PORTC.

ANSWER-

The connection of LCD and AVR atmega16/32 as shown in figure below-

We have connected higher data pins of LCD i.e. D4, D5, D6 and D7 with PINC4, PINC5, PINC6 and PINC7 respectively.

Now we will declare certain functions as in previous example to program AVR as follows-

1. LCD_INIT() – This function is used to select mode of LCD (in this example it is 4-bit mode), move cursor to right while we are writing on LCD screen etc. Given below is the declaration of this function-

In LCD_INIT() function we have used LCD_COMMAND() function which is used to send command to LCD. We will study its declaration below-

2. LCD_COMMAND() – We know how to declare this function (as studied above) but there is small difference in declaration of this function in 4-bit mode. As we have connected higher bits of data pins of LCD to AVR. So, first we send higher four bits of command to LCD then, send lower fours bits of command to LCD. We following concept to do this-

Suppose ‘x’ is the command that we want to send to LCD. So first we write higher four bits of command on PORTC as follows-

PORTC=x&0xF0.

Then, we write four lower bits of commands as follows-

PORTC=(x<<4)&0xF0.

This is the only difference in 4-bit and 8-bit mode. In 4-bit mode, we use only 4 fours pins to send data/command to LCD. That is why, we are sending those 8-bit data/command in two parts. “LCD_COMMAND()” function is declared as follows-

3.  LCD_DATA() – We know that this function is used to send the data to LCD. This function send data to LCD in following manner-

Suppose ‘x’ is the data that we want to send to LCD. So first we write higher four bits of data on PORTC as follows-

PORTC=x&0xF0.

Then, we write four lower bits of data as follows-

PORTC=(x<<4)&0xF0.

Now we are not discussing LCD_DISPLAY() function as it remains same as in previous example.

We have studied declaration of all functions which are used in programming of LCD in 4-bit mode. So , now we are able to write program code as given below-