In this project LCD is working in 8-bit mode i.e., the data transferred to the LCD must be in 8-bit data form. The PortA of ATmega16 is connected to data pins of LCD and is defined as LCD_DATA. PortB is defined as control pins (Rs, R/W and En).
The following steps explain in detail how LCD can be interfaced with ATMEGA 16:
Step1: To initialize LCD
The following instructions are used to initialize LCD.
· 0x38 to initialize LCD in 8-bit mode
· 0x01 to clear LCD
· 0x0E to make cursor ON
· 0x80 to go to first line and 0th position in LCD
Delay of around 50 micro second is necessary b/w two commands for execution of instructions.
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
Step2: Define command function
To send any command to LCD
· Transfer the command to LCD_DATA port
· Set RS and RW bit to go LOW and enable as HIGH
· Give a small delay
· Set enable to LOW
void LCD_cmd(unsigned char cmd)
ctrl =(0<<rs)|(0<<rw)|(1<<en); // RS and RW as LOW and EN as HIGH
ctrl =(0<<rs)|(0<<rw)|(0<<en); // RS, RW , LOW and EN as LOW
Step3: Define write function
To display any data on LCD
· Transfer the data (Ascii value of the character to be sent) to LCD_DATA port.
· Make RW as LOW and set RS and enable as HIGH.
· Give a small delay.
· Make enable pin LOW
void LCD_write(unsigned char data)
ctrl = (1<<rs)|(0<<rw)|(1<<en); // RW as LOW and RS, EN as HIGH
ctrl = (1<<rs)|(0<<rw)|(0<<en); // EN and RW as LOW and RS HIGH
_delay_ms(50); // delay to get things executed