Interfacing LCD with Atmega32 Microcontroller using Atmel Studio

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

Share this post

Leave a Reply

66 Comments on "Interfacing LCD with Atmega32 Microcontroller using Atmel Studio"

avatar
  Subscribe  
newest oldest most voted
Notify of
RONY
Guest

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

RONY
Guest

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

RONY
Guest

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

Ligo George
Guest

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

Ligo George
Guest

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
Guest
Alexandre Frederico Leal

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

Ligo George
Guest

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

Alexandre Frederico Leal
Guest
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
Guest
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

Ligo George
Guest

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
Guest

Hi ! , how do I jump to row ??

Ligo George
Guest

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 = 0x80 + b;
z = temp>>4;
y = (0x80+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
Guest

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

Slavanator
Guest

that’s a great library! thanks, man

tsigab
Guest
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;… Read more »
Ligo George
Guest

Hello, your code is not using our library…

David Kräuter
Guest

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

I am using C++

HELP! 😉

Ligo George
Guest

Which is the statement producing this error??

vijayshan
Guest

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.

Ligo George
Guest

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

Yew Tze Ee
Guest

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

george
Guest

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

Sylvain Pétré
Guest
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’… Read more »
nila
Guest

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

Ligo George
Guest

Print it in a loop..

Neda
Guest

hi…..
i need lcd libraray with at32uc3a0512 not atmega32….can you help me ?

Ligo George
Guest

Hello, I haven’t the code for at32u3a0512….
but you can try modifying the above header file.. Logic is the same.. just change the output commands as it compatible with at32uc3a512..

Ahmed Hzzaa
Guest

I have a 4×20 LCD how can i control the cursor as when i move it from line 1 to 2 by your function ,, i want to move it to line 3 and 4 ??

Ligo George
Guest

Try reading the datasheet of HD44780 LCD Controller.

AliReza
Guest

Hi
I want write another customized header file to interfacing with LCD
can you help me on this?
such as giving a source to learn more about that… 😉

saikat
Guest

hii…i’m the beginner and i have a question? what does it mean by lcd8-int(); functio? and what kind of loop it is while(1) ??

Ligo George
Guest

while(1) is infinite loop…
Lcd8_Init() just initializes the LCD module in 8 bit interface mode.

Raptor150
Guest

Good morning, is it possibly connect displays pins
PD3 – D4

PD2 – D5

PD1 – D6

PD0 – D7

PD5 – RW

PD4 – E
Thank you for answers

Zaniar
Guest

hi. how i can put an variant in lcd8_write_string??????

Ligo George
Guest

Yes, it should work.

Ligo George
Guest

What you meant by variant ?

Jayk19
Guest

Hello can i put changing string value??
some like :

i++;
Lcd4_Write_string(i);

thanks for help

Jayk19
Guest

I solved by my own:)

vivek singh
Guest

do we have to include lcd.h file every time we are using lcd , i.e. if we are going to do next project do we have to again use lcd.h as add file

Ligo George
Guest

Yes, you should add lcd.h header file wherever you are using LCD.

Mahendra
Guest

how can i display the custom character on the lcd including time,date and temperature should be in degree celsius, but it is displaying all except degree celsius on lcd using avr microcontroller………..

venkatesh
Guest
i can use 4bit lcd interface with atmega16 and the below code on work in proteus but did not work in hardware. plz how to get the output in hardware.please suggested to me right know. all lcd pins connected to Uc properly #include #include #include #define rs PC1 #define rw PC2 #define en PC3 void lcd_init(); void dis_cmd(char); void dis_data(char); void lcdcmd(char); void lcddata(char); void LCD_str(unsigned char *p); int main(void) { unsigned char data0[11]=”ENGINEERS”; unsigned char data1[10]=”GARAGE”; unsigned int adc_val; int i=0; DDRC=0xFF; DDRA=0x00; lcd_init(); ADC_init(); dis_cmd(0x01); while(data0[i]!=”) { dis_data(data0[i]); _delay_ms(50); i++; } dis_cmd(0xC5); i=0; while(data1[i]!=”) { dis_data(data1[i]); _delay_ms(50); i++;… Read more »
Ligo George
Guest

Check whether the program is running fine or not using simple led blinking.

Shahadat hossain
Guest

is there any command
to show integer value on lcd?? plz me. i can’t show integer value

Vladimir Mamulov
Guest

Where is download button ?????????????????????

mm
Guest

where they say: [You can download header file, atmel studio files and proteus files here…

Interfacing LCD with Atmega32 Microcontroller with Atmel Studio] the download link is highlighted

Ligo George
Guest

Just convert that integer to string and display.

zezao
Guest

How did you do that?

Amit Anand
Guest

Hi,
Thanks for sharing this tutorial…It helped me a lot to up my 16×2 LCD display……I did some tweak but at last it was nice thanks for sharing……..