You code looks correct. I have code that is extremely similar to yours with no float issue.
To verify that the snprintf() and the %f is good, are you using the debugger to breakpoint on line 73?
If you are reading TempResult "live" in your debug session and TempResult is used for immediate calculations, it might reflect different information. Using the Debugger to break at line 73 allows you to compare the value of TempResult after it gets pushed through snprintf() and before buffer is pushed through UART_1_PutString().
I think that Len-san's answer is correct.
But in case if you see different values for TempReult in debugger and printed value,
could you test following formatting?
> snprintf(buffer, sizeof(buffer), " Temp = %f \n\r", TempReult) ;
< sprintf(buffer, " Temp = %d.%03d \n\r", (int)TempResult, (int)(TempResult * 1000) % 1000) ;
1 of 1 people found this helpful
You did not show the complete code. Can you please post your complete project so that we all can have a look at all of your settings. To do so, use
Creator->File->Create Workspace Bundle (minimal)
and attach the resulting file.
I downloaded your project.
And I disabled the I2C part, since I don't have the I2C device for this project.
Then I connected an external POT to pretend the temp sensor.
I adjusted the value somewhat close to what you were seeing.
IDE debug screen (Debugger says it's 25.4319611)
Then the TeraTerm log, it also syas 25.431961, not exact but close enough?
So I think that your project/program is working correctly, if I2C is not involved.
Meantime, there could be a chance that you are using some old components.
To make sure that components are up to date, I suggest you to update component
from menu Project > Update Components
Attached is my modified project, please test it if it still show you a different value(s).
If the values are same, then change
#define USE_I2C 0
#define USE_I2C 1
Then try it again.
If the values are not same, probably something with I2C is affecting the result.
1 of 1 people found this helpful
Did moto's recommendation do the trick?
If not, try changing line 26 in main.c from:
float32 ADC_mVolt, R, R1, V, Vr, SumTemp, TempResult;
volatile float32 ADC_mVolt, R, R1, V, Vr, SumTemp, TempResult;
I noticed that sometimes due to code optimizations that a temporary variable gets stored and operated on in a CPU register and never gets place in RAM. Therefore, it might be possible that TempResult might be located in a register and the value reported in the debugger may not match the snprintf() output.
Have you had a chance to try if the volatile keyword fixes the discrepancy between the snprintf() output and the debugger value?
The volatile keyword forces the compiler not to store the data in any of the CPU registers. It is forced to store the result in RAM.
PS: Bob (user_1377889) informed me of an issue of mine that needed a volatile keyword to solve it. I was using the same global variable with my main() and a specific ISR.
An issue showed up that I didn't always get the ISR to 'pass' the correct data to main().
Since the variable was shared and the code was optimized, Bob pointed out that the variable was probably stored in a register and never got assigned to RAM. I took a look at the listing file for main(). He was correct. He recommended adding the volatile keyword where the variable is defined. Doing so, the code then began to work reliably.