1

I wrote a code for LCD interfacing with the tivaC launch pad but when I try to run it nothing appears to be happening i posted the code I have written also i have ensured the hardware connections can the delay function has an impact on not making the LCD work. Is there any logical error in the code that doesn't make the LCD performs it job.

#include "tm4c123gh6pm.h"
#define LCD_RS (*((volatile unsigned long *)0x40004200))    //PA.7 for 
  register select pin
#define LCD_EN  (*((volatile unsigned long *)0x40004100))   //PA.6 for enable 
 pin
#define LCD_RW  (*((volatile unsigned long *)0x40004080))   //PA.5 for rw pin

/*function protoypes */
void LCD_INIT(void);
void LCD_CMD(unsigned long cmd);
void LCD_WRITE (char data);
void Delay(void);

/*this function must be writen as keil 
allows running of a function before the main*/
void SystemInit() {
};

int main (void) {
    LCD_INIT();
    while(1) {
        LCD_CMD(0X38);  //8-bit bus mode, 2 line display mode, 5x8 dots display mode
        Delay();
        LCD_CMD(0X01);  //clear display
        Delay();
        LCD_CMD(0X10);  //cursor display shift
        Delay();
        LCD_CMD(0X0C);  //diplay is on
        Delay();
        LCD_WRITE('A'); //send character A on the LCD 
        Delay();
    }
}

/*this function will initate the LCD it initializes 
the portA5-6-7 as the control pins  for the LCD and portB0-7 
as the data pins*/
void LCD_INIT(void) {
    volatile unsigned long delay;
    SYSCTL_RCGC2_R |= 0X00000002;   // allow the clock for portB
    delay = SYSCTL_RCGC2_R;     // short delay for clock
    GPIO_PORTB_AFSEL_R &= ~0xff;    //disable alternative functions for portB
    GPIO_PORTB_AMSEL_R &= ~0Xff;    //disable analogue function
    GPIO_PORTB_PCTL_R &= ~0XFF;     //regular digital pins
    GPIO_PORTB_DIR_R  |= 0XFF;      //set the direction of PB0-7 as     output
    GPIO_PORTB_DEN_R  |= 0XFF;      //enable digital portB

    SYSCTL_RCGC2_R |= 0X00000001;   // allow the clock for PA5,6,7
    delay = SYSCTL_RCGC2_R;     // short delay for clock
    GPIO_PORTA_AFSEL_R &= ~0xE0;    //disable alternative functions for PA5,6,7
    GPIO_PORTA_AMSEL_R &= ~0XE0;    //disable analogue function for PA5,6,7
    GPIO_PORTA_PCTL_R &= ~0XE0;     //regular digital pins
    GPIO_PORTA_DIR_R |= 0XE0;       //set the direction of PA5,6,7 as output
    GPIO_PORTA_DEN_R |= 0XE0;       //enable digital PA5,6,7
}

//this function passes the command to the LCD
void LCD_CMD(unsigned long cmd) {
    GPIO_PORTB_DATA_R = cmd;    //set PB7-0 as the passed command to the function
    LCD_RS = 0x00;  //set PA7 register select pin to low
    LCD_RW = 0x00;  //set PA5 r/w pin to low
    LCD_EN = 0x40;  //set enable pin to high
    Delay();        //short delay 
    LCD_EN = 0x00;  //set enable pin to low 
}

//this function passes the data to the LCD
void LCD_WRITE (char data) {
    GPIO_PORTB_DATA_R = data;   //write the data to PB7-0
    LCD_RS = 0x80;  //set PA7 to high
    LCD_RW = 0x00;  //set pA5 to low
    LCD_EN = 0x40;  //set the enable pin high
    Delay();        //short delay
    LCD_EN = 0x00;  //set the enable pin to low
}

//short delay function
void Delay(void) {
    unsigned long  time;
    time = 12000; 
    while(time){
        time--;
    }
}
cooperised
  • 2,404
  • 1
  • 15
  • 18
ayman magdy
  • 63
  • 1
  • 7
  • Do you have a part number for the LCD? Does it have a datasheet? – Andy J Jul 01 '18 at 03:56
  • the LCD number is JHD 162A i think most of the 16x2 LCDs are the same in commands and pins. – ayman magdy Jul 01 '18 at 09:05
  • Just reading [this topic](https://www.ccsinfo.com/forum/viewtopic.php?t=51390) it seems as though some people need to add a short delay before calling `LCD_INIT()` to give the supply voltage time to get high enough. – Andy J Jul 01 '18 at 09:32
  • i tried it but still no data is shown on the LCD. i tried to debug on the keil simulator and for me every thing is fine the GPIO_PORTB_DATA_R which is the data register i am sending the data to is changing correctly also the portA data register which i'm sending the control signals to is changing fine. – ayman magdy Jul 01 '18 at 10:16
  • My experience of these 16x2 LCD devices is that they're very sensitive to timing. Note from the datasheet that the required delay after each command differs. Try making all of your delays longer - usually there is no maximum delay (within reason). And I'd advise using a proper delay, not a blind loop, because the timing of the loop is not easy to judge; and the whole thing could be optimised out by the compiler anyway (unless you declare the 'time' variable as volatile). – cooperised Jul 02 '18 at 10:23
  • Supplementary comment: what is the 'delay' variable in LCD_INIT() supposed to do? Currently it does nothing at all. – cooperised Jul 02 '18 at 10:30
  • @cooperised the delay variable in LCD_INIT() is to give the clock time to settle down after allowing the clock of the port it is mandatory in cortex-M. I will try to write the delay with the systick timer. – ayman magdy Jul 04 '18 at 14:39
  • @aymanmagdy Just looked it up - weird! I haven't used the TM4C123, and this is an oddity of this device (it is certainly *not* related to the Cortex-M core). It looks like it imposes a delay by deliberately delaying a memory bus transaction. Seems a bit dodgy to me - I wouldn't be confident that this would survive all possible compiler optimisations, even with the volatile qualifier - but ok, it certainly seems like it's what you're encouraged to do. – cooperised Jul 04 '18 at 22:01
  • @aymanmagdy Systick is a good way to go, but any general-purpose timer can be made to work. Just enter a while loop that terminates when the timer reaches a given value. Not efficient, but certainly effective. Let us know how you get on. – cooperised Jul 04 '18 at 22:02
  • @cooperised I used the systick and it works fine. thx for your concern. – ayman magdy Jul 05 '18 at 16:11
  • @aymanmagdy Great, is the screen working too? – cooperised Jul 06 '18 at 21:15
  • 1
    @cooperised yes everything is working fine now. I'm thinking of doing some changes the the `LCD_INIT()` function to avoid sending too many commands in the beginning. – ayman magdy Jul 09 '18 at 10:49

0 Answers0