This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

interfacing 16X2 lcd with tiva

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);

}



  • Hello Mahavir,

    A thread of interest. Please see if it the same panel as you have?

    http://e2e.ti.com/support/microcontrollers/tiva_arm/f/908/t/385440.aspx

    Regards

    Amit

  • Thanks,for your reply,After going through that code (suggested by you) I have made following changes in it

    changed the delay...

    . But still it is not working,,,,,

    my present code is

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

    void Lcd_command(unsigned char data)
    {
    GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0|GPIO_PIN_1,0x00);

    GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, data);//LCD data
    GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0,0);
    SysCtlDelay(SysCtlClockGet()/50);
    GPIOPinWrite(GPIO_PORTD_BASE,GPIO_PIN_1,GPIO_PIN_1);//enabling lcd by high to low pulse
    SysCtlDelay(SysCtlClockGet()/50);
    GPIOPinWrite(GPIO_PORTD_BASE,GPIO_PIN_1,0);
    }

    void Lcd_data(unsigned char data)
    {
    GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0|GPIO_PIN_1,0x00);

    GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, data);//LCD data
    GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0|GPIO_PIN_1,0x01);
    SysCtlDelay(SysCtlClockGet()/50);
    GPIOPinWrite(GPIO_PORTD_BASE,GPIO_PIN_1,GPIO_PIN_1);//enabling lcd by high to low pulse
    SysCtlDelay(SysCtlClockGet()/50);
    GPIOPinWrite(GPIO_PORTD_BASE,GPIO_PIN_1,0);
    }

    void Lcd_Init()
    {
    Lcd_command(0x38);
    SysCtlDelay(SysCtlClockGet()/50);

    Lcd_command(0x0c);
    SysCtlDelay(SysCtlClockGet()/50);

    Lcd_command(0x01);
    SysCtlDelay(SysCtlClockGet()/50);

    }

    void Lcd_String_Display(unsigned char *str)
    {
    while(*str)
    {
    Lcd_data(*str);
    str++;
    }
    }


    int main(void)
    {
    SysCtlClockSet(SYSCTL_SYSDIV_16|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

    GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3| GPIO_PIN_4| GPIO_PIN_5| GPIO_PIN_6| GPIO_PIN_7);
    GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_0|GPIO_PIN_1);

    //GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0|GPIO_PIN_1,0x00);

    Lcd_Init();
    Lcd_String_Display("hai how are you....");

    }

    With Regards

    Mahavir..

  • Hello Mahavir

    Is it the same panel as the original post? If not then can you please specify which panel it is and how are you interfacing the pins between TM4C123 and the Panel?

    What about contrast and backlight?

    Regards

    Amit

  • mahavir dwivedi said:
    I have used lcd in 4 bit mode

    Four bit mode is far more complex than the standard (8 bit) mode.  And - demands very exact initialization - which your code misses badly.

    Indeed 4 bit mode saves 4 GPIO (we use it w/these modules) but we've decades of experience - you not so much.  KISS is kicked to the curb - that's not an especially productive means to success.

    You have to comply w/LCD's controller data timings.  Code you post seems unlikely to meet that standard.  Does it not make sense for you to:

    a) find, read, comply w/LCD controller spec

    b) measure & compare that your various MCU data transfer timings match the LCD's spec  (past post I outlined simple method to gauge timings w/out benefit of a scope)

    c) employ KISS - which means 8 bit data transfer to the Lcd.  Only after that's up/running would I venture into the added complexity of 4 bit...

    And - if you've "harvested" your Lcd from some grey/surplus source - all bets may be off...  (sometimes "savings" prove not too effective...)

  • Hi,Amit

    Yes,It is the same panel used in the post.I have connected R/W and contrast to ground.keeping that I have made no  connections to backlight.Connected data pins of panel to

    Port B....Data pins from DB0 to DB7

    R/W..gnd

    VEE(contrast).....gnd

    Enable ..to pin 0 of port D

    RS........to pin 1 of portD

    NO connections to backlight pins of LCD

    This time I have Interfaced LCD in 8bit mode.,,,My present Code is ...


    /* 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"

    //Declarations
    #define LCD_DATA    GPIO_PORTB_BASE        //LCD data port to PORTb
    #define ctrl        PORTE                //LCD control port to PORTE
    #define rs          GPIO_PIN_1            //register select signal to RE0

    #define en          GPIO_PIN_2            //enable signal to RE2

    //Function Prototypes
    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[] = "Wel Come";

        /*Set the clocking to directly run from the crystal at 16MHz*/
        SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);

        /* Set the clock for the GPIO Port F */
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);


        /* Set the type of the GPIO Pin */
        GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);

        GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE,GPIO_PIN_0|GPIO_PIN_1);


        init_LCD();                                   // call function to initialise of LCD
        SysCtlDelay(SysCtlClockGet()/10);         // delay of 50 mili seconds

        LCD_write_string(var1);                    //Display message on first line
        SysCtlDelay(SysCtlClockGet()/10);

        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(0x38);      // initialization of 16X2 LCD in 8bit mode
        SysCtlDelay(SysCtlClockGet()/10);
        LCD_command(0x01);      // clear LCD
        SysCtlDelay(SysCtlClockGet()/10);
        LCD_command(0x0C);      // cursor off
        SysCtlDelay(SysCtlClockGet()/10);
        LCD_command(0x80);      // go to first line and 0th position
        SysCtlDelay(SysCtlClockGet()/10);
    }

    void LCD_command(unsigned char cmd) //Function to pass command to the LCD
    {
        /* Writing Command on Port B*///Send data on LCD data bus
        GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,cmd);

        GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0,0);//RS = 0 since command to LCD

        GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1,GPIO_PIN_1);//Generate High to low pulse on EN
        SysCtlDelay(SysCtlClockGet()/10);
        GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1,0);


    }

    void LCD_data(unsigned char data)//Function to write data to the LCD
    {
        /* Writing Command on Port B*///Send data on LCD data bus
        GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,data);

        GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0,GPIO_PIN_0);//RS = 1 since command to LCD

        GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0|GPIO_PIN_1,0x03);//Generate High to low pulse on EN
        SysCtlDelay(SysCtlClockGet()/10);
        GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0|GPIO_PIN_1,0x01);


    }
    //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++;
        }
    }

    Please reply ASAP...

    With Regards,

    Mahavir

  • Hey Mahavir,
    I have done the interfacing using register level programming. It works good.
  • Hello Nikhil
    Could you give me your code about this?
    I'm trying to connect my LCD 16x2 with tm4c123
    Thank you so much.
  • This "trail" may have gone more than a "bit" cold! Post is 16 month's past - and poster has not been recently noted.
    Advice w/in this thread should enable your success. "Some" effort (beyond simple copy/paste) usually is required. And proves illuminating!