Hi,I am trying to interface a 16X2 LCD with Tiva,Here I am trying to send two strings on the Lcd,,I have used lcd in 4 bit mode.I am getting garbage values at the lcd...following is the code....
/* Defines the base address of the memories and peripherals */
#include "inc/hw_memmap.h"
/* Defines the common types and macros */
#include "inc/hw_types.h"
/* Defines and Macros for GPIO API */
#include "driverlib/gpio.h"
/* Prototypes for the system control driver */
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "driverlib/interrupt.h"
#include "utils/uartstdio.h"
void InitConsole(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
/* Make the UART pins be peripheral controlled. */
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
/* Initialize the UART for console I/O */
UARTStdioInit(0);
}
//Declarations //LCD data port to PORTD
#define LCD_DATA GPIO_PORTD_BASE
//USERDEFINE DATA TYPES
typedef unsigned char LCDubyte;
//Function Prototypes
void LCDWriteByte(LCDubyte LCDData); //Function for shifting Data
void init_LCD(void); //Function to initialise the LCD
void LCD_command(unsigned char cmd); //Function to pass command to the LCD
void LCD_data(unsigned char data); //Function to write character to the LCD
void LCD_write_string(char *str); //Function to write string to the LCD
void msdelay (unsigned int time); //Function to generate delay
//Start of Main Program
int main(void)
{
char var1[] = " Wel-Come";//Declare message to be displayed
char var2[] = "Smart Tech";
/*Set the clocking to directly run from the crystal at 16MHz*/
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |SYSCTL_XTAL_20MHZ);
/* Set the clock for the GPIO Port E,using as rs,rw.en */
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
/* Set the clock for the GPIO Port D,ad command and data pins */
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
/* Set the type of the GPIO Pin */
GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
init_LCD(); // call function to initialise of LCD
msdelay(50); // delay of 50 mili seconds
LCD_write_string(var1);//Display message on first line
msdelay(15);
LCD_command(0xC0); // initiate cursor to second line
LCD_write_string(var2);//Display message on second line
while (1); //Loop here
} //End of Main
//Function Definitions
void msdelay (unsigned int time) //Function to generate delay
{
unsigned int i, j;
for (i = 0; i < time; i++)
for (j = 0; j < 275; j++);//Calibrated for a 1 ms delay in MPLAB
}
void init_LCD(void) // Function to initialise the LCD
{
LCD_command(0x20); // initialization of 16X2 LCD in 4bit mode
msdelay(15);
LCD_command(0x01); // clear LCD
msdelay(15);
LCD_command(0x0C); // cursor off
msdelay(15);
LCD_command(0x80); // go to first line and 0th position
msdelay(15);
}
void LCD_command(LCDubyte LCDData) //Function to pass command to the LCD
{
LCDWriteByte(LCDData); //Send data on LCD data bus
GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_3,0); //RS = 0 since command to LCD
GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_1,0); //RW = 0 since writing to LCD
}
void LCD_data(LCDubyte LCDData)//Function to write data to the LCD
{
LCDWriteByte(LCDData);
GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_3,GPIO_PIN_3);//RS = 1 since data to LCD
GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_1,0); //RW = 0 since writing to LCD
}
//Function to write string to LCD
void LCD_write_string(char *str)
{
int i = 0;
while (str[i] != 0)
{
LCD_data(str[i]); // sending data on LCD byte by byte
msdelay(15);
i++;
}
}
void LCDWriteByte(LCDubyte LCDData)
{
GPIOPinWrite(GPIO_PORTD_BASE,GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,(LCDData>>4));
GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_2,GPIO_PIN_2); //Generate High to low pulse on EN
msdelay(15);
GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_2,0);
GPIOPinWrite(GPIO_PORTD_BASE,GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,(LCDData));
GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_2,GPIO_PIN_2); //Generate High to low pulse on EN
msdelay(15);
GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_2,0);
msdelay(15);
}