Interfacing LCD with Atmega32 Microcontroller using Atmel Studio

As we all know LCD (Liquid Crystal Display) is an electronic display which is commonly used nowadays in applications such as calculators, laptops, tablets, mobile phones etc. 16×2 character LCD module is a very basic module which is commonly used by electronic hobbyists and is used in many electronic devices and project. It can display 2 lines of 16 character and each character is displayed using 5×7 or 5×10 pixel matrix.

16x2 Character LCD

16×2 Character LCD

Interfacing 16×2 LCD with Atmega32 Atmel AVR Microcontroller using Atmel Studio is bit complex as there is no built in libraries. To solve this difficulty we developed a LCD library which includes the commonly used features. Just include our header file and enjoy. You can download the header file from the bottom of this article.

16×2 LCD can be interfaced with a microcontroller in 8 Bit or 4 Bit mode. These differs in how data and commands are send to LCD. In 8 Bit mode character data (as 8 bit ASCII) and LCD command are sent through the data lines D0 to D7. That is 8 bit data is send at a time and data strobe is given through E of the LCD.

But 4 Bit mode uses only 4 data lines D4 to D7. In this 8 bit data is divided into two parts and are sent sequentially through the data lines. The idea of 4 bit communication is introduced to save pins of microcontroller. 4 bit communication is bit slower than 8 bit but this speed difference has no significance as LCDs are slow speed devices. Thus 4 bit mode data transfer is most commonly used.

Function in lcd.h

Lcd8_Init() & Lcd4_Init() : These functions will initialize the 16×2 LCD module connected to the microcontroller pins defined by the following constants.

For 8 Bit Mode :

#define D0 eS_PORTD0
#define D1 eS_PORTD1
#define D2 eS_PORTD2
#define D3 eS_PORTD3
#define D4 eS_PORTD4
#define D5 eS_PORTD5
#define D6 eS_PORTD6
#define D7 eS_PORTD7
#define RS eS_PORTC6
#define EN eS_PORTC7

For 4 Bit Mode :

#define D4 eS_PORTD4
#define D5 eS_PORTD5
#define D6 eS_PORTD6
#define D7 eS_PORTD7
#define RS eS_PORTC6
#define EN eS_PORTC7

These connections must be defined for the proper working of the LCD library. Don’t forget to define these pins as Output.

Lcd8_Clear() & Lcd4_Clear() : Calling these functions will clear the 16×2 LCD display screen when interfaced with 8 bit and 4 bit mode respectively.

Lcd8_Set_Cursor() & Lcd4_Set_Cursor() : These function will set the cursor position on the LCD screen by specifying its row and column. By using these functions we can change the position of character and string displayed by the following functions.

Lcd8_Write_Char() & Lcd4_Write_Char() : These functions will write a single character to the LCD screen and the cursor position will be incremented by one.

Lcd8_Write_String() & Lcd8_Write_String() : These function will write string or text to the LCD screen and the cursor positon will be incremented by length of the string plus one.

Lcd8_Shift_Left() & Lcd4_Shift_Left() : This function will shift data in the LCD display without changing data in the display RAM.

Lcd8_Shift_Right() & Lcd8_Shift_Right() : This function will shift data in the LCD display without changing data in the display RAM.

8 Bit Mode Interfacing

Circuit Diagram

Interfacing LCD with Atmega32 Microcontroller 8 Bit Mode

Interfacing LCD with Atmega32 Microcontroller 8 Bit Mode

Atmel Studio C Code

#ifndef F_CPU
#define F_CPU 16000000UL // 16 MHz clock speed
#endif

#define D0 eS_PORTD0
#define D1 eS_PORTD1
#define D2 eS_PORTD2
#define D3 eS_PORTD3
#define D4 eS_PORTD4
#define D5 eS_PORTD5
#define D6 eS_PORTD6
#define D7 eS_PORTD7
#define RS eS_PORTC6
#define EN eS_PORTC7

#include <avr/io.h>
#include <util/delay.h>
#include "lcd.h" //Can be download from the bottom of this article
int main(void)
{
  DDRD = 0xFF;
  DDRC = 0xFF;
  int i;
  Lcd8_Init();
  while(1)
  {
    Lcd8_Set_Cursor(1,1);
    Lcd8_Write_String("electroSome LCD Hello World");
    for(i=0;i<15;i++)
    {
      _delay_ms(500);
      Lcd8_Shift_Left();
    }
    for(i=0;i<15;i++)
    {
      _delay_ms(500);
      Lcd8_Shift_Right();
    }
    Lcd8_Clear();
    Lcd8_Write_Char('e');
    Lcd8_Write_Char('S');
    _delay_ms(2000);
  }
}

4 Bit Mode Interfacing

Circuit Diagram

Interfacing LCD with Atmega32 Microcontroller 4 Bit Mode

Interfacing LCD with Atmega32 Microcontroller 4 Bit Mode

Atmel Studio C Code

#ifndef F_CPU
#define F_CPU 16000000UL // 16 MHz clock speed
#endif
#define D4 eS_PORTD4
#define D5 eS_PORTD5
#define D6 eS_PORTD6
#define D7 eS_PORTD7
#define RS eS_PORTC6
#define EN eS_PORTC7

#include <avr/io.h>
#include <util/delay.h>
#include "lcd.h" //Can be download from the bottom of this article

int main(void)
{
  DDRD = 0xFF;
  DDRC = 0xFF;
  int i;
  Lcd4_Init();
  while(1)
  {
    Lcd4_Set_Cursor(1,1);
    Lcd4_Write_String("electroSome LCD Hello World");
    for(i=0;i<15;i++)
    {
      _delay_ms(500);
      Lcd4_Shift_Left();
    }
    for(i=0;i<15;i++)
    {
      _delay_ms(500);
      Lcd4_Shift_Right();
    }
    Lcd4_Clear();
    Lcd4_Set_Cursor(2,1);
    Lcd4_Write_Char('e');
    Lcd4_Write_Char('S');
    _delay_ms(2000);
  }
}

How to use the Header File?

You can download the lcd.h header file at the bottom of this article. Add the lcd.h to your project source group.

  1. Right Click on your project folder on the solution explorer on the right side.
  2. Add >> Existing Item
  3. Then Browse lcd.h
  4. Click Add

Optimize Code for More Efficiency

You already seen that by using our header file lcd.h, you can connect your 16×2 LCD to any of the output pins of the microcontroller. More coding is required for this feature which reduces the code efficiency and increases the size of hex file. You can solve this problem by making simple changes in the header file according to your LCD connections. For example consider above sample programs. I have used PORTD for sending data, 6th bit of PORTC as RS and 7th bit of PORTC as EN.

  • Change Lcd4_Port(data) and Lcd8_Port(data) to PORTD = data
  • Change pinChange(EN,1) to  PORTC |= (1<<PC7)
  • Change pinChange(EN,0) to PORTC &= ~(1<<PC7)
  • Change pinChange(RS,1) to PORTB |= (1<<PB6)
  • Change pinChange(RS,0) to PORTC &= ~(1<<PC6)

You can download header file, atmel studio files and proteus files here…

Interfacing LCD with Atmega32 Microcontroller with Atmel Studio



  • RONY

    where are the remaining 2 header files.????????????????????

  • RONY

    why those 2 header files are not included in download.????

  • RONY

    whtat is the meaning of providing the code without all the header files.???

  • http://www.electrosome.com/ Ligo George

    The remaining 2 header files are the default header files of Atmel Studio..

  • http://www.electrosome.com/ Ligo George

    LCD.H header file is used to simplifying the code.. it makes the code more readable… and simple… I will explain the details of interfacing lcd with a microcontroller in one of posts..

  • Alexandre Frederico Leal

    What should I change to use the internal clock???

  • http://www.electrosome.com/ Ligo George

    YOu should edit the CKSEL 3-0 bit.. for Calibrated internal RC oscillator as given in the datasheet..

  • Alexandre Frederico Leal

    HI I don’t understand the #define es_PORTXX Y on header, the Y waas supposed to be the pin number on datasheet? What the Y means?

    I tried to change the programm to use as 4-bits LCD on ports:

    D7-B0 PIN1
    D6-B1 PIN2
    D5-B2 PIN3
    D4-B3 PIN4
    E-B4 PIN5
    RS-B5 PIN6

    So I changed on program:

    #define D4 eS_PORTD4
    #define D5 eS_PORTD5
    #define D6 eS_PORTD6
    #define D7 eS_PORTD7
    #define RS eS_PORTC6
    #define EN eS_PORTC7

    to

    #define D4 eS_PORTB3
    #define D5 eS_PORTB2
    #define D6 eS_PORTB1
    #define D7 eS_PORTB0
    #define RS eS_PORTB4
    #define EN eS_PORTB5

    but it don’t work.

  • Alexandre Frederico Leal

    I already find the problem, I was inverting the pins on define.

    Here is the correct one:

    #define DB4 eS_PORTB0
    #define DB5 eS_PORTB1
    #define DB6 eS_PORTB2
    #define DB7 eS_PORTB3
    #define RS eS_PORTB4
    #define EN eS_PORTB5

    Thx for all

  • http://www.electrosome.com/ Ligo George

    Ok thanks for the feedback….please optimize the code before using it in hardware… otherwise it will consume more memory.. resulting in less efficiency..

  • Janazami

    Hi ! , how do I jump to row ??

  • http://www.electrosome.com/ Ligo George

    It is a mistake in the header file.. substitute the set cursor function for 4 bit with the following code..

    void Lcd4_Set_Cursor(char a, char b)
    {
    char temp,z,y;
    if(a == 1)
    {
    temp = 0×80 + b;
    z = temp>>4;
    y = (0×80+b) & 0x0F;
    Lcd4_Cmd(z);
    Lcd4_Cmd(y);
    }
    else if(a == 2)
    {
    temp = 0xC0 + b;
    z = temp>>4;
    y = (0xC0+b) & 0x0F;
    Lcd4_Cmd(z);
    Lcd4_Cmd(y);
    }
    }

  • Er Chirag Bumtariya

    how to download lcd.h file ?????????
    plz send me the whole program of both 4 & 8 bit datalines………..

  • Slavanator

    that’s a great library! thanks, man

  • tsigab

    hello would you please help me troubleshoot this code i tried to interfaceATMEGA32A and programme it in 8bit lcd mode . it kept displaying the following message oohrhwhrhdhewhhww….., the code is as follows:

    #include

    #include

    #define LCD_DPRT PORTB

    #define LCD_DDDR DDRB

    #define LCD_DPIN PINB

    #define LCD_CPRT PORTA

    #define LCD_CDDR DDRA

    #define LCD_CPIN PINA

    #define LCD_RS 0

    #define LCD_RW 1

    #define LCD_EN 2

    void lcdCommand(unsigned char cmnd);

    void lcd_data(unsigned char data);

    void lcd_gotoxy(unsigned char x, unsigned char y);

    void lcd_init(void);

    void lcd_print(char *str);

    void lcdCommand(unsigned char cmnd)

    {

    LCD_DPRT =cmnd;

    LCD_CPRT &=~((1<<LCD_RS) |(LCD_RW));

    _delay_us(1);

    LCD_CPRT |=1<<LCD_EN;

    _delay_us(1);

    LCD_CPRT &=~(1<<LCD_EN);

    _delay_us(10);

    LCD_DPRT=0;

    }

    void lcd_data(unsigned char data)

    {

    LCD_DPRT =data;

    LCD_CPRT |= 1<<LCD_RS;

    LCD_CPRT &= ~ (1<<LCD_RW);

    _delay_us(1);

    LCD_CPRT |= (1<<LCD_EN);

    _delay_us(1);

    LCD_CPRT &= ~ (1<<LCD_EN);

    _delay_us(10);

    LCD_DPRT=0;

    }

    void lcd_init()

    {

    LCD_DDDR =0xFF;

    LCD_CDDR =0xFF;

    LCD_CPRT &=~(1<<LCD_EN);

    _delay_us(2000);

    lcdCommand(0×38);

    // _delay_us(10);

    lcdCommand(0x0E);

    // _delay_us(10);

    lcdCommand(0×01);

    _delay_us(2000);

    lcdCommand(0×06);

    }

    void lcd_gotoxy(unsigned char x, unsigned char y)

    {

    unsigned char firstCharAdr[]={0×80,0x0C};//,0×90,0xD4};

    lcdCommand( 0×80 + firstCharAdr[y-1] + x-1);

    _delay_us(100);

    }

    void lcd_print(char *str)

    {

    unsigned char i;

    while(str[i] !=0)

    {

    lcd_data(str[i]);

    i++;

    }

    }

    int main(void)

    {

    lcd_init();

    while(1)

    {

    lcd_gotoxy(1,1);

    lcd_print("hello");

    lcd_gotoxy(2,1);

    lcd_print("world");

    }

  • http://www.electrosome.com/ Ligo George

    Hello, your code is not using our library…

  • David Kräuter

    Warning 1 deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

    I am using C++

    HELP! ;)

  • http://www.electrosome.com/ Ligo George

    Which is the statement producing this error??

  • vijayshan

    hey , i want to create this program so that it only uses portd , not any other pins in port !! suggest the improvements , any change in header file of functions.

  • http://www.electrosome.com/ Ligo George

    Read the “Optimize Code for More Efficiency” section above and make changes to the header file accordingly..

  • Yew Tze Ee

    Hey George, I want to use 20×4 LCD module, what modification should I make?

  • george

    as i used the code, i get delays as high as 5 secs between character shift

  • Sylvain Pétré

    I want to program for a Atmega32u4
    But many errors :

    Error 1 ‘PORTA’ undeclared (first use in this function) c:program filesatmelavr studio 5.1extensionsatmelavrgcc3.3.1.27avrtoolchainbin../lib/gcc/avr/4.5.1/../../../../avr/include/util/lcd.h 49 5 Leonardo32u4_LCD
    Warning 2 each undeclared identifier is reported only once for each function it appears in c:program filesatmelavr studio 5.1extensionsatmelavrgcc3.3.1.27avrtoolchainbin../lib/gcc/avr/4.5.1/../../../../avr/include/util/lcd.h 49 5 Leonardo32u4_LCD
    Error 3 ‘PA0′ undeclared (first use in this function) c:program filesatmelavr studio 5.1extensionsatmelavrgcc3.3.1.27avrtoolchainbin../lib/gcc/avr/4.5.1/../../../../avr/include/util/lcd.h 49 19 Leonardo32u4_LCD
    Error 4 ‘PA1′ undeclared (first use in this function) c:program filesatmelavr studio 5.1extensionsatmelavrgcc3.3.1.27avrtoolchainbin../lib/gcc/avr/4.5.1/../../../../avr/include/util/lcd.h 51 19 Leonardo32u4_LCD
    Error 5 ‘PA2′ undeclared (first use in this function) c:program filesatmelavr studio 5.1extensionsatmelavrgcc3.3.1.27avrtoolchainbin../lib/gcc/avr/4.5.1/../../../../avr/include/util/lcd.h 53 19 Leonardo32u4_LCD
    Error 6 ‘PA3′ undeclared (first use in this function) c:program filesatmelavr studio 5.1extensionsatmelavrgcc3.3.1.27avrtoolchainbin../lib/gcc/avr/4.5.1/../../../../avr/include/util/lcd.h 55 19 Leonardo32u4_LCD
    Error 7 ‘PA4′ undeclared (first use in this function) c:program filesatmelavr studio 5.1extensionsatmelavrgcc3.3.1.27avrtoolchainbin../lib/gcc/avr/4.5.1/../../../../avr/include/util/lcd.h 57 19 Leonardo32u4_LCD
    Error 8 ‘PA5′ undeclared (first use in this function) c:program filesatmelavr studio 5.1extensionsatmelavrgcc3.3.1.27avrtoolchainbin../lib/gcc/avr/4.5.1/../../../../avr/include/util/lcd.h 59 19 Leonardo32u4_LCD
    Error 9 ‘PA6′ undeclared (first use in this function) c:program filesatmelavr studio 5.1extensionsatmelavrgcc3.3.1.27avrtoolchainbin../lib/gcc/avr/4.5.1/../../../../avr/include/util/lcd.h 61 19 Leonardo32u4_LCD
    Error 10 ‘PA7′ undeclared (first use in this function) c:program filesatmelavr studio 5.1extensionsatmelavrgcc3.3.1.27avrtoolchainbin../lib/gcc/avr/4.5.1/../../../../avr/include/util/lcd.h 63 19 Leonardo32u4_LCD
    Error 11 ‘PC0′ undeclared (first use in this function) c:program filesatmelavr studio 5.1extensionsatmelavrgcc3.3.1.27avrtoolchainbin../lib/gcc/avr/4.5.1/../../../../avr/include/util/lcd.h 81 19 Leonardo32u4_LCD
    Error 12 ‘PC1′ undeclared (first use in this function) c:program filesatmelavr studio 5.1extensionsatmelavrgcc3.3.1.27avrtoolchainbin../lib/gcc/avr/4.5.1/../../../../avr/include/util/lcd.h 83 19 Leonardo32u4_LCD
    Error 13 ‘PC2′ undeclared (first use in this function) c:program filesatmelavr studio 5.1extensionsatmelavrgcc3.3.1.27avrtoolchainbin../lib/gcc/avr/4.5.1/../../../../avr/include/util/lcd.h 85 19 Leonardo32u4_LCD
    Error 14 ‘PC3′ undeclared (first use in this function) c:program filesatmelavr studio 5.1extensionsatmelavrgcc3.3.1.27avrtoolchainbin../lib/gcc/avr/4.5.1/../../../../avr/include/util/lcd.h 87 19 Leonardo32u4_LCD
    Error 15 ‘PC4′ undeclared (first use in this function) c:program filesatmelavr studio 5.1extensionsatmelavrgcc3.3.1.27avrtoolchainbin../lib/gcc/avr/4.5.1/../../../../avr/include/util/lcd.h 89 19 Leonardo32u4_LCD
    Error 16 ‘PC5′ undeclared (first use in this function) c:program filesatmelavr studio 5.1extensionsatmelavrgcc3.3.1.27avrtoolchainbin../lib/gcc/avr/4.5.1/../../../../avr/include/util/lcd.h 91 19 Leonardo32u4_LCD

    WOW ?

  • nila

    hey i want to scroll only a particular line, how to do…

  • http://www.electrosome.com/ Ligo George

    Print it in a loop..