HAVING TROUBLE DISPLAYING AN INTEGER TO A 0216  LCD - Page 1
Forum section: electronics / beginners

author: neko efecktz
author_rank: Regular Contributor
timestamp: March 19, 2017, 01:37:19 am
content: Good morning fellow EEVBLOG contributors.I have a small problem i was hoping someone might be able to help me with.I have been trying to display an integer to a 2 line 16 char LCD display.cap-1.PNG shows the sample code I downloaded to insert into my own program code.It was in the form of a JPEG picture file.The lineWriteDataToLCD(Count+0x03);is not workingat the top of the code there is a lineunsigned Count = 0;I have the lineunsigned COUNTER = 0;I am trying to display text plus the integer.LED 3or whatever the count is up to.my code is cap-2.PNGI have tried displaying just the integer as in the sample code.it does not work.I have tried several variations but have no idea what is wrong.can someone tell me what is the+0x30for?sprintf(OutString, "LED ") + (COUNTER+0x30);Lcd_Write_String(OutString);sprintf(OutString, "LED ") + (COUNTER);Lcd_Write_String(OutString);Lcd_Write_String ("LED " + (COUNTER+0x30));Lcd_Write_String ("LED " + (COUNTER));thanking you in advanceBILL

author: boffin
author_rank: Unknown
timestamp: March 19, 2017, 01:43:27 am
content: Quote from: neko efecktz on March 19, 2017, 01:37:19 amThe difference is between 3 (the number) and "3" the character as displayed on the screen.The digits 0-9 are characters 48-59 in the ASCII character set or in hex 0x30 - 0x39.So if you add the number 3 to the offset of what represents the numbers in the ASCII character set (0x30) you get the character for the digit 3 (0x33)

author: Nusa
author_rank: Super Contributor
timestamp: March 19, 2017, 02:04:35 am
content: You don't snow the declaration of OutString, but as for the sprintf, try:sprintf(OutString, "LED %d", COUNTER);As for the rest, we don't even know what microprocessor and LCD library you're using.

author: neko efecktz
author_rank: Regular Contributor
timestamp: March 19, 2017, 03:05:34 am
content: Thank you Nusa.That worked exactly as I wanted.sprintf(OutString, "LED %d", COUNTER);The pic I am using is PIC16F877AThe original code I tried to modify with the COUNTER+0x30 code was something that I got from IANM a few months agoThe COUNTER+0x30 part of the code came from a blog by Saeed Yasin.Would the COUNTER+0x30 work in a different compiler or is it just rubbish code?Once againThank you for your helpBILL

author: IanB
author_rank: Super Contributor
timestamp: March 19, 2017, 03:12:43 am
content: Quote from: neko efecktz on March 19, 2017, 03:05:34 amIt's not rubbish code, but you have to understand what it does. If you have single digit integer value in the range 0-9 such as 4, then (value + 0x30) will produce the ASCII code for that digit in printable form, for example it will turn 4 into '4'. But it's important to understand that '4' is a single character, not a string.This technique can be useful in cases when you want to have compact and efficient code, and where the needs are simple.

author: neko efecktz
author_rank: Regular Contributor
timestamp: March 19, 2017, 03:45:37 am
content: I don't understand then why the code wouldn't work.I effectively replicated the code by Saeed Yasin but didn't see any numbers, only the text. LEDattached is the entire code supplied by Saeed Yasin.It is onlyThe main function code.BILL

author: Ian.M
author_rank: Super Contributor
timestamp: March 19, 2017, 04:07:03 am
content: Please post code in code tags, or as an attached C source file, as it is a PITA dealing with it as a JPEG - we cant even do something simple like open it in our favourite editor to check your braces and brackets are properly balanced. Also please provide a link to the blog page you are working from.My first question would be: Does it actually reach the second ClearLCDScreen()?

author: StillTrying
author_rank: Super Contributor
timestamp: March 19, 2017, 04:38:04 am
content: Confucius says:COUNTER+0x30converts the integers 0-9 into the printable ASCII characters 0-9.The+0x30is not needed if you're usingsprintf(OutString, "LED %d", COUNTER);becausethe%dtells sprintf() to convert the integer in COUNTER into a string of printable ASCII number characters, which is better, because sprintf() goes further than just 0-9, - it'll domultiple digitnumbers.

author: Ian.M
author_rank: Super Contributor
timestamp: March 19, 2017, 04:51:16 am
content: OTOH all the printf() family functions take a lot of RAM and program memory, so although that's a good way of getting something working quickly, you will probably still need to learn how to do your own integer to ASCII conversions the hard way. A middle road is to use the library function itoa(), which can convert an integer to ASCII in any number base up to 16 (HEX), but you need to allocate an output buffer for it with enough space to hold the longest possible number + a terminating '\0'. It wont be as efficient as hand crafted number conversion code for a specific task, but is often good enough.

author: neko efecktz
author_rank: Regular Contributor
timestamp: March 19, 2017, 07:19:47 am
content: IanMhere is the link to the code I reproduced to use in my project.http://2.bp.blogspot.com/-JXMA2FBhjL4/UUtqnIlellI/AAAAAAAACfg/wkYx5pgLzz8/s1600/PIC16F877+UP_DOWN+COUNTER+MAIN+CODE.bmpas I said, it is incomplete,attached is the complete code for project.I haven't done much with it for a few months as I'm also playing about with a Raspberry PI 3I know the header part is not the best.Ian you have seen this and helped me a lot on it and Stilltrying, I believe you have also given some help in the pastAll the bestBILL.