Library to control a BMP180 sensor.
BMP180.h@1:608e890e88e7, 2016-08-28 (annotated)
- Committer:
- Wosser1sProductions
- Date:
- Sun Aug 28 23:28:03 2016 +0000
- Revision:
- 1:608e890e88e7
- Parent:
- 0:373de0f4d5cd
Fixed calibration values not being stored correctly.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Wosser1sProductions | 0:373de0f4d5cd | 1 | /* |
Wosser1sProductions | 0:373de0f4d5cd | 2 | @file BMP180.h |
Wosser1sProductions | 0:373de0f4d5cd | 3 | @brief Barometric Pressure and Temperature Sensor BMP180 Breakout I2C Library |
Wosser1sProductions | 0:373de0f4d5cd | 4 | */ |
Wosser1sProductions | 0:373de0f4d5cd | 5 | |
Wosser1sProductions | 0:373de0f4d5cd | 6 | #ifndef BMP180_H |
Wosser1sProductions | 0:373de0f4d5cd | 7 | #define BMP180_H |
Wosser1sProductions | 0:373de0f4d5cd | 8 | |
Wosser1sProductions | 0:373de0f4d5cd | 9 | #include "mbed.h" |
Wosser1sProductions | 0:373de0f4d5cd | 10 | |
Wosser1sProductions | 1:608e890e88e7 | 11 | /// default address is 0xEF => (0x77<<1 + 1) |
Wosser1sProductions | 0:373de0f4d5cd | 12 | #define BMP180_I2C_ADDRESS 0xEF |
Wosser1sProductions | 1:608e890e88e7 | 13 | #define DEGREES '\u00B0' |
Wosser1sProductions | 1:608e890e88e7 | 14 | #define DEGREES_CELCIUS "\u00B0C" |
Wosser1sProductions | 0:373de0f4d5cd | 15 | |
Wosser1sProductions | 0:373de0f4d5cd | 16 | // Oversampling settings |
Wosser1sProductions | 0:373de0f4d5cd | 17 | typedef enum { |
Wosser1sProductions | 0:373de0f4d5cd | 18 | ULTRA_LOW_POWER = 0, ///< 1 pressure sample : 4.5 ms delay |
Wosser1sProductions | 0:373de0f4d5cd | 19 | STANDARD = 1, ///< 2 pressure samples : 7.5 ms delay |
Wosser1sProductions | 0:373de0f4d5cd | 20 | HIGH_RESOLUTION = 2, ///< 4 pressure samples : 13.5 ms delay |
Wosser1sProductions | 0:373de0f4d5cd | 21 | ULTRA_HIGH_RESOLUTION = 3 ///< 8 pressure samples : 25.5 ms delay |
Wosser1sProductions | 0:373de0f4d5cd | 22 | } OverSamplingSetting; |
Wosser1sProductions | 0:373de0f4d5cd | 23 | |
Wosser1sProductions | 0:373de0f4d5cd | 24 | /** BMP180 class. |
Wosser1sProductions | 0:373de0f4d5cd | 25 | * Read Pressure and temperature from the BMP180 Breakout I2C sensor |
Wosser1sProductions | 0:373de0f4d5cd | 26 | * |
Wosser1sProductions | 0:373de0f4d5cd | 27 | * Example: |
Wosser1sProductions | 0:373de0f4d5cd | 28 | * @code |
Wosser1sProductions | 0:373de0f4d5cd | 29 | * #include "mbed.h" |
Wosser1sProductions | 0:373de0f4d5cd | 30 | * #include "BMP180.h" |
Wosser1sProductions | 0:373de0f4d5cd | 31 | * |
Wosser1sProductions | 0:373de0f4d5cd | 32 | * int main() { |
Wosser1sProductions | 0:373de0f4d5cd | 33 | * BMP180 bmp180(PIN_SDA, PIN_SCL); |
Wosser1sProductions | 0:373de0f4d5cd | 34 | * float pressure, temperature; |
Wosser1sProductions | 0:373de0f4d5cd | 35 | * |
Wosser1sProductions | 0:373de0f4d5cd | 36 | * // bmp180.Initialize(); // no altitude compensation and normal oversampling |
Wosser1sProductions | 0:373de0f4d5cd | 37 | * bmp180.Initialize(64, BMP180_OSS_ULTRA_LOW_POWER); // 64m altitude compensation and low power oversampling |
Wosser1sProductions | 0:373de0f4d5cd | 38 | * |
Wosser1sProductions | 0:373de0f4d5cd | 39 | * while(1) { |
Wosser1sProductions | 0:373de0f4d5cd | 40 | * if (bmp180.ReadData(&pressure, &temperature)) |
Wosser1sProductions | 0:373de0f4d5cd | 41 | * printf("Pressure(hPa): %8.2f \t Temperature(C): %8.2f\n", pressure, temperature); |
Wosser1sProductions | 0:373de0f4d5cd | 42 | * wait(1); |
Wosser1sProductions | 0:373de0f4d5cd | 43 | * } |
Wosser1sProductions | 0:373de0f4d5cd | 44 | * } |
Wosser1sProductions | 0:373de0f4d5cd | 45 | * @endcode |
Wosser1sProductions | 0:373de0f4d5cd | 46 | */ |
Wosser1sProductions | 0:373de0f4d5cd | 47 | class BMP180 { |
Wosser1sProductions | 0:373de0f4d5cd | 48 | public: |
Wosser1sProductions | 0:373de0f4d5cd | 49 | /** Create a BMP180 instance |
Wosser1sProductions | 0:373de0f4d5cd | 50 | * @param i2c object |
Wosser1sProductions | 0:373de0f4d5cd | 51 | */ |
Wosser1sProductions | 0:373de0f4d5cd | 52 | BMP180(I2C& i2c); |
Wosser1sProductions | 0:373de0f4d5cd | 53 | |
Wosser1sProductions | 0:373de0f4d5cd | 54 | /** Initialization: set member values and read BMP180 calibration parameters |
Wosser1sProductions | 0:373de0f4d5cd | 55 | * @param altitude (in meter) |
Wosser1sProductions | 0:373de0f4d5cd | 56 | * @param overSamplingSetting |
Wosser1sProductions | 0:373de0f4d5cd | 57 | * @returns |
Wosser1sProductions | 0:373de0f4d5cd | 58 | * 1 on success, |
Wosser1sProductions | 0:373de0f4d5cd | 59 | * 0 on error |
Wosser1sProductions | 0:373de0f4d5cd | 60 | */ |
Wosser1sProductions | 0:373de0f4d5cd | 61 | int initialize(float altitude = 0.F, OverSamplingSetting oss = STANDARD); |
Wosser1sProductions | 0:373de0f4d5cd | 62 | |
Wosser1sProductions | 0:373de0f4d5cd | 63 | /** Read pressure and temperature from the BMP180. |
Wosser1sProductions | 0:373de0f4d5cd | 64 | * @param pressure (hPa) |
Wosser1sProductions | 0:373de0f4d5cd | 65 | * @param temperature (C) |
Wosser1sProductions | 0:373de0f4d5cd | 66 | * @returns |
Wosser1sProductions | 0:373de0f4d5cd | 67 | * 1 on success, |
Wosser1sProductions | 0:373de0f4d5cd | 68 | * 0 on error |
Wosser1sProductions | 0:373de0f4d5cd | 69 | */ |
Wosser1sProductions | 1:608e890e88e7 | 70 | int readData(float& pTemperature, float& pPressure); |
Wosser1sProductions | 0:373de0f4d5cd | 71 | |
Wosser1sProductions | 0:373de0f4d5cd | 72 | protected: |
Wosser1sProductions | 0:373de0f4d5cd | 73 | /** Perform temperature measurement |
Wosser1sProductions | 0:373de0f4d5cd | 74 | * |
Wosser1sProductions | 0:373de0f4d5cd | 75 | * @returns |
Wosser1sProductions | 0:373de0f4d5cd | 76 | * temperature (C) |
Wosser1sProductions | 0:373de0f4d5cd | 77 | */ |
Wosser1sProductions | 0:373de0f4d5cd | 78 | int ReadRawTemperature(long* pUt); |
Wosser1sProductions | 0:373de0f4d5cd | 79 | |
Wosser1sProductions | 0:373de0f4d5cd | 80 | /** Perform pressure measurement |
Wosser1sProductions | 0:373de0f4d5cd | 81 | * |
Wosser1sProductions | 0:373de0f4d5cd | 82 | * @returns |
Wosser1sProductions | 0:373de0f4d5cd | 83 | * pressure (mbar) |
Wosser1sProductions | 0:373de0f4d5cd | 84 | */ |
Wosser1sProductions | 0:373de0f4d5cd | 85 | int ReadRawPressure(long* pUp); |
Wosser1sProductions | 0:373de0f4d5cd | 86 | |
Wosser1sProductions | 0:373de0f4d5cd | 87 | /** Calculation of the temperature from the digital output |
Wosser1sProductions | 0:373de0f4d5cd | 88 | */ |
Wosser1sProductions | 0:373de0f4d5cd | 89 | float TrueTemperature(long ut); |
Wosser1sProductions | 0:373de0f4d5cd | 90 | |
Wosser1sProductions | 0:373de0f4d5cd | 91 | /** Calculation of the pressure from the digital output |
Wosser1sProductions | 0:373de0f4d5cd | 92 | */ |
Wosser1sProductions | 0:373de0f4d5cd | 93 | float TruePressure(long up); |
Wosser1sProductions | 0:373de0f4d5cd | 94 | |
Wosser1sProductions | 0:373de0f4d5cd | 95 | OverSamplingSetting m_oss; |
Wosser1sProductions | 0:373de0f4d5cd | 96 | float m_altitude; |
Wosser1sProductions | 0:373de0f4d5cd | 97 | |
Wosser1sProductions | 0:373de0f4d5cd | 98 | I2C m_i2c; |
Wosser1sProductions | 0:373de0f4d5cd | 99 | char m_data[2]; |
Wosser1sProductions | 0:373de0f4d5cd | 100 | |
Wosser1sProductions | 0:373de0f4d5cd | 101 | //short mb; // Not used? |
Wosser1sProductions | 1:608e890e88e7 | 102 | uint16_t ac4, ac5, ac6; |
Wosser1sProductions | 1:608e890e88e7 | 103 | int16_t ac1, ac2, ac3, b1, b2, b5, md; |
Wosser1sProductions | 1:608e890e88e7 | 104 | long x1, x2, x3, b3, b6, mc; |
Wosser1sProductions | 0:373de0f4d5cd | 105 | unsigned long b4, b7; |
Wosser1sProductions | 0:373de0f4d5cd | 106 | }; |
Wosser1sProductions | 0:373de0f4d5cd | 107 | |
Wosser1sProductions | 0:373de0f4d5cd | 108 | #endif //BMP180_H |