Interfacing 16×2 lcd with atmega16/32 AVR

To interface LCD with the microcontroller in default configuration requires 3 control signals and 8 data lines.This is known as 8 bit interfacing mode which requires total 11 I/O lines.

16x2 lcd display
Typical LCD Display

To reduce the number of I/Os required for LCD interfacing we can use 4 bit interfacing mode which requires 3 control signals with 4 data lines. In this mode upper nibble and lower nibble of commands/data set needs to be sent separately. Below block diagram shows LCD interfacing in 4 bit mode. The three control lines are referred to as EN, RS, and RW.

LCD Module Pins
Microcontroller Pin LCD Pin Description
VCC VCC Supply Voltage 5V
GND GND Ground 0V
PC0 RS (Control Line) Read/Write
PC1 RW (Control Line) Enable
PC2 EN (Control Line) Enable
PC4 – PC7 D4 – D7 (Data Line) Bidirectional Data
Back Light LCD Light +- LCD Back Light

Enable:

The EN line is called “Enable” and it is connected to PC2. This control line is used to tell the LCD that microcontroller has sent data to it or microcontroller is ready to receive data from LCD. This is indicated by a high-to-low transition on this line. To send data to the LCD, program should make sure that this line is low (0) and then set the other two control lines as required and put data on the data bus. When this is done, make EN high (1) and wait for the minimum amount of time as specified by the LCD datasheet, and end by bringing it to low (0) again.

Register Select:

The RS line is the “Register Select” line and it is connected to PC0. When RS is low (0), the data is treated as a command or special instruction by the LCD (such as clear screen, position cursor, etc.). When RS is high (1), the data being sent is treated as text data which should be displayed on the screen.

Read/Write:

The RW line is the “Read/Write” control line and it is connected to PC1. 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 from) the LCD.

Bi-Directional Data Bus:

The data bus is bidirectional, 4 bit wide and is connected to PC4 to PC7 of the microcontroller.
The MSB(Most Significant Bit) bit (DB7) of data bus is also used as a Busy flag. When the Busy flag is 1, the LCD is in internal operation mode, and the next instruction will not be accepted. When RS = 0 and R/W = 1, the Busy flag is output on DB7. The next instruction must be written after ensuring that the busy flag is 0.

Schematic of Interfacing  16×2 Lcd with AVR

Interfacing LCD 16X2 LCD Atmega16
Schematic Diagram

 

Programming:

 The program provided below is for understanding purpose only. You can download AVR Studio files here

LCD header file functions:

The function in lcd.h file are defined we are calling these functions in main function.See the functions explained below:

lcd_clrscr(); : To clear the LCD and reset the cursor to initial state.

lcd_int : Initializing the LCD Display.

LCD_MOVE_DISP_LEFT : This command is used to sift text one character left.

lcd_gotoxy(column,row) : To move cursor to a row or column.

Download LCD 16×2 Atmega