Interfacing 20x4 character LCD with TI TM4C1294 generates some random characters

286 Views Asked by At

I have a 20x4 character LCD connected to my TI TM4C1294 kit as follows:

  1. Data bus to port A.
  2. RS, RW and E pins to port L 0-2

I wrote this code in order to drive it:

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

void LCD_SendChar(short c)
{
    GPIO_PORTA_AHB_DATA_R = c;
    GPIO_PORTL_DATA_R |= 0x01;
    GPIO_PORTL_DATA_R |= 0x04;
    // asm ("   NOP");
    SysCtlDelay(18);
    GPIO_PORTL_DATA_R &= ~(0x04);
    SysCtlDelay(10000);
}

void LCD_SendCmd(short command)
{
    GPIO_PORTA_AHB_DATA_R = command;
    GPIO_PORTL_DATA_R |= 0x00;
    GPIO_PORTL_DATA_R |= 0x04;
    // asm ("   NOP");
    SysCtlDelay(18);
    GPIO_PORTL_DATA_R &= ~(0x04);
    SysCtlDelay(10000);
}

void LCD_SendString(char* value)
{
    while(*value != 0)
    {
        LCD_SendChar(*value++);
    }
}

void LCD_Init()
{
    LCD_SendCmd(0x01);
    LCD_SendCmd(0x38);
    LCD_SendCmd(0x0E);
    LCD_SendCmd(0x06);
    LCD_SendCmd(0x02);
}

int main(void)
{
    // Enable clock gating control for GPIO A and L
    SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R10;
    SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R0;

    // Wait until GPIO blocks are ready.
    while((SYSCTL_PRGPIO_R & SYSCTL_PRGPIO_R10) == 0);
    while((SYSCTL_PRGPIO_R & SYSCTL_PRGPIO_R0 ) == 0){};

    // Configure as Output, digital, no alternate or analog functions.
    GPIO_PORTA_AHB_DIR_R = 0xFF;
    GPIO_PORTA_AHB_DEN_R = 0xFF;
    GPIO_PORTA_AHB_AFSEL_R = 0x0;
    GPIO_PORTA_AHB_AMSEL_R = 0x0;
    GPIO_PORTA_AHB_PCTL_R  = 0x0;

    GPIO_PORTL_DIR_R = 0x7;
    GPIO_PORTL_DEN_R = 0x7;
    GPIO_PORTL_AFSEL_R = 0x0;
    GPIO_PORTL_AMSEL_R = 0x0;
    GPIO_PORTL_PCTL_R  = 0x0;

    GPIO_PORTL_DATA_R = 0x00;
    SysCtlDelay(2200000);

    LCD_Init();

    LCD_SendCmd(0x94);
    LCD_SendString("Hello World!");
    LCD_SendCmd(0xD4);
    LCD_SendString("Hello World!");

    while (1);

}

The board is running at a 120MHz speed.

The problem is with line addressing. I found out that the line addressing for a 20x4 LCD are 0x80, 0xC0, 0x94, 0xD4 for lines 1 to 3 respectively. When sending a "Hello World" on two lines as shown the two strings are displayed on the same line with a random character between. If I try one "Hello World" for one line while changing line address each time it works fine. What could be the reason for not addressing the lines correctly? and what's the deal with such random characters?

I suspect with:

  1. Values provided for each delay function are not accurate.
  2. I have read that I have to check if the LCD is busy or not but I did not understand the mechanism behind it.
0

There are 0 best solutions below