FLoris
Joined: 24 Jun 2011 Posts: 2
|
printing floats question |
Posted: Fri Jun 24, 2011 8:11 am |
|
|
Hello people,
I am curious about an error that happend today. i was programming my temperature sensor (NTC resistance to ADC) and calculating the temperature with the values i already obtained. those variables where printed to my pc and I could read them perfectly
my last value (the temperature) proved to be an problem. i was able to calculate it within the controller but could not print the value towards my pc.
the code:
Code: |
// A, B , C already known variables for in my equation
// temp = temperature
//
float temp = (1.0 /(A + (B * log(R)) + (C * pow(log(R),3.0))))-273.15;
printf("de temperatuur = %f \n\r",temp);
|
this printf produce -273,15... not the temp i wanted to see
but when i used this code:
Code: |
printf("B*log r + C*pow(log(R),3.0)= %f\r\n",(1.0 /(A + (B * log(R)) + (C * pow(log(R),3.0))))-273.15);
|
it started to print the temprature i wanted to know.
could someone tell me why the first method did not work and the second method did work??
does printf have a problem with the float im sending it in the first method?
if so why does he accept exactly the same float in the second?
Thanks!!!
bigger code for understanding :
Code: |
#define ADC_MAX 1024.0
#define R2K2 2180.0
float A = 0.1473921876E-2;
float B = 0.2379423397E-3;
float C = 0.1071824519E-6;
float R;
unsigned int16 ADCResult = 0;
while (1)
{
read_adc(ADC_START_ONLY);
int1 done = adc_done();
while(!done)
{
done = adc_done();
}
ADCResult = read_adc();
printf("Analog to digital value = %Lu \n\r", ADCResult);
printf("V over the NTC = %f \n\r", ((float)ADCResult*(3.187/1024.0)));
printf("R over the NTC = %f \n\r",((ADC_MAX * R2K2)/((float)ADCResult))-R2K2);
R = ((ADC_MAX * R2K2)/((float)ADCResult))-R2K2;
//R=2200; // for testing the equation with fixed resistance ovet NTC
float temp = (1.0 /(A + (B * log(R)) + (C * pow(log(R),3.0))))-273.15; //returns -275,15 all the time
printf("the temperature = %f \n\r",temp);
printf("the temperature = %f\r\n",(1.0 /(A + (B * log(R)) + (C * pow(log(R),3.0))))-273.15); //works just fine
printf("\n\r");
}
|
|
|