adafruit / Adafruit-BMP085-Library

A powerful but easy to use BMP085/BMP180 Arduino library
http://www.adafruit.com/products/1603
233 stars 194 forks source link

Temperature calculation #8

Closed sphh closed 10 years ago

sphh commented 11 years ago

In issue 5 (and the resulting commit) the calculation for the temperature was changed from X1 = ((UT - (int32_t)ac6) * (int32_t)ac5) >> 15; to X1 = ((UT - (int32_t)ac6) * (int32_t)ac5) / pow(2,15); I think (but may be wrong) that ">> 15" is faster than "/pow(2,15)" and gives the same result. So I would propose to use ">> 15" and "<< 11" (the other change made) again.

I added a new private function to get rid of the redundant code:

int32_t Adafruit_BMP085::readB5(void) { int32_t UT, X1, X2, B5; // following ds convention

UT = readRawTemperature();

if BMP085_DEBUG == 1

// use datasheet numbers! UT = 27898; ac6 = 23153; ac5 = 32757; mc = -8711; md = 2868;

endif

// step 1 X1 = (UT - (int32_t)ac6) * ((int32_t)ac5) >> 15; X2 = ((int32_t)mc << 11) / (X1+(int32_t)md); B5 = X1 + X2;

if BMP085_DEBUG == 1

Serial.print("X1 = "); Serial.println(X1); Serial.print("X2 = "); Serial.println(X2); Serial.print("B5 = "); Serial.println(B5);

endif

return B5; }

Now the new readTemperature() becomes: float Adafruit_BMP085::readTemperature(void) { int32_t B5; // following ds convention float temp;

// step 1 B5 = readB5(); temp = (B5+8) >> 4; temp /= 10;

return temp; }

and readPressure(): int32_t Adafruit_BMP085::readPressure(void) { int32_t UT, UP, B3, B5, B6, X1, X2, X3, p; uint32_t B4, B7;

B5 = readB5(); UP = readRawPressure();

if BMP085_DEBUG == 1

...

I hope you like the change.

Stephan

tdicola commented 10 years ago

Thanks for making the suggestion and apologies it wasn't looked at sooner. I just integrated a fix to use bit shifting and put the B5 computation into a common function. Thanks again for the suggestion!