Interfacing LCD with ARM
16x2 LCD is widely used in various embedded applications. The reason for this is its easy operation and it can display a large number of characters and symbols. In this tutorial we’ll interface LCD with ARM7 based microcontroller LPC2138.
LCD can be interfaced in 8-bit as well as 4-bit mode (mode signifies Data bits sent at a time). In the 8-bit mode we require 8-pins of the microcontroller whereas in 4-bit mode the data is sent ‘nibble’ by ‘nibble’ (4 data lines used).
16x2 alphanumeric LCD can display 16 characters in each of its row, and it has two rows. To interface an LCD we need to know about its internal registers and pin functions.
An LCD has two important registers namely command register and Data register. Both are 8-bit registers. The command register is written with various commands by the user. These commands are responsible for the functioning of LCD.
The data register is loaded with the ASCII value of the Data (Character/Number, etc.) to be displayed.
Both the registers are accessed through 8-bit Data Line (D0-D7). There is one selection line provided, namely R/S Pin, in LCD Module which selects between these two registers.
R/S=0-> Command Register
R/S=1-> Data Register
Apart from R/S, there are other two Pins, viz. R/W and EN.
EN pin is to enable (1) or to disable (0) the LCD.
R/W is used to select the operation ‘Read from LCD’ or ‘Write to LCD’. Logic 0 at R/W stands for Write operation and 1 for Read operation.
Apart from these pins, the last two Pins (15 & 16) on the LCD are for Backlight. And pins 1,2,3 are for GND, +VCC and +VEE(contrast adjustment) respectively.
There are various commands that need to be send to LCD so as to Initialize it, to make it work accordingly. These command are listed below.
Apart from these there are two more important commands.
(0x8m)à value of m (in hex) determines the position of character to be displayed in first line.
For e.g. (0x80)-> place display pointer at 1st line 1st position
(0x8a)-> place display pointer at 1st line 10th position
Similarly for displaying at various positions in Second line of LCD, we use the commandà (0xcm), again m (in hex) determines the position of character to be displayed in second line.
In this tutorial of ARM, the Data lines of the LCD are connected at Port 0 (P0.0 to P0.7) and Control lines (r/s, r/w, en) at P0.8 to P0.10 of the LPC2138.
An important concept behind getting a logic state on a particular pin, without altering logic state of other pins, is ANDing and ORing. Here in LCD interfacing it is of much use, so let us get familiar with it.
Let us say we want ‘1’ at pin P0.4. This has a simple syntax,
But the problem with this is, it alters the logic state of other pins. To overcome this problem we introduce OR condition, as below;
IOPIN0 = IOPIN0 + (1<<4)
Similarly, for a low logic state we use AND-operation,
e.g. to make P0.6 = low, we write,
IOPIN0& = ~ (1<<6)
Which means, IOPIN0 = IOPIN0 x (0<<6)
This makes P0.6=0, leaving states of other pins unchanged.
Now let’s come back to 16x2 LCD interfacing which has got three control lines (viz. r/s, r/w, en ) that need to be controlled individually.
However in this code we’ll use IOSET and IOCLR for the sake of simplicity.
Explanation of the code:
The functions cmd() , data() and str() are purposefully for sending command, data and string to LCD.
To latch a command to LCD, rs=0(command register); r/w=0; en=1
Similarly to latch data, rs=1(data register); r/w=0; en=1;
To display string, each of its character is extracted through str() function and is latched one byte at a time (at fast rate, so it appears to be continuous).