Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: WeatherStation WeatherStation1 ProjectD Weather_Station ... more
Revision 2:79d0d565c3af, committed 2015-03-13
- Comitter:
- eencae
- Date:
- Fri Mar 13 18:44:57 2015 +0000
- Parent:
- 1:e6c317e0f3a4
- Child:
- 3:0e92710a46f9
- Commit message:
- -Added in test data from data sheet (algorithm matches solution presented in data sheet).; -Swapped int for int32_t to be sure of size.; -Replaced dividing by powers of 2 by bit-shifting.;
Changed in this revision
| BMP180.cpp | Show annotated file Show diff for this revision Revisions of this file |
| BMP180.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/BMP180.cpp Sun Mar 08 16:14:36 2015 +0000
+++ b/BMP180.cpp Fri Mar 13 18:44:57 2015 +0000
@@ -17,12 +17,12 @@
Measurement BMP180::readValues()
{
// algorithm for taking measurement is taken from datasheet
- int UT = readUncompensatedTemperatureValue();
- int UP = readUncompensatedPressureValue();
+ int32_t UT = readUncompensatedTemperatureValue();
+ int32_t UP = readUncompensatedPressureValue();
// once you have the uncompensated T and P, you can calculate the true T and P
// using the equations from the datasheet
- int T = calcTrueTemperature(UT);
- int P = calcTruePressure(UP);
+ int32_t T = calcTrueTemperature(UT);
+ int32_t P = calcTruePressure(UP);
Measurement measurement;
measurement.temperature = T*0.1; // scaled by 0.1 C
@@ -31,7 +31,7 @@
return measurement;
}
-int BMP180::readUncompensatedTemperatureValue()
+int32_t BMP180::readUncompensatedTemperatureValue()
{
// from algorithm in datasheet - p15
sendByteToRegister(0x2E,0xF4);
@@ -39,12 +39,13 @@
char MSB = readByteFromRegister(0xF6);
char LSB = readByteFromRegister(0xF7);
// combine in 16-bit value
- int UT = (MSB << 8) | LSB;
+ int UT = (MSB << 8) | LSB;
+ //int32_t UT = 27898; // test data from datasheet
+ //printf("\n\nUT = %d\n",UT);
return UT;
-
}
-int BMP180::readUncompensatedPressureValue()
+int32_t BMP180::readUncompensatedPressureValue()
{
// from datasheet
char byte = 0x34 + (oss << 6);
@@ -53,44 +54,59 @@
char MSB = readByteFromRegister(0xF6);
char LSB = readByteFromRegister(0xF7);
+ char XLSB = readByteFromRegister(0xF7);
// just do 16-bit, not 19-bit
+ //int UP = (MSB << 16 | LSB << 8 | XLSB) >> (8 - oss);
int UP = (MSB << 16 | LSB << 8) >> (8 - oss);
+
+ //int32_t UP = 23843; // test data from datasheet
+ //printf("UP = %d\n",UP);
return UP;
}
-int BMP180::calcTrueTemperature(int UT)
+int32_t BMP180::calcTrueTemperature(int32_t UT)
{
// equations from data sheet
- X1 = (UT - calibration.AC6) * calibration.AC5/32768;
- X2 = calibration.MC * 2048 / (X1 + calibration.MD);
+ X1 = ((UT - calibration.AC6)*calibration.AC5) >> 15;
+ X2 = (calibration.MC << 11) / (X1 + calibration.MD);
B5 = X1 + X2;
- int T = (B5 + 8)/16;
+ int32_t T = (B5 + 8) >> 4;
+ //printf("X1=%d\nX2=%d\nB5=%d\n*****\nT=%d\n******\n",X1,X2,B5,T);
return T;
}
-int BMP180::calcTruePressure(int UP)
+int32_t BMP180::calcTruePressure(int32_t UP)
{
// equations from data sheet
B6 = B5 - 4000;
- X1 = (calibration.B2 * (B6*B6/4096))/2048;
- X2 = calibration.AC2*B6/2048;
+ //printf("\n********\nB6=%d\n",B6);
+ X1 = (calibration.B2 * ((B6*B6) >> 12))>>11;
+ X2 = (calibration.AC2*B6)>>11;
X3 = X1 + X2;
- B3 = (((4*calibration.AC1 + X3) << oss)+2)/4;
- X1 = calibration.AC3*B6/8192;
- X2 = (calibration.B1*(B6*B6/4096))/65536;
+ B3 = (((calibration.AC1*4 + X3) << oss)+2)/4;
+ //printf("X1=%d\nX2=%d\nX3=%d\nB3=%d\n",X1,X2,X3,B3);
+ X1 = (calibration.AC3*B6)>>13;
+ X2 = (calibration.B1*((B6*B6)>>12))>>16;
X3 = ((X1+X2)+2)/4;
- B4 = calibration.AC4*(unsigned int)(X3+32768)/32768;
- B7 = ((unsigned int)UP - B3)*(50000>>oss);
- int P;
+ B4 = (calibration.AC4*(uint32_t)(X3+32768))>>15;
+ //printf("X1=%d\nX2=%d\nX3=%d\nB4=%u\n",X1,X2,X3,B4);
+ B7 = ((uint32_t)UP - B3)*(50000>>oss);
+ //printf("B7=%u\n",B7);
+ int32_t P;
if (B7 < 0x80000000)
P = (B7*2)/B4;
else
P = (B7/B4)*2;
- X1 = (P/256)*(P/256);
- X1 = (X1*3038)/65536;
- X2 = (-7357*P)/65536;
+ //printf("P=%d\n",P);
+ X1 = (P>>8)*(P>>8);
+ //printf("X1=%d\n",X1);
+ X1 = (X1*3038)>>16;
+ //printf("X1=%d\n",X1);
+ X2 = (-7357*P)>>16;
+ //printf("X2=%d\n",X2);
P = P + (X1+X2+3791)/16;
-
+ //printf("P=%d\n",P);
+
return P;
}
@@ -105,15 +121,18 @@
error();
}
- readCalibrationData();
- oss = 1; // standard oversampling setting
+ readCalibrationData();
+ oss = 1; // standard power oversampling setting
+ //oss = 0; // used when testing data sheet example
}
// Reads factory calibrated data
void BMP180::readCalibrationData()
{
+
char eeprom[22];
+
readBytesFromRegister(EEPROM_REG_ADD,22,eeprom);
// store calibration data in structure
calibration.AC1 = (int16_t) (eeprom[0] << 8) | eeprom[1];
@@ -127,6 +146,13 @@
calibration.MB = (int16_t) (eeprom[16] << 8) | eeprom[17];
calibration.MC = (int16_t) (eeprom[18] << 8) | eeprom[19];
calibration.MD = (int16_t) (eeprom[20] << 8) | eeprom[21];
+
+ // test data from data sheet
+ //calibration.AC1 = 408;calibration.AC2 = -72;calibration.AC3 = -14383;calibration.AC4 = 32741;calibration.AC5 = 32757;
+ //calibration.AC6 = 23153;calibration.B1 = 6190;calibration.B2 = 4;calibration.MB = -32768;calibration.MC = -8711;calibration.MD = 2868;
+ //printf("AC1=%d\nAC2=%d\nAC3=%d\nAC4=%u\nAC5=%u\nAC6=%u\nB1=%d\nB2=%d\nMB=%d\nMC=%d\nMD=%d\n",
+ // calibration.AC1,calibration.AC2,calibration.AC3,calibration.AC4,calibration.AC5,calibration.AC6,
+ // calibration.B1,calibration.B2,calibration.MB,calibration.MC,calibration.MD);
}
--- a/BMP180.h Sun Mar 08 16:14:36 2015 +0000
+++ b/BMP180.h Fri Mar 13 18:44:57 2015 +0000
@@ -106,10 +106,10 @@
private:
void error();
- int readUncompensatedTemperatureValue();
- int readUncompensatedPressureValue();
- int calcTrueTemperature(int UT);
- int calcTruePressure(int UP);
+ int32_t readUncompensatedTemperatureValue();
+ int32_t readUncompensatedPressureValue();
+ int32_t calcTrueTemperature(int32_t UT);
+ int32_t calcTruePressure(int32_t UP);
void sendByteToRegister(char byte,char reg);
char readByteFromRegister(char reg);
void readBytesFromRegister(char reg,int numberOfBytes,char bytes[]);
@@ -121,8 +121,8 @@
BusOut* leds;
Calibration calibration; // variable to store calibration data
// variables for calculation
- int X1,X2,X3,B3,B5,B6;
- unsigned int B4,B7;
+ int32_t X1,X2,X3,B3,B5,B6;
+ uint32_t B4,B7;
int oss; // oversampling setting
};