Search code examples
cavr-gcc

Issue with Null terminating string display


I have written small piece of code for displaying data in 2x16 character LCD.

char str2[] = "Hello Welcome";
char *ptr2=str2;

initializeLCD();
sendLCDCommand(CLEAR_DISPLAY);
while(*ptr2) {
    displayInRow1(ptr2++);
    CL_delayMS(300);
}

In this code, the display displays "Hello Welcomeeeeeeeeeeeeeeeeeeeee" I thought while(*ptr2) will terminate after printing last letter "e" in the string. If I comment the line containing while(*ptr2), the data is displayed correctly. I am not sure what is wrong with this code above. I am using Atmel Studio with Atmega328 controller. displayInRow1() function is tested and no issue with that.


Solution

  • Your function displayInRow1() takes a pointer and passes it to sendTextToLCD() which displays a string. That's why it works correctly when you remove the while. So all you need is

    initializeLCD();
    sendLCDCommand(CLEAR_DISPLAY);
    displayInRow1(str2);
    

    I am not going to try to explain the result you are getting, except to say the while loop displays something like this

    Hello Welcome
    ello Welcome
    llo Welcome
    lo Welcome
    o Welcome
     Welcome
    Welcome
    elcome
    lcome
    come
    ome
    me
    e