I tried to send a character to LCD 16x2 but it failed. Nothing was displayed. With this code I can interface with LCD by PIC and 8051 microcontroller. I changed a little bit to suit to Tiva C Launchpad but it didn't work.
I used pin B0-B7 to transfer data. A5-A6 are RS and E, RW to ground. This is my code:
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#define P0 GPIO_PIN_0
#define P1 GPIO_PIN_1
#define P2 GPIO_PIN_2
#define P3 GPIO_PIN_3
#define P4 GPIO_PIN_4
#define P5 GPIO_PIN_5
#define P6 GPIO_PIN_6
#define P7 GPIO_PIN_7
void Delay_ms(int t)
{
SysCtlDelay(SysCtlClockGet()/(3000/t));
}
void LCD_cmd(char cmd)
{
GPIOPinWrite(GPIO_PORTA_BASE,P5,0);
GPIOPinWrite(GPIO_PORTA_BASE,P6,1<<6);
GPIOPinWrite(GPIO_PORTB_BASE,P0|P1|P2|P3|P4|P5|P6|P7,cmd);
Delay_ms(10);
GPIOPinWrite(GPIO_PORTA_BASE,P6,0);
Delay_ms(20);
}
void LCD_data(char data)
{
GPIOPinWrite(GPIO_PORTA_BASE,P5,1<<5);
GPIOPinWrite(GPIO_PORTA_BASE,P6,1<<6);
GPIOPinWrite(GPIO_PORTB_BASE,P0|P1|P2|P3|P4|P5|P6|P7,data);
Delay_ms(10);
GPIOPinWrite(GPIO_PORTA_BASE,P6,0);
Delay_ms(20);
}
void LCD_WrStr(char *str)
{
while(*str)
{
LCD_data(*str);
str++;
}
}
void SetCursor_LCD(unsigned char m,unsigned char n) //Chinh vi tri con tro
{
if (m==1)
{
LCD_cmd(0x80+n);
Delay_ms(10);
}
else
{
LCD_cmd(0xC0+n);
Delay_ms(10);
}
}
void LCD_init(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE,P0|P1|P2|P3|P4|P5|P6|P7);
GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE,P5|P6);
GPIOPinWrite(GPIO_PORTA_BASE,P5,0);
GPIOPinWrite(GPIO_PORTA_BASE,P6,0);
GPIOPinWrite(GPIO_PORTB_BASE,P0|P1|P2|P3|P4|P5|P6|P7,0);
// Delay_ms(200);
// LCD_cmd(0x30);
// Delay_ms(500);
// LCD_cmd(0x30);
// Delay_ms(110);
// LCD_cmd(0x30);
// Delay_ms(1000);
LCD_cmd(0x38);
Delay_ms(1000);
LCD_cmd(0x0C);
Delay_ms(1000);
LCD_cmd(0x06);
Delay_ms(1000);
LCD_cmd(0x01);
Delay_ms(1000);
}
int main(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
LCD_init();
SetCursor_LCD(1,0);
Delay_ms(100);
LCD_data('H');
}