Interfacing character LCD to AVR

In this tutorial, we will be interfacing a HD44780 character LCD. Although as an alternative you can use JHD162A LCD. Both follows the same instruction set along with the same pin diagram. Both these LCD’s are easily available in the market at a low cost. HD44780 LCD has a set of 16 columns and 2 rows for display.

HD44780 character LCD

The pin diagram of the LCD is as follows

pin diagram of the LCD

There are two ways to interface an LCD. One way is the 8bit mode which requires 11pins and the other ways is the 4 bit mode which requires only 7 pins. The 8-bit mode is faster than 4-bit mode but one has to compromise in the number of pins available for other operations. The pins 15 and 16 are used for back-light display. Pin15 has to be connected to positive supply while pin 16 to negative.

AVR LCD interface schematic

In this tutorial, we will be using the display in 4-bit mode. Also we won’t get into details of how the LCD work and what is its instruction sets. We will directly using a library to interface as it will be time-saving, efficient, and easy-to learn. Before starting kindly download the library here [LCd_library]. Before moving to the coding part, we will first configuring the library.

Adding And Configuring The Library

Before starting configuration of the library, make sure you have created a new project. I will be using AVR studio 5 , but the procedure remains the same for any other compiler. Please follow the illustrations given below

Adding_Library_avrstudio5

Right clock on your project name in the solution-explorer windows and navigate to ‘existing item’ as shown above.
Now browse to the downloaded library files and add both lcd.h as well as lcd.c

After adding the solution explorer window should look like this

LCD_project_tree

Now double click on the lcd.h file in the solution explorer windows. As soon as you do that, you will find lcd.h open up right next to your project file.c window just like tabs in any internet browser

Here comes the main part, of configuring the library. Scroll down very slowly until you find the following “ #define XTAL 1000000

define_xtal

The above line is responsible for calculating the delay in LCD operation.  Here, you have to define your crystal frequency irrespective of whether you are using internal or external clock. Currently it’s set to 1MHZ. To change it to 12MHZ just replace 1000000 by 12000000.

Scroll down until you find the following

AVR_interafce_to_LCD

As you can see , the above set of liens is responsible for selecting port to interface with the lcd. Currently it’s set to PORTD. If you want to use any other port like PORTB you can change PORTD to PORTB. As you continue to scroll down, you can find the list of functions that can be used like lcdgotoxy(x,y) ,lcd_home() that can be used directly in your code.

CODE

#include <avr/io.h>
#include <util/delay.h>

#include "lcd.h"

int main(void)
{
    lcd_init(LCD_DISP_ON); /* initialize lcd, display on */
    lcd_clrscr();             /* clear screen of lcd */

    while(1)                      /* infinite loop */
    {

        lcd_home();               /* bring cursor to 0,0 */
        lcd_puts("Welcome");      /*text to be displayed */
        lcd_gotoxy(8,1);          /* go to 2nd row 8th col */
        lcd_puts("To Embedds.com");  /* text to be displayed */
        _delay_ms(100);            /* wait 100ms */
    }
}

Explanation:

Initially the lcd is initialized to cursor off and display on command lcd_init(LCD_DIsply_ON). However if you want blinking cursor you can replace the initialization with lcd_init(LCD_DISPLAY_ON_CURSOR_BLINK) .More sets of initialization option can be found in  the library configuration as mentioned above. The next step clears everything written on lcd and then the code is self explanatory. However, the procedure to display numbers both integer and floating is different.

Displaying Numbers on the lcd

As of now, the library doesn’t support displaying numbers both integer and floating point. However there is a workaround for this. For displaying integer values, following things are to be added up

LCD_int_value

By declaring a ‘a’ character array, and using the itoa() function we are converting the number into an equivalent characters with name ‘a’ and then displaying “a”

For displaying floating values, the following changes are required

float

Make sure in both cases you include the delay as well as the lcd library. Also even if the code gets compiled, in the floating cases you might get a “?” mark. To solve you can navigate here and follow the steps. They worked for me.

By: Tushar Gupta

Read

Bookmark the permalink.

5 Comments

  1. When using 4bit operation… connect pin 1,3,5,7,8,9,and 10 to GND… You only need 6 signals to drive LCD…Pin 3 to GND will set highest contrast setting…

  2. yes you are right setting 3 pin to ground will set to high contrast and regarding 5 pin you can set it to low,till you are writing the data,but it won’t save you any pin as the library is configured to use that pin and you can’t use it for any other purpose. Also even if you are not using the library and using the general method by giving commands,you need to connect the read/write pin as before writing any data you have to check whether the lcd is busy or not.To check first you have to set high the read/write pin and then monitor the d7 bit until it goes low. Alternate way is to give a delay before writing the next data. Also regarding the 4 data pins 7,8,9,10 it’s not a necessary to connect them to ground.You can connect them if your display is not properly displaying the reuqired daya ,inspite of correct code. The lCd works for me by leaving these four pins unconnected

  3. By the way,thanks for reading the blog.If still you have any doubt you can comment below or even you can mail me at bondtushar@gmail.com .i will try to reply to your mail within 12 hours

  4. sir i want some lpc2148 projects. i want to learn.help me

Add Comment Register



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>