I have to following code:
// Write screen n = sprintf( screen_msg, " TV: %#4.1f FV: %#4.1f CV: %#4.1f SV: %#4.1f\r\n", ad_value[2], ad_value[1], ad_value[0], ad_value[3] ); n += sprintf( screen_msg + n, " TI: %#4.1f AT: %#4.1f CT: %#4.1f ST: %#4.1f\r\n", ad_value[4], ad_value[5], ad_value[7], ad_value[6] ); n += sprintf( screen_msg + n, "\r\n" ); n += sprintf( screen_msg + n, "ENGINE CHARGE CLK EN DIS" ); write_0( screen_msg );
When the string is printed to my LCD CV, SV, and ST are always zero, regardless of the value in the ad_value array. If I print just those values in the same routine they appear as they should. When I debug the program the entire string appears as it should. My screen_msg buffer is 256 characters and the entire string in only 170.
I have looked over the code a dozen times and can't figure out what's going on. Can anyone see something that I may be missing?
Thanks, Paul
When I started out using XDATA meant an external chip and all the adress decoding hardware that went with it. And using printf once in a project would eat up half the available internal ROM so it was always avoided if possible. These days it isn't as important.
Fixed point means that for example if all your numbers are positive, less than 6553 and you only nead one decimal place then instead of using floats, use 16 bit integers and store them as 10 times the actual value. Then when you come to print the value just manually insert the decimal place before the last figure.
A simplified routine to convert such a number to a string is as follows. It would need a little bit of work to remove any leading zero's and it could be tidied up a lot but it should give you an idea.
char *shorttostr (unsigned short value) { static unsigned char valuestr[5]; valuestr[5] = '\0'; valuestr[0] = (value/1000); valuestr[1] = ((value-(1000*valuestr[0]))/100); valuestr[2] = ((value-(1000*valuestr[0])-(100*valuestr[1]))/10); valuestr[4] = (value-(1000*valuestr[0])-(100*valuestr[1])-(valuestr[2]*10)); valuestr[0] +='0'; valuestr[1] +='0'; valuestr[2] +='0'; valuestr[3] = '.'; valuestr[4] +='0'; return (valuestr); }
MOst often, integers are emitted into a temporary buffer from least significant digit. When the conversion is done, and you know how many digits the result become, you then check if the data should be padded with one or more spaces or zeroes and if a sign needs to be added.
Conversion right-to-left requires one division, one modulo and no multiply for each emitted digit and is well-suited for a loop.
But if a project already needs (s)printf() for other reasons, there is no reason to not use it. It isn't really so big. The bad part is for the processor to have to emulate floating point. And if the compiler treat double as an alias for float, there can be problems with the precision, since floating point can not represent most numbers exactly.
Per: Quite right, how about
char *shorttostr (unsigned short value) { unsigned char valuestr[7]; valuestr[6] = '\0'; valuestr[4] = '.'; for (x=5; x>=0; x--) { if (x!=4) { if ( !value && x<3 ) valuestr[x] = ' '; else { valuestr[x] = value%10 +'0'; value /= 10 } } } }
Here is an example (just a demo, to use or not to use - this is you who decides ;-) ) suitable to use with, e.g., ADC, but not limited to. So, for an ADC with (always) known numeric scale, if you want to display a conversion result in a-la floating point style, you may want to regard using construction like this:
prerequisites int - 16-bit integer long - 32-bit integer #define ADCSCALE_CODE 1024L - known a-priori #define ADCSCALE_VOLT 5L - known #define MY_PRECISION 1000L - I want to see 3 digits after decimal point
MY_PRECISION can not be arbitrary, but provide for lTmp (see below) do not exceed the 32-bit long value, considering sign bit where applicable.
pseudo-code long lValue4User; long lTmp; int iAdcSample; lTmp= ((long)iAdcSample) * ADCSCALE_VOLT * MY_PRECISION; lValue4User= lTmp / ADCSCALE_CODE; The way to display to the user 1) Display lValue4User/MY_PRECISION 2) Display decimal point 3) Display lValue4User%MY_PRECISION Numeric demo Let the iAdcSample= 830, so expected real-world value is 830/1024*5= 4.0527 (Volt). Here what we have: lTmp= 830L * 5L * 1000L= 4150000L; lValue4User= 4150000L/1024L= 4052L; Now you can display it to an user as set of {4052/1000= 4, and 4052%1000= 52}.
If ADCSCALE_VOLT is not 'round' number, you (almost) always may adjust formula with arbitrary koefficient.
Be cautious about not using 'L' in #defines shown above - many compilers will do implicit conversions, but few of them can give you a wrong code (!).
I really like shown approach for the simplicity to use to display real-world numbers to an user. It does not involve float but long, and works fast for most platforms. Of course, more rigorous consideration should be taken if you are concerned with precision.