Using CW 10.2, JM60 micro full chip simulation.
My code calculates pressures and temperatures from ADC values and outputs them to a display that accepts ascii characters. Trying to use the sprintf to convert between float and ASCII. When the sprintf lines execute the result loads into the variable locations correctly, but it also corrupts a lot of other variables. Guessing I am running into the overflow issue with sprintf, but I thought the format part would have fixed that. Any suggestions on fixes or alternative methods?
//from header
struct Values
{
float T1;
float T2;
float T3;
float P1;
float P2;
float P3;
};
struct DisplayValues
{
char T1[64];
char T2[64];
char T3[64];
char P1[64];
char P2[64];
char P3[64];
};
//from Main.c
struct Values Vals;
struct DisplayValues DispVals;
void Load_Display(void)
{
int res;
res = sprintf(DispVals.P1, "%6.3f", Vals.P1);
res = sprintf(DispVals.T1, "%4.1f", Vals.T1);
}
解決済! 解決策の投稿を見る。
Could be either that T? destination buffers are overwritten or the default stack space allocated in the prm file might also be too small. What areas got overwitten? The P2/T2 arrays or other globals? Try to increase the STACKSIZE in the prm file (also search for that in the forum, with floats/doubles the default STACKSIZE is often too small.
A simple float to buffer formatting routine might need less stack and also be considerably smaller than sprintf.
Daniel
Also a note about the formatting argument of sprintf. Note that for the printf format width only defines the minimal width which will be used. So sprintf(buffer, "%10f", 1.0F) will always print 10 characters (space filed), it does NOT limit the maximum number of characters written.
E.g. sprintf(buffer, "%1.1f", 1E10) will still print "10000000000.0"
The reason why I don't think in your code the destination buffer overflows is that for IEEE32 floats, the largest float only has 3.4 E38, so with a negative sign, a decimal dot and a few digits after the decimal dot, the max output should still be less than 64 characters, So I guess in your case, you instead run into a overflow, can happen with any code, but with floats/sprintf its much more likely.
Daniel
Could be either that T? destination buffers are overwritten or the default stack space allocated in the prm file might also be too small. What areas got overwitten? The P2/T2 arrays or other globals? Try to increase the STACKSIZE in the prm file (also search for that in the forum, with floats/doubles the default STACKSIZE is often too small.
A simple float to buffer formatting routine might need less stack and also be considerably smaller than sprintf.
Daniel
Daniel,
Default stack size was 0x80, searched for the issue and saw people having trouble even at 0x100. Upped mine to 0x200 as they did to fix their problem and it worked for me. In response to your question it was corrupting other global variables. Thanks for the help.