Hi All,
I'm hoping someone here can please help, I'm getting serious code rage!
Basically I'm trying to use a BMP085 sensor but I'm getting an unexpected result from one of the math equations. I have checked against other examples (adafruit, sparkfun) but can't see how my code is different and would be causing this result.
The calculation of X2 is causing all the grief, using the datasheet example it says X2 should be -2344 but I get -2343. I'm using the code below (I've hacked this example together to simplify it down to just this equation issue as much as possible).
Here is the code:
//BMP085 Variables
unsigned int BMP085_AC5;
unsigned int BMP085_AC6;
int BMP085_MC;
int BMP085_MD;
long BMP085_B5; //Calculated value for true temperature and pressure
void setup()
{
long UT=27898; //Uncompensated Temperature - Datasheet value
long TrueTemperature; //Stores the calculated temperature
BMP085_AC5=32757; //Change Variables to datasheet values
BMP085_AC6=23153;
BMP085_MC=-8711;
BMP085_MD=2868;
Serial.begin(9600);
TrueTemperature=BMP085_Calculate_Temperature(UT);
Serial.print("Temperature in C is (*0.1): "); Serial.println(TrueTemperature);
}
void loop()
{
}
// Calculate the true temperature from UT
long BMP085_Calculate_Temperature(long UT)
{
long X1;
long X2; //Temporary values to hold calculations
X1=(UT-(long)BMP085_AC6)*((long)BMP085_AC5)>>15;
X2=((long)BMP085_MC<<11)/(X1+(long)BMP085_MD);
BMP085_B5=X1+X2;
Serial.print("UT= "); Serial.println(UT);
Serial.print("X1 = "); Serial.println(X1, DEC);
Serial.print("X2 = "); Serial.println(X2, DEC);
Serial.print("B5 = "); Serial.println(BMP085_B5, DEC);
return ((BMP085_B5+8)>>4);
}
- When I recreate the same formula in excel to zero decimal places I get the correct result.
- I've tried testing it using direct substitution of some/all of the values and I haven't been able to pin point the issue.
- I am wondering if maybe it is some sort of rounding issue caused by the calculation and a long value type.
Any help would be greatly received.
Thanks,
Shaz
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.