Featured post

A reason to exercise.

It's been two years since I started going to the gym and this post is just a summary of my experience. Before going to the gym. Iv...

Tuesday, 12 January 2016

Interfacing LCD with microcontrollers

Following my previous post of an 8051 based Chess clock that uses a 16x2 LCD display to show the players time, this post is to help you get acquainted with the "language" of an LCD display so that you can "tell" it what to display.

Why learn the "language" of the LCD?

Well,we could just get away with using the libraries available to help with our projects but it doesn't help us in any way.
If the library is lost,we are too.
So,its always better to understand how the component works, which frees us from being a slave to the library.


Let's take the simplest of the LCD displays,a 16x2 display.
It consists of 16 columns and 2 rows.
There are 16 pins available helping us to "talk" with the display.
 
                                         

1. GND: The ground pin.
               This is the negative terminal of the power source.
               Connect it to the negative terminal with a wire and you are done.

2. VCC: The power pin.
              It is the positive terminal of the power source.
               Connect it to the positive terminal with a wire and you are done.

3. VEE: The contrast pin.
              Connect the wiper/middle pin of an 10Kohm potentiometer to this terminal and the others to               the VCC and GND respectively.
              Varying the pot helps you adjust the contrast of the display.

4. RS: Register select.
           The LCD has two registers inside of it.One is for holding the "data" and the other is for                        holding the "command".
           RS=0 =>Command Mode.
           RS=1 =>Data Mode.

  How does the RS help?

You notice the pins DB0 - DB7 on the display?Well,these are the "Databits" of the LCD.You place whatever value you want to on these 8 bits and if RS=0,this value is taken as Command towards the display and if RS=1,it is taken as Data and the value is displayed on the LCD.

5.R/W: Read or Write
             R/W=0 =>Write mode.
             R/W=1 =>Read mode.
             Based on your need,you can either read or write to the LCD.
             I generally only write to the LCD and save a pin of the microcontroller by directly connecting              this pin to the GND.

6.EN: Enable
          This pin "tells" the LCD that it has been configured to your needs and it is time to display it on           the LCD.
          The LCD enable is activated by a HIGH -> LOW signal on this pin.

7.DB0 - DB7: Databits 0 to 7.
                       These are the pins used to place the 8 bit ASCII data or command on the LCD.

8. LED+ and LED-: Backlight connections
                                 Connect LED+ to VCC and LED- to GND to get the backlight of the LCD to get                                  glowing.
                                 You could leave them unconnected if you don't want the backlight and it will                                        help you save power.

The command table for the LCD is:


Command                                 Function
0F           LCD ON, Cursor ON, Cursor blinking ON
01           Clear screen
02           Return home
04           Decrement cursor
06           Increment cursor
0E           Display ON ,Cursor blinking OFF
80           Force cursor to the beginning of  1st line
C0           Force cursor to the beginning of 2nd line
38           Use 2 lines and 5×7 matrix
83           Cursor line 1 position 3
3C           Activate second line
08           Display OFF, Cursor OFF
C1           Jump to second line, position1
OC          Display ON, Cursor OFF
C1           Jump to second line, position1

C2           Jump to second line, position2

The LCD can be interfaced in 8 bit or 4 bit mode.

In 8 bit mode,you need to connect all the DB pins to the microcontroller.

In 4 bit mode,only the high bit DB4 to DB7 are connected with the microcontroller.
This helps us save 4 pins.

I'll help you with the 8 bit mode in this post.
I'm using the 8051 to help me with the project but you could use whatever microcontroller you are comfortable with.

//write the LCD command table and display code for "the curious engineer"

#include<reg52.h>

int j=0;

sbit rs=P2^6; //rs=0=>command mode
                     //rs=1=>data mode

sbit en=P2^7; //high to low transition enables lcd module

void initialize_lcd(void);
void write(int,char);//based on value of int,it is either command or data that is written
void delay(void);

void main()
{
//set P0 as Databits output
P0=0X00;

while(1)
{
        //initializing lcd module
initialize_lcd();
delay();

//display data
//set cursor to line 1
write(0,0X80);

//display The Curious
write(1,'T');
write(1,'h');
write(1,'e');
write(1,' ');
write(1,'C');
write(1,'u');
write(1,'r');
write(1,'i');
write(1,'o');
write(1,'u');
write(1,'s');

//set cursor to line 2
write(0,0XC0);

//display Engineer
write(1,'E');
write(1,'n');
write(1,'g');
write(1,'i');
write(1,'n');
write(1,'e');
write(1,'e');
write(1,'r');
}
}

void initialize_lcd(void)
{
//initialize lcd module

write(0,0X38);
write(0,0X0C);
write(0,0X01);
write(0,0x06);
write(0,0X3C);
write(0,0X80);

}

void write(int a,char b)
{
P0=b;       //8 bit ASCII value
rs=a;        //a==0 =>command mode
               //a==1 =>data mode

       //high to low transition for enable pin
en=1;
for(i=0;i<=300;i++);
en=0;
for(i=0;i<=300;i++);
//some delay
delay();
}

void delay(void)
{
for(j=0;j<=3000;j++);
}

The technique used to display the letters in not a very effective technique.The better way to perform it is by the use of pointer that points to the start of the string.
This can be used if the string to be displayed is fixed.So the string can be stored in a character array and used as and when required.
In cases when the string to be displayed is not static,such as readings from a sensor,the above technique provides us with the flexibility of displaying data anywhere on the display.

No comments:

Post a Comment