Author Topic: HAVING TROUBLE DISPLAYING AN INTEGER TO A 0216 LCD  (Read 1236 times)

0 Members and 1 Guest are viewing this topic.

Offline neko efecktz

  • Regular Contributor
  • *
  • Posts: 154
  • Country: au
HAVING TROUBLE DISPLAYING AN INTEGER TO A 0216 LCD
« on: March 19, 2017, 01:37:19 am »
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 line          WriteDataToLCD(Count+0x03);            is not working
at the top of the code there is a line        unsigned Count = 0;
I have the line          unsigned COUNTER = 0;

I am trying to display text plus the integer.    LED 3     or whatever the count is up to.

my code is cap-2.PNG

I 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 +0x30 for?

            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 advance
BILL



« Last Edit: March 19, 2017, 01:40:38 am by neko efecktz »
 

Offline boffin

  • Supporter
  • ****
  • Posts: 898
  • Country: ca
Re: HAVING TROUBLE DISPLAYING AN INTEGER TO A 0216 LCD
« Reply #1 on: March 19, 2017, 01:43:27 am »
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 line          WriteDataToLCD(Count+0x03);            is not working
at the top of the code there is a line        unsigned Count = 0;
I have the line          unsigned COUNTER = 0;

I am trying to display text plus the integer.    LED 3     or whatever the count is up to.

my code is cap-2.PNG

I 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 +0x30 for?

            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 advance
BILL

The 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)

 

Offline Nusa

  • Super Contributor
  • ***
  • Posts: 1610
  • Country: us
Re: HAVING TROUBLE DISPLAYING AN INTEGER TO A 0216 LCD
« Reply #2 on: March 19, 2017, 02:04:35 am »
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.
 

Offline neko efecktz

  • Regular Contributor
  • *
  • Posts: 154
  • Country: au
Re: HAVING TROUBLE DISPLAYING AN INTEGER TO A 0216 LCD
« Reply #3 on: March 19, 2017, 03:05:34 am »
Thank you Nusa.
That worked exactly as I wanted.
sprintf(OutString, "LED %d", COUNTER);
The pic I am using is PIC16F877A
The original code I tried to modify with the COUNTER+0x30 code was something that I got from IANM a few months ago
The 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 again
Thank you for your help
BILL
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 9593
  • Country: us
Re: HAVING TROUBLE DISPLAYING AN INTEGER TO A 0216 LCD
« Reply #4 on: March 19, 2017, 03:12:43 am »
Would the COUNTER+0x30 work in a different compiler or is it just rubbish code?

It'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.
I'm not an EE--what am I doing here?
 

Offline neko efecktz

  • Regular Contributor
  • *
  • Posts: 154
  • Country: au
Re: HAVING TROUBLE DISPLAYING AN INTEGER TO A 0216 LCD
« Reply #5 on: March 19, 2017, 03:45:37 am »
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.  LED

attached is the entire code supplied by Saeed Yasin.
It is only The main function code.

BILL

 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 8112
Re: HAVING TROUBLE DISPLAYING AN INTEGER TO A 0216 LCD
« Reply #6 on: March 19, 2017, 04:07:03 am »
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()?
 

Offline StillTrying

  • Super Contributor
  • ***
  • Posts: 2268
  • Country: dk
  • Country: Broken Britain
Re: HAVING TROUBLE DISPLAYING AN INTEGER TO A 0216 LCD
« Reply #7 on: March 19, 2017, 04:38:04 am »
Confucius says:

COUNTER+0x30 converts the integers 0-9 into the printable ASCII characters 0-9.

The +0x30 is not needed if you're using sprintf(OutString, "LED %d", COUNTER); because

the %d tells 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 do multiple digit numbers.
« Last Edit: March 19, 2017, 04:39:40 am by StillTrying »
CML+  That took much longer than I thought it would.
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 8112
Re: HAVING TROUBLE DISPLAYING AN INTEGER TO A 0216 LCD
« Reply #8 on: March 19, 2017, 04:51:16 am »
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.
 

Offline neko efecktz

  • Regular Contributor
  • *
  • Posts: 154
  • Country: au
Re: HAVING TROUBLE DISPLAYING AN INTEGER TO A 0216 LCD
« Reply #9 on: March 19, 2017, 07:19:47 am »
IanM
here 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.bmp

as 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 3
I 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 past

All the best
BILL.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf