CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

printing floats question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
FLoris



Joined: 24 Jun 2011
Posts: 2

View user's profile Send private message

printing floats question
PostPosted: Fri Jun 24, 2011 8:11 am     Reply with quote

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");
}
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Fri Jun 24, 2011 8:26 am     Reply with quote

Defining variables in the midst of code is a C++ feature, that's not supported in CCS C.
Try with a separate variable definition for temp.
FLoris



Joined: 24 Jun 2011
Posts: 2

View user's profile Send private message

thx
PostPosted: Fri Jun 24, 2011 8:38 am     Reply with quote

Thanks for the reply FvM.
That and thanks for fixing the bug!!!!

I need to recheck my code another 5 times before posting Embarassed
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group