0

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Mahesha Padyana
  • 431
  • 6
  • 22
  • Your code looks fine. `while(*ptr2)` will terminate as soon as you reach `'\0'` in the string. Since you are not modifying the string, you could use `char *str2 = "Hello Welcome" ` to avoid copying the literal into writable memory. – Sergey Kalinichenko Jul 26 '15 at 17:05
  • 1
    Looks like you want `displayInRow1(*ptr2++);`, no? – The Paramagnetic Croissant Jul 26 '15 at 17:05
  • The function prototype is void displayInRow1(char* data); So I want to call displayInRow1(ptr2++). I changed the string declaration as per your suggestion as char *str2 = "Hello Welcome"; But not sure why I see eeeeeeeeeeeeeee at the end. – Mahesha Padyana Jul 26 '15 at 17:12
  • 2
    No sane function printing a `char` would take a pointer. Are you sure that function does not already print a string? If not: check the function; that looks broken by design. Post the function. (Strange enough: how can you display >16 chars in one row of an LCD with only 16 chars width?) – too honest for this site Jul 26 '15 at 17:16
  • My complete LCD code is in the link below. Refer to my last edit and not the first post. In the above case, I am not displaying 16 characters. Even otherwise LCD would scroll and display in my above code since I am displaying every time from position 1. I am expecting this almost like a scrolling display. http://stackoverflow.com/questions/31221491/displaying-hexadecimal-value-in-lcd/31231450?noredirect=1#comment50488824_31231450 – Mahesha Padyana Jul 26 '15 at 17:31
  • I did refer to what is the most current version. You should post a [mcve], which includes **correct** error output; that's what we have to rely on'. (Note: normally, such small display do **not** scroll (about the link: tl;dr!), that does the name of your output function actually imply). Apparently my comment was quite correct. – too honest for this site Jul 26 '15 at 18:17

1 Answers1

1

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
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • Thank you for the wonderful explanation. I was also expecting exactly the same what you entered at the end. But I was seeing eeeeeeeee at the end in the display and got confused and I was thinking that loop is not coming out when it encounters '\0'. I could not initially make out why it is displaying it. Now I realize that the last e remains in the display from the previous calls. – Mahesha Padyana Jul 26 '15 at 18:23