Interfacing LCD 16x2 with ATMEGA32 AVR Microcontroller in 4-bit mode

VCC - connect to 5V
VSS - connect to ground
VEE - contrast adjustment
RS - register select 0:command, 1:data
RW - read/write 0:write 1:read
EN - Enable, falling edge trigged
D0-D7 - Data pins
A/LED+ - Back-light Anode(+)
K/LED- -Back-light Cathode(-)

The internal circuit of the LCD

Blog Diagram
Layers of LCD Display

Specifications of LCD Display

• Input Voltage – 5.0 V
• Supply Current – 1.0 mA (without backlight)
• Display format – 16*2 characters
• Duty Cycle – 1/16
• System Interface – 4 bit or 8 bit
• 5*8 dots include cursor
• Built – in – controller: ST 7066 (or equivalent)
• Operating Temperature 0-50℃
• Storage Temperature -10℃ − 60℃

Functions in LCD.h file

#ifndef LCD_H_#define LCD_H_
#include <avr/io.h>#define F_CPU 8000000UL#include <util/delay.h>#define LCD_CMD_CLEAR_DISPLAY 0x01#define LCD_CMD_CURSOR_HOME 0x02// Display control#define LCD_CMD_DISPLAY_OFF 0x80#define LCD_CMD_DISPLAY_NO_CURSOR 0x0c#define LCD_CMD_DISPLAY_CURSOR_NO_BLINK 0x0E#define LCD_CMD_DISPLAY_CURSOR_BLINK 0x0F// Function set#define LCD_CMD_4BIT_2ROW_5X7 0x28#define LCD_CMD_8BIT_2ROW_5X7 0x38//LCD DATA AND CONTROL PORTS#define DATA_BUS PORTA#define CTL_BUS PORTB#define DATA_DDR DDRA#define CTL_DDR DDRB//LCD DATA PINS#define LCD_D4 4#define LCD_D5 5#define LCD_D6 6#define LCD_D7 7// LCD CONTROL PINS#define LCD_EN 3#define LCD_RW 2#define LCD_RS 1//functions prototypevoid lcd_init(void);void lcd_send_command (uint8_t );void lcd_write_character(uint8_t );void lcd_write_word(uint8_t[]);void lcd_clear(void);void lcd_set_courser(uint8_t,uint8_t);void lcd_goto_xy (uint8_t , uint8_t );

Circuit diagram

Circuit diagram of LCD display

Atmel Studio C code

#include "lcd2.h"/**Function name  : lcd_init*Parameters     : void*return       : void*purpose        : initialize LCD pins as output*       and setting up the operation mode(4-bit)*      default setting (cursor on)*/void lcd_init(void){DATA_DDR = (1<<LCD_D7) | (1<<LCD_D6) | (1<<LCD_D5)| (1<<LCD_D4);CTL_DDR |= (1<<LCD_EN)|(1<<LCD_RW)|(1<<LCD_RS);DATA_BUS = (0<<LCD_D7)|(0<<LCD_D6)|(1<<LCD_D5)|(0<<LCD_D4);CTL_BUS|= (1<<LCD_EN)|(0<<LCD_RW)|(0<<LCD_RS);_delay_ms(5);CTL_BUS &=~(1<<LCD_EN);_delay_ms(5);lcd_send_command(LCD_CMD_4BIT_2ROW_5X7); /*2 line,5*7 matrix in 4-bit mode*/_delay_ms(5);lcd_send_command(LCD_CMD_DISPLAY_CURSOR_BLINK); /*Auto Increment cursor*/_delay_ms(5);lcd_send_command(0x80); /*display off*/}/**Function name  : lcd_send_command*Parameters: uint8_t command*return: void*purpose: sending a command to LCD by sending*       the first nibble then the second nibble*      enabling and disabling the LCD in between*/void lcd_send_command (uint8_t command){DATA_BUS=((command&0b11110000)); /*Sending upper nibble*/CTL_BUS &=~(1<<LCD_RS);  /*RS=0, command reg*/CTL_BUS |=(1<<LCD_EN);   /*Enable pulse*/_delay_ms(5);CTL_BUS &=~(1<<LCD_EN);_delay_ms(5);DATA_BUS=((command&0b00001111)<<4); /*Sending lower nibble*/CTL_BUS |=(1<<LCD_EN);_delay_ms(5);CTL_BUS &=~(1<<LCD_EN);_delay_ms(5);}/**Function name  : lcd_write_word*Parameters     : uint8_t word[20]*return       : void*purpose        : printing a full word to the*       LCD (Maximum 20 characters)*/void lcd_write_word(uint8_t word[20]){int i=0;while(word[i]!='\0'){lcd_write_character(word[i]);i++;}}/**Function name  : lcd_write_character*Parameters: uint8_t character*return: void*purpose: sending one character to LCD by sending*        the first nibble first then the second nibble*       enabling and disabling the LCD in between*/void lcd_write_character(uint8_t character){DATA_BUS=((character & 0b11110000));CTL_BUS|=(1<<LCD_RS);CTL_BUS |=(1<<LCD_EN);_delay_ms(5);CTL_BUS &=~(1<<LCD_EN);_delay_ms(5);DATA_BUS=((character & 0b00001111)<<4);CTL_BUS |=(1<<LCD_EN);_delay_ms(5);CTL_BUS &=~(1<<LCD_EN);_delay_ms(5);}/**Function name  : lcd_clear*Parameters     : void*return       : void*purpose        : Clearing the LCD screen by sending*      the LCD_CMD_CLEAR_DISPLAY command to LCD*/void lcd_clear(void){lcd_send_command(LCD_CMD_CLEAR_DISPLAY);_delay_ms(5);}void lcd_goto_xy (uint8_t line,uint8_t pos)    //line = 0 or 1{lcd_send_command((0x80|(line<<6))+pos);_delay_us (50);}
int main(void){/* Replace with your application code */lcd_init();_delay_ms(20);lcd_init();
lcd_write_word("Hello Coders"); //Display "Not Registered"



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store