You are on page 1of 9

Interfacing a 16x2 LCD with TIVA TM4C123

Interfacing a 16x2 LCD with TIVA TM4C123


Overview
In this document we are going to interface a 16x2 LCD with Texas Instruments TIVA TM4C123 Launchpad
Kit and display the characters send from MCU to the LCD. The IDE we are going to use is Code Composer
Studio from TI. The program for this experiment is written with TivaWare API’s, which makes easy to
program the Tiva C series devices. This manual is best suitable for programmers who are aware of
TivaWare libraries and Code Composer Studio.

Tools Used
The tools we are going to use in this experiment are listed as below

1.Tiva C Launchpad

http://www.ti.com/tool/ek-tm4c123gxl

2. A 16x2 LCD

http://www.engineersgarage.com/electronic-components/16x2-lcd-module-datasheet

3. A 10k POT

4. 220Ohm resistor

Requirements
A user needs to have full access to the all required documents related to the tools used and other
software packages needed to support the programming part.

You can download the related documents from the following links

1. User guide for Tiva Launchpad


2. http://www.ti.com/lit/ug/spmu296/spmu296.pdf

3. TM4C123GH6PM Datasheet

http://www.ti.com/lit/ds/spms376e/spms376e.pdf

4. TI_TivaWare
http://www.ti.com/tool/sw-tm4c#Technical Documents

1|Page
Interfacing a 16x2 LCD with TIVA TM4C123

16X2 Character LCD working, Pinout and Description


LCD stands for liquid crystal display. They come in many sizes 8x1 , 8x2 , 10x2 , 16x1 , 16x2 , 16x4 , 20x2 ,
20x4 ,24x2 , 30x2 , 32x2 , 40x2 etc . Many multinational companies like Philips Hitachi Panasonic make
their own special kind of lcd's to be used in their products. All the lcd's performs the same functions
(display characters numbers special characters ASCII characters etc).Their programming is also same and
they all have same 14 pins (0-13) or 16 pins (0 to 15). The Lcd we are going to use is 16x2 having 16 pins.
All lcd’s have following pins in common.

 Eight(8) Data pins


 VCC (Apply 5v here)
 GND (Ground this pin)
 RS (Register select)
 RW (read - write)
 EN (Enable)
 V0 (Set Lcd contrast)

The two extra pins in our Lcd are backlight+ and backlight- to control backlight of lcd.

8-Data pins carries 8-bit data or command from an external unit such as microcontroller.

V0(Set Lcd contrast)


Set lcd contrast here. Best way is to use variable resistor such as potentiometer. Output of the
potentiometer is connected to this pin. Rotate the potentiometer knob forward and backward to adjust
the lcd contrast.

RS(Register select)

There are two registers in every lcd


1 Command Register
2 Data Register
Command Register
When we send commands to lcd these commands go to Command register and are processed their.
Commands with their full description are given in the picture below.
When RS=0 Command Register is Selected.
Data Register
When we send Data to lcd it goes to data register and is processed.
When RS=1 Data Register is selected.
RW (Read - Write) When RW=1, We want to read data from lcd.

When RW=0 We want to write to lcd.

2|Page
Interfacing a 16x2 LCD with TIVA TM4C123

EN (Enable signal)
When you select the register(Command and Data) and set RW(read - write) now it’s time to execute the
instruction. By instruction i mean the 8-bit data or 8-bit command present on Data lines of lcd.
This requires an extra voltage push to execute the instruction and EN (enable) signal is used for this
purpose. Usually we make it en=0 and when we want to execute the instruction we make it high en=1
for some milli seconds. After this we again make it ground en=0. Data which we send to our lcd can be
any alphabet (small or big) , digit or ASCII character.

NOTE: we can not send an integer, float, long, double type data to lcd because lcd is designed to display
a character only. The 8 data pins on lcd carries only ASCII 8-bit code of the character to lcd. However we
can convert our data in character type array and send one by one our data to lcd. Data can be sent
using lcd in 8-bit 0r 4-bit mode. If 4-bit mode is used, two nibbles of data (First high four bits and then
low four bits) are sent to complete a full eight-bit transfer. 8-bit mode is best used when speed is
required in an application and at least ten I/O pins are available. 4-bit mode requires a minimum of
seven bits. In 4-bit mode, only the top 4 data pins (4-7) are used.

3|Page
Interfacing a 16x2 LCD with TIVA TM4C123

Standard Lcd Commands with their functions are described with their
functionality below.

Command 0x30 means we are setting 8-bit mode lcd having 1 line and we are initializing it to be
5x7 character display. Now this 5x7 is something which everyone should know what it stands
for. Usually the characters are displayed on lcd in 5x8 matrices form. Where 5 is total number
of coulombs and 8 is number of rows.Thus the above 0x30 command initializes the lcd to
display character in 5 coulombs and 7 rows the last row we usually leave for our cursor to move or blink
etc.

4|Page
Interfacing a 16x2 LCD with TIVA TM4C123

The Character is displayed on lcd screen in 5x8 or 5x7 matrix. Where 5 represents number of
coulombs and 7 represent number of rows. Maximum size of the matrix is 5x8. You cannot display
character greater then 5x8 dimension matrix. To display character greater than this dimension you have
to switch to graphical lcd’s.

 The command 0x38 means we are setting 8-bit mode lcd having two lines and character shape
between 5x7 matrix.
 The command 0x20 means we are setting 4-bit mode lcd having 1 line and character shape
between 5x7 matrix.
 The command 0x28 means we are setting 4-bit mode lcd having 2 lines and character shape
between 5x7 matrix.
 The command 0x06 is entry mode it tells the lcd that we are going to use you.
 The command 0x08 displays cursor off and display off but without clearing DDRAM contents
 The command 0x0E displays cursor on and display on.
 The command 0x0c display on cursor off(displays cursor off but the text will appear on lcd).
 The command 0x0F display on cursor blink(text will appear on screen and cursor will blink).
 The command 0x18 shift entire display left (shift whole off the text on the particular line to its
left).
 The command 0x1C shift entire display right (shift whole off the text on the particular line to its
right).
 The command 0x10 Moves cursor one step left or move cursor on step ahead to left when ever
new character is displayed on the screen.
 The command 0x14 Moves cursor one step right or move cursor on step ahead to right when
ever new character is displayed on the screen.
 The command 0x01 clear all the contents of the DDRAM and also clear the lcd removes all the
text from the screen.
 The command 0x80 initialize the cursor to the first position means first line first matrix(start
point) now if we add 1 in 0x80+1=0x81 the cursor moves to second matrix.

16x1 lcd displays 16 characters only. The first will appear on 0x80 second 0x81 third 0x82 and so on until
last, the 16 once on address 0x8F.

5|Page
Interfacing a 16x2 LCD with TIVA TM4C123

NOTE: You can send commands in hexadecimal or decimal form which one do you like the result is same
because the microcontroller translates the command in 8-bit binary value and send it to the lcd. Before
you send commands and data to your lcd. Lcd must first be initialized. This initialization is very
important for lcd, If you don't initialize it properly you will see nothing on your lcd.

For 8-bit mode, this is done as follows:

1. Wait more than 15 msecs after power is applied.

2. Write command 0x30 to LCD and wait 5 milli seconds for the instruction to complete.

3. Write command 0x30 to LCD and wait 160 micro seconds for instruction to complete.

4. Write command 0x30 AGAIN to LCD and wait 160 micro seconds or Poll the Busy Flag.

In 4-bit mode the high nibble is sent first before the low nibble and the EN pin is toggled each

time four bits is sent to the LCD. To initialize in 4-bit mode:

1. Wait more than 15 msecs after power is applied.

2. Write command 0x03 to LCD and wait 5 msecs for the instruction to complete.

3. Write command 0x03 to LCD and wait 160 usecs for instruction to complete.

4. Write command 0x03 AGAIN to LCD and wait 160 usecs (or poll the Busy Flag).

Write 0x02 to the LCD to Enable 4-Bit Mode

6|Page
Interfacing a 16x2 LCD with TIVA TM4C123

Connection between TIVA and LCD


When using 4 bit mode, the above connections require 6 pins of your MCU.

The LCD display will also require additional pins to be connected to Vcc or GND, including:
 Power and ground for the LCD.
 Power and ground for the LCD backlight (if applicable)
 Ground connection of the R/W pin (to enable writing to the LCD).
 A potentiometer between Vcc and GND to pin 5 for adjusting display contrast.
Here is the pinout used in my version of the LCD library for the TivaC Launchpad.

LCD pin TivaC


-------------------
DB4 PD0
DB5 PD1
DB6 PD2
DB7 PD3
RS PE5
EN PE4

The Code

To upload the code on Tiva launchpad we need to create a new project and then select the
target as Tiva TM4C123GH6PM.Copy the following code in main.c file of the project and the
build and debug the code.

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"

#define PIN_RS GPIO_PIN_5 // PE.5


#define PIN_EN GPIO_PIN_4 // PE.4
#define PIN_D7 GPIO_PIN_3 // PD.3
#define PIN_D6 GPIO_PIN_2 // PD.2
#define PIN_D5 GPIO_PIN_1 // PD.1
#define PIN_D4 GPIO_PIN_0 // PD.0
#define PDMask (GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3)
#define PEMask (GPIO_PIN_4|GPIO_PIN_5)
#define FALSE 0
#define TRUE 1
#define HIGH 0xFF
#define LOW 0x00

7|Page
Interfacing a 16x2 LCD with TIVA TM4C123

//initializing the gpio pins for lcd


void GPIO_init(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, PDMask);
GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, PEMask);
}
//Enable pin sequence initilization
void Push(void)
{
GPIOPinWrite(GPIO_PORTE_BASE,PIN_EN, LOW);
SysCtlDelay(200000);
GPIOPinWrite(GPIO_PORTE_BASE,PIN_EN, HIGH);
SysCtlDelay(200000);
GPIOPinWrite(GPIO_PORTE_BASE,PIN_EN, LOW);
SysCtlDelay(200000);
}
void Send_data(char value, int status)

{
GPIOPinWrite(GPIO_PORTD_BASE,PDMask,~(PDMask));//clearing data bits to 0
GPIOPinWrite(GPIO_PORTE_BASE,PEMask,~(PEMask));//clearing the bits EN and RS
//to 0

GPIOPinWrite(GPIO_PORTD_BASE,PDMask,(value&0xF0)>>4);
if(status==TRUE)
{
GPIOPinWrite(GPIO_PORTE_BASE,PIN_RS,HIGH);

}
else
{
GPIOPinWrite(GPIO_PORTE_BASE,PIN_RS,LOW);
}
Push();
GPIOPinWrite(GPIO_PORTD_BASE,PDMask,~(PDMask));//clearing the data bits to 0
GPIOPinWrite(GPIO_PORTE_BASE,PEMask,~(PEMask));

GPIOPinWrite(GPIO_PORTD_BASE,PDMask,value&0x0F);
if(status==TRUE)
{
GPIOPinWrite(GPIO_PORTE_BASE,PIN_RS,HIGH);

}
else
{
GPIOPinWrite(GPIO_PORTE_BASE,PIN_RS,LOW);
}
Push();

8|Page
Interfacing a 16x2 LCD with TIVA TM4C123

void LCD_init(void)
{
GPIOPinWrite(GPIO_PORTD_BASE,PDMask,~(PDMask));//LCD Initialization Steps
GPIOPinWrite(GPIO_PORTE_BASE,PEMask,~(PEMask));
SysCtlDelay(200000);
//4 bit mode

GPIOPinWrite(GPIO_PORTD_BASE,PDMask,0x02);//set the lcd in 4 bit mode


Push();
Send_data(0x28,FALSE);
Send_data(0x0E,FALSE);
Send_data(0x06,FALSE);

}
void LCD_clear()//clear Lcd statements
{
Send_data(0x01,FALSE);
Send_data(0x02,FALSE);
}
void LCD_print(char *data)//to print a character on lcd
{
while((*data!=0)&&(data!=0))
{
Send_data(*data,TRUE);//send data

data++;
}
}
int main(void)
{
GPIO_init();
LCD_init();
LCD_clear();

while(1)
{
LCD_clear();
SysCtlDelay(2000);
LCD_print("Have a nice Day");//
//send_data('T',TRUE);
}

After successfully implementing the above procedure, the lcd will display the characters mentioned in
the above C code.

9|Page

You might also like