Oct-11th-2009

Interfacing LCD Modules with PIC Microcontrollers.

A large number of embedded project require some type of user interface. This includes displaying numerical, textual and graphical data to user. For very simple numerical display we can use 7 segment displays. If the requirement is little more than that, like displaying some alphanumeric text, we can use LCD Modules. They are cheap enough to be used in low cost projects. They come in various sizes for different requirement. A very popular one is 16x2 model. It can display 2 lines of 16 characters. Other models are 16x4,20x4, 8x1,8x2 etc.

In this tutorial we will learn how we can use such modules with Microchip PIC Microcontrollers. Here I will present my LCD library which you can use to create LCD based application/projects quickly. For demo I will use PIC18F4520 Microcontroller but you can use any PIC18 MCU. But you have to calculate the CONFIG values for correct setting and CPU clock selection etc. That means the chip should be configured correctly. See datasheet for more info on CONFIG bytes.

MPLAB Project Creation

First create a MPLAB project as described in this tutorial. Name the project LCD. Also add a main file called "lcd_test.c". To use my LCD library you need to add it to your project. Just copy/paste the following files to your project folder.

Header Files

  • lcd.h
  • myutils.h

Source File

  • lcd.c

How to add files to MPLAB Project is described here.

Now you are ready to the library functions to interact with the LCD module.

Sample Program (lcd_test.c)


/********************************************************************

16X2 ALPHANEUMERIC LCD INTERFACING LIBRARY TEST PROGRAM

---------------------------------------------------------

A testing program for our LCD library.

Easy to use library for interfacing 16x2 lcd in 4 bit mode.
MCU: PIC18FXXXX Series from Microchip.
Compiler: HI-TECH C Compiler for PIC18 MCUs (http://www.htsoft.com/)

Copyrights 2008-2009 Avinash Gupta
eXtreme Electronics, India

For More Info visit
http://www.eXtremeElectronics.co.in

Mail: me@avinashgupta.com

********************************************************************/
#include <htc.h>

#include "lcd.h"

//Chip Settings
__CONFIG(1,0x0200);
__CONFIG(2,0X1E1F);
__CONFIG(3,0X8100);
__CONFIG(4,0X00C1);
__CONFIG(5,0XC00F);


//Simple Delay Routine
void Wait(unsigned int delay)
{
   for(;delay;delay--)
      __delay_us(100);
}

void main()
{
   //Let the Module start up

   Wait(100);

   //Initialize the LCD Module
   LCDInit(LS_BLINK);

   //Clear the Module
   LCDClear();

   //Write a string at current cursor pos
   LCDWriteString("Good Is Great !!");

   Wait(20000);

   //Now Clear the display

   LCDClear();

   LCDWriteString("God Bless all !!");

   //Goto POS (X=0,Y=1 i.e. Line 2)
   //And Write a string
   LCDWriteStringXY(0,1,"<**************>");

   Wait(20000);

   //Write Some Numbers

   for(char i=0;i<100;i++)
   {
         LCDClear();
         LCDWriteInt(i,3);
         Wait(3000);
   }

   LCDClear();
   LCDWriteString("    The  End    ");

   //Loop Forever

   while(1);


}

The First thing you should do is to Initialize the LCD Module by calling the fuction LCDInit(). This will setup the LCD module. Now you can use the various functions like...

  • LCDClear() - For Clearing the screen.
  • LCDWriteString(unsigned char *) - For Writing Text Message at Current cursor position.
  • LCDWriteStringXY(x,y,msg) - For Writing Text Message at position x,y
  • LCDWriteInt() - For printing integer numbers at Current cursor position.
  • LCDWriteIntXY() - For printing integer numbers at position x,y.

For detailed information on these functions please refer to this tutorial. It is for the AVR microcontrollers but the functions are same for PIC mcus also.

Hardware Connections

Connect the LCD Module with PIC Microcontroller as shown below.

LCD Interface with PIC microcontroller schematic

Fig.: LCD Module Interface with PIC Microcontroller.

If you are using our PIC development board then you don't need to care for crystal or power supply (they are already on board). You just need to connect the LCD to PIC micro by using 7 i/o lines. 5v supply for LCD and Variable Resistor (10K) can be easily drawn from the board.

LCD Module Interface with PIC Microcontroller

Fig.: LCD Module Interface with PIC Microcontroller.

 

Fig.: LCD Module Interface with PIC Microcontroller.

Program the Micro controller with the HEX code using the ICSP method described here. Then adjust the Variable resistor until the display is clearly visible.

Videos

Coming Up !!!

Downloads

All files required for this experiment.

Microchip PIC Essential Tutorials


12 Responses to “Interfacing LCD Modules with PIC Microcontrollers.”

  1. 1
    AVR Micro Says:

    Fantastic!

    This is how a website for beginners should look like. Simple projects with clear explanation.

  2. 2
    Interfacing DS1307 RTC Chip with AVR Microcontroller | eXtreme Electronics Says:

    [...] LCD Interfacing library for Microchip PIC MCUs [...]

  3. 3
    r4i Says:

    This code is really useful for me I am an Engineering student and this code is very important for me Thank you very much for this useful post.

  4. 4
    sAnuB sAliM Says:

    Thanxz dude….

    This site is very useful to the beginners…. Thank u…
    Thanku so much…

  5. 5
    ankita Says:

    dis was realy helpful…
    but can we do the same ussing pic16f876a????
    how to include lcd.h header file.???
    pl rply

  6. 6
    Retellect Says:

    Thanks man! I used your tutorial long before I learnt embedded C and I always refer to it. I wrote a tutorial how to do the same in C, enjoy!

    http://hubpages.com/hub/How-to-write-text-to-an-LCD-using-a-pic-chip

  7. 7
    nirav Says:

    i am beginner in pic microcontroller and want some more info untill now what ever we got from your site i complttly sutisfy.

  8. 8
    Problem with Hi tech C and pic18f4550+lcd Says:

    [...] the error. this the bug of the compiler or the program problem. here is what the code i am trying Interfacing LCD Modules with PIC Microcontrollers. | eXtreme Electronics btw, anyone have LCD (jhd162a, i think is compatible with HD447800 ) library that using Hi tech c [...]

  9. 9
    SedoSan Says:

    Guys, I tried the write a string to LCD function but kept failing.
    My function was
    void Print_Word(const char *Word)
    {
    SendLCDdata(0×80, 0) //Starts first line
    while(*Word)
    SendLCDdata(*Word++, 1); //sends letters one by one
    }
    then in main i send something liek this
    Print_Word(“Hello World!”);
    I use MPLAB and MCC18 to compile in, I dont receive anything on my LCD, what is going wrong?

    I tried using MPLAB SIM debugger and check the *Word value, weirdly, it gave me “Restricted Memory”

    Would be much appreciated if someone can help me out with this.

  10. 10
    Making “The LCD Expansion Board” for PIC18F4520 | eXtreme Electronics Says:

    [...] recommend you to read the LCD Interfacing Tutorial before you proceed. It will give you an Idea how LCD is connected to PIC Microcontrollers. So lets [...]

  11. 11
    Interfacing LM35 Temperature Sensor with PIC Microcontroller. | eXtreme Electronics Says:

    [...] Interfacing LCD Module with PIC Microcontrollers. [...]

  12. 12
    sanyi Says:

    Thank you, it was wery helpful!

    Köszi…

Leave a Reply

Comments

    • Akhila: thank you sir.
    • kiran: Thank you Avinash..i didn’t read the user manual thats the problem ..any how thank you.....
    • Akhila: Sir, I’m working on my final yr proj which is based on “Swarmbots”.I would...
    • Uttam Dutta: RGB LED are expected from long before, now easily avilabe, thank you, may be breadboard...
    • Sayantan: sir,can u please upload the custom characters tutorial for a 16×2 lcd (4-wire)module.I have...
    • kiran: Good tutorial ..In last tutorial you created library for Progfx with extension of .lib file...
    • Abraham: Awesome tutorials….I have gone through most of them…and they are so simple and...

Video

  • Comments

    • Akhila: thank you sir.
    • kiran: Thank you Avinash..i didn’t read the user manual thats the problem ..any how thank you.....
    • Akhila: Sir, I’m working on my final yr proj which is based on “Swarmbots”.I would...
    • Uttam Dutta: RGB LED are expected from long before, now easily avilabe, thank you, may be breadboard...
    • Sayantan: sir,can u please upload the custom characters tutorial for a 16×2 lcd (4-wire)module.I have...
    • kiran: Good tutorial ..In last tutorial you created library for Progfx with extension of .lib file...
    • Abraham: Awesome tutorials….I have gone through most of them…and they are so simple and...