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 3:0e92710a46f9, committed 2015-03-14
- Comitter:
- eencae
- Date:
- Sat Mar 14 12:57:44 2015 +0000
- Parent:
- 2:79d0d565c3af
- Commit message:
- Added DEBUG mode to check algorithm and compare to data sheet value.; Uncomment #define DEBUG in header file.; Confirmed agreement with data sheet values.
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 |
diff -r 79d0d565c3af -r 0e92710a46f9 BMP180.cpp
--- a/BMP180.cpp Fri Mar 13 18:44:57 2015 +0000
+++ b/BMP180.cpp Sat Mar 14 12:57:44 2015 +0000
@@ -39,9 +39,11 @@
char MSB = readByteFromRegister(0xF6);
char LSB = readByteFromRegister(0xF7);
// combine in 16-bit value
- int UT = (MSB << 8) | LSB;
- //int32_t UT = 27898; // test data from datasheet
- //printf("\n\nUT = %d\n",UT);
+ int UT = (MSB << 8) | LSB;
+#ifdef DEBUG
+ UT = 27898; // test data from datasheet
+ printf("****DEBUG MODE****\nUT = %d\n",UT);
+#endif
return UT;
}
@@ -55,12 +57,12 @@
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);
+ int UP = (MSB << 16 | LSB << 8 | XLSB) >> (8 - oss);
+
+#ifdef DEBUG
+ UP = 23843; // test data from datasheet
+ printf("UP = %d\n",UP);
+#endif
return UP;
}
@@ -71,7 +73,9 @@
X2 = (calibration.MC << 11) / (X1 + calibration.MD);
B5 = X1 + X2;
int32_t T = (B5 + 8) >> 4;
- //printf("X1=%d\nX2=%d\nB5=%d\n*****\nT=%d\n******\n",X1,X2,B5,T);
+#ifdef DEBUG
+ printf("****\nX1=%d\nX2=%d\nB5=%d\nT=%d\n",X1,X2,B5,T);
+#endif
return T;
}
@@ -79,34 +83,49 @@
{
// equations from data sheet
B6 = B5 - 4000;
- //printf("\n********\nB6=%d\n",B6);
X1 = (calibration.B2 * ((B6*B6) >> 12))>>11;
X2 = (calibration.AC2*B6)>>11;
X3 = X1 + X2;
B3 = (((calibration.AC1*4 + X3) << oss)+2)/4;
- //printf("X1=%d\nX2=%d\nX3=%d\nB3=%d\n",X1,X2,X3,B3);
+#ifdef DEBUG
+ printf("*****\nB6=%d\nX1=%d\nX2=%d\nX3=%d\nB3=%d\n",B6,X1,X2,X3,B3);
+#endif
X1 = (calibration.AC3*B6)>>13;
X2 = (calibration.B1*((B6*B6)>>12))>>16;
X3 = ((X1+X2)+2)/4;
B4 = (calibration.AC4*(uint32_t)(X3+32768))>>15;
- //printf("X1=%d\nX2=%d\nX3=%d\nB4=%u\n",X1,X2,X3,B4);
+#ifdef DEBUG
+ printf("X1=%d\nX2=%d\nX3=%d\nB4=%u\n",X1,X2,X3,B4);
+#endif
B7 = ((uint32_t)UP - B3)*(50000>>oss);
- //printf("B7=%u\n",B7);
+#ifdef DEBUG
+ printf("B7=%u\n",B7);
+#endif
int32_t P;
if (B7 < 0x80000000)
P = (B7*2)/B4;
else
P = (B7/B4)*2;
- //printf("P=%d\n",P);
+#ifdef DEBUG
+ printf("P=%d\n",P);
+#endif
X1 = (P>>8)*(P>>8);
- //printf("X1=%d\n",X1);
+#ifdef DEBUG
+ printf("X1=%d\n",X1);
+#endif
X1 = (X1*3038)>>16;
- //printf("X1=%d\n",X1);
+#ifdef DEBUG
+ printf("X1=%d\n",X1);
+#endif
X2 = (-7357*P)>>16;
- //printf("X2=%d\n",X2);
+#ifdef DEBUG
+ printf("X2=%d\n",X2);
+#endif
P = P + (X1+X2+3791)/16;
- //printf("P=%d\n",P);
-
+#ifdef DEBUG
+ printf("P=%d\n",P);
+#endif
+
return P;
}
@@ -120,17 +139,22 @@
if (data != 0x55) { // if correct ID not found, hang and flash error message
error();
}
-
+
readCalibrationData();
+
oss = 1; // standard power oversampling setting
- //oss = 0; // used when testing data sheet example
+
+#ifdef DEBUG
+ oss = 0; // used when testing data sheet example
+#endif
+
}
// Reads factory calibrated data
void BMP180::readCalibrationData()
{
-
+
char eeprom[22];
readBytesFromRegister(EEPROM_REG_ADD,22,eeprom);
@@ -146,13 +170,25 @@
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);
+#ifdef DEBUG
+ 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("****EXAMPLE CALIBRATION DATA****\n");
+ 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);
+#endif
}
diff -r 79d0d565c3af -r 0e92710a46f9 BMP180.h
--- a/BMP180.h Fri Mar 13 18:44:57 2015 +0000
+++ b/BMP180.h Sat Mar 14 12:57:44 2015 +0000
@@ -15,6 +15,9 @@
#define ID_REG 0xD0
#define EEPROM_REG_ADD 0xAA
+// uncomment line below to run in debug mode and compare to datasheet values
+//#define DEBUG
+
// Struct to store calibration data
typedef struct Calibration Calibration;
struct Calibration {