Interfacing LCDs to TM4C Tiva C LaunchPad

Share

Two example source codes are presented in this lesson. The first program shows how to interface the LCD with 8-bits data line and the second example show how to interface the LCD with only 4-bits data line. There are  also some function for processing characters into strings and performing other auxiliary tasks. Please leave a comment if you have any questions .

Program 1 : LCD 8-bits 


#include "TM4C123.h" // Device header
void delay_milli(int n);
void delay_micro(int n);
void LCD_init(void);
void LCD_Cmd(unsigned char command);
void LCD_Data(unsigned char data);
int main(){
LCD_init();
// while(1)
// {
LCD_Cmd(0x01);
LCD_Cmd(0x80);
delay_milli(500);
LCD_Data('C');
delay_milli(1);
LCD_Data('O');
delay_milli(1);
LCD_Data('R');
delay_milli(1);
LCD_Data('T');
delay_milli(1);
LCD_Data('E');
delay_milli(1);
LCD_Data('X');
delay_milli(1);
LCD_Data('-');
delay_milli(1);
LCD_Data('M');
delay_milli(1);
LCD_Data('.');
delay_milli(1);
LCD_Data('C');
delay_milli(1);
LCD_Data('O');
delay_milli(1);
LCD_Data('M');
delay_milli(500);
//}
}
void LCD_init(void){
SYSCTL->RCGCGPIO |= 0x01; //PORTA clock
SYSCTL->RCGCGPIO |= 0x02;
GPIOA->DIR |=0xE0; //PORTA controls RS,E and R/W
GPIOA->DEN |=0xE0;
GPIOB->DIR |=0xFF; //PORTB D0-D7
GPIOB->DEN |=0xFF; //PORTB D0-D7
LCD_Cmd(0x38); //8-bits,2 display lines, 5x7 font
LCD_Cmd(0x06); //increments automatically
LCD_Cmd(0x0F); //Turn on display
LCD_Cmd(0x01); //clear display

}

void LCD_Cmd(unsigned char command)
{
GPIOA->DATA = 0x00; //RS =0, E=0, RW=0
GPIOB->DATA =command;
GPIOA->DATA =0x80; //E=1 to secure command
delay_micro(0);
GPIOA ->DATA =0x00;
if(command <4) delay_milli(2); else delay_micro(37); } void LCD_Data(unsigned char data) { GPIOA ->DATA =0x20; //RS=1, E=0,RW=0
GPIOB->DATA =data;
GPIOA->DATA |= 0x80;
GPIOA->DATA =0x00;
delay_micro(0);

}
void delay_milli(int n){
int i,j;
for(i=0;i<n;i++)
for(j=0;j<3180;j++)
{}
}

void delay_micro(int n){
int i,j;
for(i=0;i<n;i++)
for(j=0;j<3;j++)
{}
}

Program 2 : LCD 4-bits 

#include "TM4C123.h" // Device header

#define LCD_CTRLP GPIOA
#define LCD_DATAP GPIOB
#define RS 0x20 /* PORTA BIT5 mask */
#define RW 0x40 /* PORTA BIT6 mask */
#define EN 0x80 /* PORTA BIT7 mask */
#define HIGH 1
#define LOW 0

void delay_milli(int n);
void delay_micro(int n);
void LCD_init(void);
void LCD4bit_Cmd(unsigned char command);
void LCD4bit_Data(unsigned char data);

int main()
{
 LCD_init();
 LCD4bit_Cmd(0x01);
 LCD4bit_Cmd(0x80);
 delay_milli(500);

 LCD4bit_Data('a');
 delay_micro(20);
// LCD4bit_Data('E');
 delay_milli(500);
// 

}

void LCD_init(void)
{
 SYSCTL->RCGCGPIO |=0x01;
 SYSCTL->RCGCGPIO |=0x02;
 LCD_DATAP ->DIR |=0xF0;
 LCD_DATAP ->DEN |=0xF0;
 LCD_CTRLP ->DIR |=0xF0;
 LCD_CTRLP ->DEN |=0xF0;

 LCD4bit_Cmd(0x20);
 LCD4bit_Cmd(0x28);
 LCD4bit_Cmd(0x06);
 LCD4bit_Cmd(0x01);
 LCD4bit_Cmd(0x0F);

 //..
 //...
}
void LCD4bit_Data(unsigned char data)
{
 unsigned char temp;
 LCD_CTRLP->DATA = RS;
 temp = data;
 temp = temp>>4;
 LCD_DATAP->DATA = temp;
 LCD_CTRLP->DATA = RS |EN;
 delay_micro(10);
 LCD_CTRLP->DATA =RS;
 delay_micro(37);

 temp =data;
 //LCD_CTRLP->DATA = RS;
 LCD_DATAP->DATA = temp;
 LCD_DATAP->DATA = RS |EN;
 delay_micro(10);
 LCD_CTRLP->DATA =0x00;
 delay_micro(37);

}

void LCD4bit_Cmd(unsigned char command)
{
 unsigned char temp;
 LCD_CTRLP->DATA = 0x00;
 temp = command;
 temp = temp>>4;
 LCD_DATAP->DATA =temp;
 LCD_CTRLP->DATA = EN;
 delay_micro(0);
 LCD_CTRLP->DATA = 0x00;
 if(command < 4) delay_milli(2); else delay_micro(37); temp = command; LCD_DATAP->DATA =temp;
 LCD_CTRLP->DATA = EN;
 delay_micro(0);
 LCD_CTRLP->DATA =0x00;

}

void delay_milli(int n)
{
 int i,j;
 for(i=0;i<n;i++)
 for(j=0;j<3180;j++)
 {}
}

void delay_micro(int n)
{
 int i,j;
 for(i=0;i<n;i++)
 for(j=0;j<3;j++)
 {}

}

Please leave your comments at the bottom of this page. Click here

London, U.K
© 2019 BOHOBIOM LTD