NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » String manipulation
October 12, 2011 by claesz |
This is really embarrassing, but C is not my strong side and I just got my NK the other day and though I'd test it out by making a scrolling text through string manipulation. I know there are many examples of LCD scrollers on this forum that are all much better and more efficient, and this was just meant as a bit of practice for me. The thing works (though I must be overwriting some null str termination somewhere as it from time to time it will mess itself up). Now, my though was take a text string. Start by adding screen width number of spaces + text string. Only spaces will show. Next step add width-1 number of spaces + text. One letter will show, etc, etc. When number of spaces = 0, just write text+1, text+2 etc. If strlen less than screen width add spaces to fill up. The principle works of course, all though it is a silly way of making a scroller. However, and finally I get to my question. The line //printline[21] = 'x'; has been commented out. In my mind that should actually add an x just off the screen every time (or next line if you don't cut off the string), since the print function will print from start of array until end. Here I must have misunderstood something in how C works, because in reality it will add an "x" as the first letter of the text-to-show string (the first 20 chrs are spaces) and move it along the whole screen as it scrolls. Originally I wanted to add a "0" to avoid writing a string so long that it continued on to the next line. For testing purposes I have put X there now. What am I misunderstanding? I know this is not a NK question and I should have found a C forum to post this question in, but you guys seem friendly so I though I'd give it a shot.
|
---|---|
October 12, 2011 by claesz |
Ok, I am sure I am missing something very basic here, but I cannot for the life of me figure out what. If I do printline[3] = 'x'; There will be an x in a fixed spot, 3rd chr from left, not affected by the scrolling. To me that makes sense, you are setting the third letter of the string just before it is printed. The content of the printline array should not matter - it will always be the third chr that is shown and the rest will just appear to scroll under it. However, if I do printline[21] = 'x' the "x" will scroll along with the rest of the text which makes absolutely no sense. I am beginning to think I am losing my mind, and any advice will be appreciated. |
October 12, 2011 by claesz |
I know I am talking to myself here, but still wanted to give yet another updated. I suspect the declared length of the array printline was the problem here and basically the x overwrote the array string termination. Still doesn't explain the weird behaviour where the "x" starts scrolling with the text. array[21] should still remain in position 21 and not suddenly become dynamic, scrolling along with the text. Also doesn't explain why replacing "x" with 0 would be a problem. I would only have overwritten a string termination with another. |
October 13, 2011 by hevans (NerdKits Staff) |
Hi claesz, I have a theory as to what is happening here, its a bit of a long shot but it might explain what you are seeing. I believe that your thinking about where the null termination is on a char array is off by 1. If I declared a char myStr[5], it would give me an array of 5 characters to work with, this means I could safely address myStr[0] through myStr[4]. If I want to this to be a happy null terminated string I have to make sure myStr[4] is a null 0. Addressing myStr[5] is a buffer overrun, and depending on where on the stack your buffer is living you are quite possibly blowing away the first character in another array. I think you had this in mind when you made your printline array 21 characters wide, but you didn't remember you can only safely address length-1, so setting printline[20] to a null 0 would still give you a 20 character string. It is fairly bizarre that the x ended up at the beginning of your output string, I can't really explain that. Humberto |
October 13, 2011 by claesz |
Thanks Humberto! I believe you are right about my mistake regarding the max array length. Rather a silly mistake. Hopefully I would have noticed it earlier myself if it wasn't for the fact that the "x" started to scroll. Through me off a bit on my attempt to track down the problem Very strange behaviour, but I guess that is what can happen when you overrun the buffer. Thanks a lot! C |
October 13, 2011 by claesz |
Just to close off the thread properly, I am adding the entire code, which now seems to work fine for me. (There is a switch on PC4 to stop/start the scroll and a red diode on PC2 and green on PC3 to indicate scroll status (scrolling or not) none of which are needed of course). The code is messy and there are many much better ways of making a scrolling text. This was just for me to practice a bit.
|
Please log in to post a reply.
Did you know that a flyback diode is important when driving a motor or any inductive load? Learn more...
|