HD44780 based LCD modules are very popular among hobbyists because they are cheap and they can display characters. Besides they are very easy to interface with microcontrollers. In this lab, you will learn how to interface an HD44780 based character LCD to a TI Tiva LaunchPad. The interface requires 7 I/O lines of the TM4C microcontroller: 4 data lines and 3 control lines. You also will learn how to use ezTiva LIB in order to display information on the LCD module.
Required Components List
Character LCD Module
× 1
10 KΩ Potentiometer
× 1
Breadboard
× 1
Jumper Wires Ribbon Cable
× 1
Breadboard Power Supply Module
× 1
Power Adapter 9V/2A
× 1
Circuit / Schematic Diagram
In this lab, a 16x2 character LCD module will be used and connected to the Tiva LaunchPad, as shown in the following diagram. The LCD module will be configured in 4-bit interfacing mode, so the data pin D[0]~D[3] on the LCD module will not be used. All the pins used to connect to the LCD module must be configured with the GPIO output direction.
Create a new folder under the EE3450 folder and name it Lab04_CharLCD.
Launch the Keil μVisio and create a new project. Save the project to the project folder you just created in the previous step and set the project name to Lab04_CharLCD.)
Add the Common and ezTivaLIB folders to the Options for Target ➤ C/C++ ➤ include paths setting.
Add ezTiva LIB (ez123GLIB.lib or ez1294LIB.lib) into your project, increase the stack and heap size under the "startup_TM4cXXX.s (Startup)" setting. (Lesson 08: Add ezTiva Library into Your Project)
Configurations
Write down the following configuration information into your lab report.
Next, we need to configure all the GPIO ports and pins that are used in the design.
According to the pin connections, complete the following GPIO configurations for each port. Fills the pin field by the value below:
0: Clean the bit
1: Set the bit
x: Do not change the bit
d: Do not care
For both TM4C123GXL and TM4C1294XL LaunchPads, the Port C [3:0] are used for JTAG/SWD. Therefore, when you configure Port C, you have to use bitwise operators to make sure your new configuration settings do not affect the JTAG/SWD function (PC3 ~ PC0).
Most of GPIO pins are configured as GPIOs and tri-stated by default (GPIOPCTL = 0, CPIOAFSEL = 0, GPIODIR = 0, GPIOPUR = 0, GPIOPDR = 0, GPIOODR = 0)
Enable Clock to the GPIO Modules (RCGCGPIO register) TM4C123G: SYSCTL->RCGCGPIO |= (_PORTs);|= binary = hex
Unlock Port TM4C123G: PD7 and PF0 are locked after reset. TM4C1294: PD7 and PE7 are locked after reset If those pins are used in the design, they must be unlocked first. To unlock the port, 0x4C4F434B must be written into the GPIOLOCK register and uncommit it by setting the GPIOCR register.
8
4
2
1
8
4
2
1
7
6
5
4
3
2
1
0
bit
Port
Pin 7
Pin 6
Pin 5
Pin 4
-
Pin 3
Pin 2
Pin 1
Pin 0
pin
Value in Hex
Register
Value to Register
-
-
=
➤
GPIO->LOCK
=
0x4C4F434B
-
-
=
➤
GPIO->CR
-
-
=
➤
GPIO->LOCK
=
0x4C4F434B
-
-
=
➤
GPIO->CR
Convert above configuration into registers
GPIO Analog Mode Select If any pin is used as an Analog signal (check Signal Type field on table 1), the appropriate bit in AMSEL must be set.
0: Digital signal
1: Analog signal
8
4
2
1
8
4
2
1
7
6
5
4
3
2
1
0
bit
Port
Pin 7
Pin 6
Pin 5
Pin 4
-
Pin 3
Pin 2
Pin 1
Pin 0
pin
Value in Hex
Register
Value to Register
-
-
=
➤
GPIO->AMSEL
-
-
=
➤
GPIO->AMSEL
-
-
=
➤
GPIO->AMSEL
-
-
=
➤
GPIO->AMSEL
-
-
=
➤
GPIO->AMSEL
-
-
=
➤
GPIO->AMSEL
-
-
=
➤
GPIO->AMSEL
GPIO Port Control (PCTL) The PCTL register is used to select the specific peripheral signal for each GPIO pin when using the alternate function mode.
0: GPIO
1~0xF: Check the GPIO Pins and Alternate Function table
GPIO Alternate Function Select (AFSEL) Setting a bit in the AFSEL register configures the corresponding GPIO pin to be controlled by PCTL peripheral function.
0: General I/O
1: Pin connected to the digital function defined in the PCTL register
8
4
2
1
8
4
2
1
7
6
5
4
3
2
1
0
bit
Port
Pin 7
Pin 6
Pin 5
Pin 4
-
Pin 3
Pin 2
Pin 1
Pin 0
pin
Value in Hex
Register
Value to Register
-
-
=
➤
GPIO->AFSEL
-
-
=
➤
GPIO->AFSEL
-
-
=
➤
GPIO->AFSEL
-
-
=
➤
GPIO->AFSEL
-
-
=
➤
GPIO->AFSEL
-
-
=
➤
GPIO->AFSEL
-
-
=
➤
GPIO->AFSEL
GPIO Pin Direction (DIR) Set pin direction
0: Input pin
1: Output pin
8
4
2
1
8
4
2
1
7
6
5
4
3
2
1
0
bit
Port
Pin 7
Pin 6
Pin 5
Pin 4
-
Pin 3
Pin 2
Pin 1
Pin 0
pin
Value in Hex
Register
Value to Register
-
-
=
➤
GPIO->DIR
-
-
=
➤
GPIO->DIR
-
-
=
➤
GPIO->DIR
-
-
=
➤
GPIO->DIR
-
-
=
➤
GPIO->DIR
-
-
=
➤
GPIO->DIR
-
-
=
➤
GPIO->DIR
Internal Pull-Up Resistor (PUR), Pull-Down Resistor (PDR), and Open-Drain (ODR) PUR: The pull-up control register PDR: The pull-down control register ODR: The open-drain control register
0: Disable
1: Enable
8
4
2
1
8
4
2
1
7
6
5
4
3
2
1
0
bit
Port
Pin 7
Pin 6
Pin 5
Pin 4
-
Pin 3
Pin 2
Pin 1
Pin 0
pin
Value in Hex
Register
Value to Register
-
-
=
➤
GPIO->
-
-
=
➤
GPIO->
-
-
=
➤
GPIO->
-
-
=
➤
GPIO->
-
-
=
➤
GPIO->
-
-
=
➤
GPIO->
-
-
=
➤
GPIO->
GPIO Digital Enable Enables all the pins that are used in the design, including GPIO pins and alternate function pins.
0: Pin undriven
1: Enable pin
8
4
2
1
8
4
2
1
7
6
5
4
3
2
1
0
bit
Port
Pin 7
Pin 6
Pin 5
Pin 4
-
Pin 3
Pin 2
Pin 1
Pin 0
pin
Value in Hex
Register
Value to Register
-
-
=
➤
GPIO->DEN
-
-
=
➤
GPIO->DEN
-
-
=
➤
GPIO->DEN
-
-
=
➤
GPIO->DEN
-
-
=
➤
GPIO->DEN
-
-
=
➤
GPIO->DEN
-
-
=
➤
GPIO->DEN
Example Source Code
Copy the following example code to your main.c file.
Note: The example code is not yet complete, you must implement all functions following the lab instructions.
The following code does not complete, you need to finish the GPIO configurations.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include "TM4C123GH6PM.h"
#include "MyDefines.h"
#include "ez123G.h"
void Setup_GPIO(void);
int main(void)
{
PEZOBJ_LCD lcd; // Declare an object for LCD module
char str[100];
uint16_t i = 0;
Setup_GPIO(); // GPIO Configuration
lcd = ezLCD_Create(); // Creating a new Hardware LCD Object
ezLCD_Connect_DataPort(lcd, _PORTD, ON_PIN_3_0);// Connecting DATA port on PortD[3:0]
ezLCD_Connect_ENPin(lcd, _PORTE, _PIN1); // Connecting EN pin on PortE[1]
ezLCD_Connect_RSPin(lcd, _PORTE, _PIN2); // Connecting RS pin on PortE[2]
ezLCD_Connect_RWPin(lcd, _PORTE, _PIN3); // Connecting RW pin on PortE[3]
ezLCD_Start(lcd); // Starting lcd object
ezLCD_LoadVerticalBargraphFonts(lcd); // Load vertical bargraph fonts
ezLCD_ClearDisplay(lcd); // Clear LCD display
ezLCD_PrintString(lcd, "HELLO"); // Display string on LCD screen
ezLCD_Position(lcd, 1,0); // Set display position at [1,0] (raw 1. col 0)
ezLCD_PrintString(lcd, "TI Tiva 123G"); // Display string on LCD screen
while (1){
sprintf(str,"i = %d ", i);
ezLCD_Position(lcd, 0, 6);
ezLCD_PrintString(lcd, str);
ezLCD_DrawVerticalBG(lcd, 1, 15, 2, i%16);
i++;
timer_waitMillis(100); // Hardware timer delay for 100 ms
}
}
void Setup_GPIO()
{
// GPIO Initialization and Configuration
// 1. Enable Clock to the GPIO Modules (SYSCTL->RCGCGPIO |= (_PORTs);)
SYSCTL->RCGCGPIO |= (__);
// allow time for clock to stabilize (SYSCTL->PRGPIO & (_PORTs))
while ((SYSCTL->PRGPIO & (__)) != (__)) {};
// 2. Unlock GPIO only PD7, PF0 on TM4C123G; PD7 on TM4C1294 (GPIOx->LOCK = 0x4C4F434B; and GPIOx->CR = _PINs;)
// 3. Set Analog Mode Select bits for each Port (GPIOx->AMSEL = _PINs; 0=digital, 1=analog)
// 4. Set Port Control Register for each Port (GPIOx->PCTL = PMCn << _PTCL_PINn, check the PCTL table)
// 5. Set Alternate Function Select bits for each Port (GPIOx->AFSEL = _PINs; 0=regular I/O, 1=PCTL peripheral)
// 6. Set Output pins for each Port (Direction of the Pins: GPIOx->DIR = _PINs; 0=input, 1=output)
// 7. Set PUR bits (internal Pull-Up Resistor), PDR (Pull-Down Resistor), ODR (Open Drain) for each Port (0: disable, 1=enable)
// 8. Set Digital ENable register on all port.pins (GPIOx->DEN = _PINs; 0=disable, 1=enable)
}