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
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.