Friday 21 February 2014

8051 LCD INTERFACE

LEDs (lights), interfaced with 8051 microcontroller, can be used to display binary numbers or on/off states while Seven Segment Displays (SSD) can display digits but that's not enough. What if you wanted to display a proper message consisting of numbers, letters, characters, symbols e.t.c.? That's where LCD Screens jump in. LCDs make it convenient to display anything to the user. It may sound tricky to interface an LCD with a microcontroller but trust me or not, it's simpler than it looks and this short tutorial will make sure that you get the hold of it.


LCD

Stands for Liquid Crystal Display and is definitely an output device. It comes in many popular shapes and sizes but here is the one that you will most probably face at the beginner's level.

COMMON LCD
COMMON LCD
Let's focus on this one. It is basically called 16x2 LCD because it has sixteen columns and two rows thus it can display thirty-two (32) characters at a time (sixteen characters in each row). There are sixteen hardware pins as shown.

LCD HARDWARE PINS
LCD HARDWARE PINS

Number
Pin
Function



1
GND
Ground
2
Vcc
Power Source (+5V)
3
Vee
Contrast Control
4
RS
Register Select
0=Command Register
1=Data Register
5
R/W
Read/Write
0=Write
1=Read
6
E
Enable
7-14
D0 – D7
8-bit data pins
15
BL+
Backlight Control
16
BL-
Ground


Don't worry, I am going to explain the confusing terms. Our LCD comes with two important registers that we can manipulate.

  1. Control Register
    Used for instructing LCD on what to do next. It's like talking to your LCD using this register.
  1. Data Register
    Used for displaying data on LCD. 

You have to use Control Register for sending commands to LCD and when it's ready, only then you can use Data Register to display your data on the LCD. In other words, first you have to talk to your LCD (by sending commands) about what you want it to do and then send all the useful data for display.


PINS

The RS (Register Select) control pin is used to select either command or data register as described in the pin configuration table. Once selected, all data sent on the 8-bit data lines will be latched to that register.

The R/W (Read/Write) control pin is used to determine the flow of data. You have to select

  • Write Mode when you're sending something to the LCD (data or command)
  • Read Mode when you're reading from the LCD

The E (Enable) control pin acts as a guard to allow exchange of data. This pin is very important in this whole process and must be handled precisely. The following animation explains its function

SENDING DATA TO LCD
It must be used in the following manner

  • HIGH to LOW transition for Write Operation
  • LOW to HIGH transition for Read Operation

Don't worry about it right now as you will understand it more clearly in the program code.


COMMANDS

LCDs have some useful handful of commands that you can use for different operations. You can check a comprehensive list of commands here. I am going to concentrate only on the commands required to initialize an LCD because they are fairly sufficient to get things done and they are as follows


Instruction
Decimal
Hexa-Decimal



Enable 8-bit interface, 2 lines, 5*7 Pixels
56
0x38
Display ON, Cursor Blinking
14
0x0E
Clear Screen
1
0x01


HARDWARE CONNECTIONS

Here is my optimized version of LCD hardware Connections. It takes the minimum possible time and works most of the time.

LCD Hardware Connections
LCD Hardware Connections
However I have assumed that we are

  • Always writing to LCD (R/W = 0)
  • Constant Contrast (through 1K ohm resistor)
  • Full Brightness (no resistor in the BL connections)

The rest of the pins are connected to microcontroller. If you have problem with these assumptions, you can play around with your hardware. For instance, connect potentiometers to Vee or BL controls to control the Contrast or Brightness yourself.


LCD INTERFACE CODE

In the Download Package below, I have included the Assembly Language Code as well as the C language Code for the very same program so you can have a clear idea of how it's done. Good news is that I am going to explain the LCD program code in a stepwise fashion. Let's get started.

  • For convenience, I gave a unique name to each pin on the LCD using #define directive so I can mention them easily.
Naming each pin using #define
Naming each pin using #define


  • To differentiate between command and data as discussed above, I created two separate functions. As you can see, the only difference is that of using RS pin according to requirement.

LCD Command Function
LCD Command Function
LCD Data Function
LCD Data Function

  • The delay function is a general one and you can use it safely at this stage but remember that it's not accurate. 
Delay Function
Delay Function

  • Here is my main function

LCD Main Function
LCD Main Function


  • To send a command to LCD, use the lcdcmd function with the command as input.
  • To send data to LCD, use the lcddata function with the data as input.

The LCD has to be initialized using the appropriate commands as you can see in the main function. Once initialized, you can send data for display. It is very important to note that the LCD accepts only ASCII characters which means that every character you send for display must be in ASCII. You can check here for a complete list of ASCII characters and their corresponding values.

There are many ways in which you can send an ASCII character to LCD. For instance, you can display A in the following ways

  • x = 'A'
    lcddata ( x )
  • x = 65     //decimal value
    lcddata ( x )
  • x = 0x41 //hexadecimal value
    lcddata ( x )
  • or directly as lcddata ( 'A' ) 

where x can be of type char or int whether signed or unsigned

As discussed above, the enable pin (e) is given a high-to-low transition for data transfer. 

Here is the Proteus Simulation Diagram for LCD interface. I have used a BUS in this simulation for simplicity.

Proteus Simulation
Proteus Simulation
That's all. Feel free to share this post if you liked it and leave feedback. If I missed something important, do let me know. 



This ZIP package contains the following files
  • C Language Code
  • Assembly Language Code
  • Compiled HEX file
  • Proteus Design File
You can use all these files at your will but remember that they are for the sake of understanding the concept. You have to know what's going on and then it won't hurt if you just copy/paste but if you do it without caring about understanding, well all I can say is that it's not good for development. 

No comments:

Post a Comment