1

Please consider the following code:

unsigned int  beta;

unsigned int theta;

unsigned int m = 4;

unsigned int c = 986;

unsigned int Rpre = 49900;



unsigned int getUAnalog(unsigned char channel) // to get the PT1000 Signal

{

unsigned int result;

unsigned int f_temp;

//select channel and initiate conversion

ADCSC1 |= (channel & 0b11111);



//wait until conversion is complete

while(!ADCSC1_COCO);



f_temp = ADCRH;

f_temp <<= 8;

f_temp += ADCRL;



beta = (((f_temp) / (4096*28))*1000); // warning: possible loss of data.

theta = ((((beta)*(Rpre))/(1-beta))*1000);

result = (theta-c)/(m);

return result;
}

I am using MC9S08DZ60 (http://www.freescale.com/files/microcontrollers/doc/data_sheet/MC9S08DZ60.pdf) with PT1000-Temperature Sensor on CodeWarrior version 5.9.0 . This function is meant to calculate the temp and return "result". But the "beta" and "theta" values remain 0. No Change.

Also i get the warning as C2705: Possible Loss of Data. The value of "result" is not right. PLease help as i dont have clue of what going wrong!!

Thanks in advance!

Philipp
  • 67,764
  • 9
  • 118
  • 153
sheetansh
  • 195
  • 2
  • 11
  • 4
    what is `sizeof(unsigned int)` on your platform? `4096*28` will overflow a 16-bit uint. – simonc Apr 11 '13 at 13:50
  • @simonc: 65535. ANd i have no option of using float. – sheetansh Apr 11 '13 at 14:07
  • 1
    @sheetansh what about a long? – h4lc0n Apr 11 '13 at 15:10
  • @sheetansh You could use `long int` or `long long` instead. – simonc Apr 11 '13 at 15:16
  • @h4lc0n: already tried the same, no change with the o/p. – sheetansh Apr 11 '13 at 15:22
  • @simonc: tried the same, also tried to use typecasting, no change! – sheetansh Apr 11 '13 at 15:23
  • 1
    f_temp / (4096*28) will most probably always give 0 as it's not a floating point operation. Unless f_temp is greater or equal to (4096*28), you always get 0, is this intended behavior? – h4lc0n Apr 11 '13 at 15:29
  • @h4lc0n: the value of f_temp is always less than (4096*28). actually the max value that f_temp can get from ADC is 4096 (the adc is configured for 12bits operation) Is there a way of using bitshift operators to do such calculations? would the result still be the same? – sheetansh Apr 11 '13 at 15:41

1 Answers1

3

Your 4096*28 doesn't fit into a 16-bit unsigned integer and is going to be truncated, giving you wrong results and that's why you got the warning.

But most importantly...

This

beta = (((f_temp) / (4096*28))*1000); // warning: possible loss of data.

theta = ((((beta)*(Rpre))/(1-beta))*1000);

result = (theta-c)/(m);

is equivalent to

beta = (((f_temp) / (4096*28))*1000);

theta = ((((beta)*(49900))/(1-beta))*1000);

result = (theta-986)/(4);

which in turn is equivalent to:

result = ((((((((f_temp) / (4096*28))*1000))*(49900))/(1-(((f_temp) / (4096*28))*1000)))*1000)-986)/(4)

If you plot it, you'll see there's a discontinuity at f_temp = 14336/125 ≈ 115 and the range of result is from -986/4 (≈-247) at f_temp=0 to ≈-4*109 at f_temp=115 or -∞ at f_temp=14336/125.

This suggests that either you're doing something wrong (you've got wrong formulas or constants) or you aren't giving us enough information (the valid range(s) of f_temp would be nice to have). And using integer arithmetic for these calculations is going to be problematic due to the ranges of result.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • OK! this is wat i am trying to do. – sheetansh Apr 11 '13 at 15:53
  • ADC is configured for 12 bit operation. the output i get is analog to digital conversion of the sensor signal. then using the equation derived from the op-amp circuit i get the equations for beta(beta=Nadc/((2^12)*gain);and theta (theta = (beta*Rpre)/(1-beta) ).So the theta is the resistence value of the sensor. Now pt1000 is a linear temperature sensor therefore y=m*x+c is applied. the value of m and c have been obtained from solving the equation for standard values. – sheetansh Apr 11 '13 at 16:09
  • Nadc is the actual value i get from the sensor. – sheetansh Apr 11 '13 at 16:09