BMP180 Pressure/Temperature Sensor library
Dependents: GliderFuncTest1 DropTest
Fork of BMP180 by
BMP180.h@2:1dad52218be5, 2017-04-09 (annotated)
- Committer:
- chasefarmer2808
- Date:
- Sun Apr 09 05:05:11 2017 +0000
- Revision:
- 2:1dad52218be5
- Parent:
- 1:072073c79cfd
- Child:
- 3:ae101ea7bd90
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
spiridion | 0:9a0671b6009f | 1 | /* |
spiridion | 0:9a0671b6009f | 2 | @file BMP180.h |
spiridion | 0:9a0671b6009f | 3 | |
spiridion | 0:9a0671b6009f | 4 | @brief Barometric Pressure and Temperature Sensor BMP180 Breakout I2C Library |
spiridion | 0:9a0671b6009f | 5 | |
spiridion | 0:9a0671b6009f | 6 | @Author spiridion (http://mailme.spiridion.net) |
spiridion | 0:9a0671b6009f | 7 | |
spiridion | 0:9a0671b6009f | 8 | Tested on LPC1768 and FRDM-KL25Z |
spiridion | 0:9a0671b6009f | 9 | |
spiridion | 0:9a0671b6009f | 10 | Copyright (c) 2014 spiridion |
spiridion | 0:9a0671b6009f | 11 | Released under the MIT License (see http://mbed.org/license/mit) |
spiridion | 0:9a0671b6009f | 12 | |
spiridion | 0:9a0671b6009f | 13 | Documentation regarding the BMP180 can be found here: |
spiridion | 1:072073c79cfd | 14 | http://mbed.org/media/uploads/spiridion/bst-bmp180-ds000-09.pdf |
spiridion | 0:9a0671b6009f | 15 | */ |
spiridion | 0:9a0671b6009f | 16 | |
spiridion | 0:9a0671b6009f | 17 | #ifndef BMP180_H |
spiridion | 0:9a0671b6009f | 18 | #define BMP180_H |
spiridion | 0:9a0671b6009f | 19 | |
spiridion | 0:9a0671b6009f | 20 | #include "mbed.h" |
spiridion | 0:9a0671b6009f | 21 | |
spiridion | 0:9a0671b6009f | 22 | /// default address is 0xEF |
spiridion | 0:9a0671b6009f | 23 | #define BMP180_I2C_ADDRESS 0xEF |
spiridion | 0:9a0671b6009f | 24 | |
spiridion | 0:9a0671b6009f | 25 | // Oversampling settings |
spiridion | 0:9a0671b6009f | 26 | #define BMP180_OSS_ULTRA_LOW_POWER 0 // 1 sample and 4.5ms for conversion |
spiridion | 0:9a0671b6009f | 27 | #define BMP180_OSS_NORMAL 1 // 2 samples and 7.5ms for conversion |
spiridion | 0:9a0671b6009f | 28 | #define BMP180_OSS_HIGH_RESOLUTION 2 // 4 samples and 13.5ms for conversion |
spiridion | 0:9a0671b6009f | 29 | #define BMP180_OSS_ULTRA_HIGH_RESOLUTION 3 // 8 samples and 25.5ms for conversion |
spiridion | 0:9a0671b6009f | 30 | |
spiridion | 0:9a0671b6009f | 31 | #define UNSET_BMP180_PRESSURE_VALUE 0.F |
spiridion | 0:9a0671b6009f | 32 | #define UNSET_BMP180_TEMPERATURE_VALUE -273.15F // absolute zero |
spiridion | 0:9a0671b6009f | 33 | |
spiridion | 0:9a0671b6009f | 34 | /** BMP180 class. |
spiridion | 0:9a0671b6009f | 35 | * Read Pressure and temperature from the BMP180 Breakout I2C sensor |
spiridion | 0:9a0671b6009f | 36 | * |
spiridion | 0:9a0671b6009f | 37 | * Example: |
spiridion | 0:9a0671b6009f | 38 | * @code |
spiridion | 0:9a0671b6009f | 39 | * #include "mbed.h" |
spiridion | 0:9a0671b6009f | 40 | * #include "BMP180.h" |
spiridion | 0:9a0671b6009f | 41 | * |
spiridion | 0:9a0671b6009f | 42 | * #if defined(TARGET_LPC1768) |
spiridion | 0:9a0671b6009f | 43 | * #define PIN_SDA p9 |
spiridion | 0:9a0671b6009f | 44 | * #define PIN_SCL p10 |
spiridion | 0:9a0671b6009f | 45 | * #elif defined(TARGET_KL25Z) // watch out for the PTE0/PTE1 mixed up in the KL25Z doc |
spiridion | 0:9a0671b6009f | 46 | * #define PIN_SDA PTE0 |
spiridion | 0:9a0671b6009f | 47 | * #define PIN_SCL PTE1 |
spiridion | 0:9a0671b6009f | 48 | * #endif |
spiridion | 0:9a0671b6009f | 49 | * |
spiridion | 0:9a0671b6009f | 50 | * int main() |
spiridion | 0:9a0671b6009f | 51 | * { |
spiridion | 0:9a0671b6009f | 52 | * BMP180 bmp180(PIN_SDA, PIN_SCL); |
spiridion | 0:9a0671b6009f | 53 | * float pressure, temperature; |
spiridion | 0:9a0671b6009f | 54 | * |
spiridion | 1:072073c79cfd | 55 | * // bmp180.Initialize(); // no altitude compensation and normal oversampling |
spiridion | 1:072073c79cfd | 56 | * bmp180.Initialize(64, BMP180_OSS_ULTRA_LOW_POWER); // 64m altitude compensation and low power oversampling |
spiridion | 0:9a0671b6009f | 57 | * |
spiridion | 1:072073c79cfd | 58 | * while(1) |
spiridion | 1:072073c79cfd | 59 | * { |
spiridion | 0:9a0671b6009f | 60 | * if (bmp180.ReadData(&pressure, &temperature)) |
spiridion | 0:9a0671b6009f | 61 | * printf("Pressure(hPa): %8.2f \t Temperature(C): %8.2f\n", pressure, temperature); |
spiridion | 0:9a0671b6009f | 62 | * wait(1); |
spiridion | 0:9a0671b6009f | 63 | * } |
spiridion | 0:9a0671b6009f | 64 | * } |
spiridion | 0:9a0671b6009f | 65 | * @endcode |
spiridion | 0:9a0671b6009f | 66 | */ |
spiridion | 0:9a0671b6009f | 67 | class BMP180 |
spiridion | 0:9a0671b6009f | 68 | { |
spiridion | 0:9a0671b6009f | 69 | |
spiridion | 0:9a0671b6009f | 70 | public: |
spiridion | 0:9a0671b6009f | 71 | |
spiridion | 0:9a0671b6009f | 72 | /** Create a BMP180 instance |
spiridion | 0:9a0671b6009f | 73 | * @param sda pin |
spiridion | 0:9a0671b6009f | 74 | * @param scl pin |
spiridion | 0:9a0671b6009f | 75 | * @param address: I2C slave address |
spiridion | 0:9a0671b6009f | 76 | */ |
spiridion | 0:9a0671b6009f | 77 | BMP180(PinName sda, PinName scl, int address = BMP180_I2C_ADDRESS); |
spiridion | 0:9a0671b6009f | 78 | |
spiridion | 0:9a0671b6009f | 79 | /** Create a BMP180 instance |
spiridion | 0:9a0671b6009f | 80 | * @param i2c object |
spiridion | 0:9a0671b6009f | 81 | * @param address: I2C slave address |
spiridion | 0:9a0671b6009f | 82 | */ |
spiridion | 0:9a0671b6009f | 83 | BMP180(I2C& i2c, int address = BMP180_I2C_ADDRESS); |
spiridion | 0:9a0671b6009f | 84 | |
spiridion | 0:9a0671b6009f | 85 | /** Initialization: set member values and read BMP180 calibration parameters |
spiridion | 0:9a0671b6009f | 86 | * @param altitude (in meter) |
spiridion | 0:9a0671b6009f | 87 | * @param overSamplingSetting |
spiridion | 0:9a0671b6009f | 88 | */ |
spiridion | 0:9a0671b6009f | 89 | int Initialize(float altitude = 0.F, int overSamplingSetting = BMP180_OSS_NORMAL); |
spiridion | 0:9a0671b6009f | 90 | |
spiridion | 0:9a0671b6009f | 91 | /** Read pressure and temperature from the BMP180. |
spiridion | 0:9a0671b6009f | 92 | * @param pressure (hPa) |
spiridion | 0:9a0671b6009f | 93 | * @param temperature (C) |
spiridion | 0:9a0671b6009f | 94 | * @returns |
spiridion | 0:9a0671b6009f | 95 | * 1 on success, |
spiridion | 0:9a0671b6009f | 96 | * 0 on error |
spiridion | 0:9a0671b6009f | 97 | */ |
chasefarmer2808 | 2:1dad52218be5 | 98 | int ReadData(float* pTemperature = NULL, float* pPressure = NULL, float* pAltitude = NULL); |
chasefarmer2808 | 2:1dad52218be5 | 99 | |
chasefarmer2808 | 2:1dad52218be5 | 100 | float getAltitude(float* p); |
spiridion | 0:9a0671b6009f | 101 | |
spiridion | 0:9a0671b6009f | 102 | /** Get temperature from a previous measurement |
spiridion | 0:9a0671b6009f | 103 | * |
spiridion | 0:9a0671b6009f | 104 | * @returns |
spiridion | 0:9a0671b6009f | 105 | * temperature (C) |
spiridion | 0:9a0671b6009f | 106 | */ |
spiridion | 0:9a0671b6009f | 107 | float GetTemperature() {return m_temperature;}; |
spiridion | 0:9a0671b6009f | 108 | |
spiridion | 0:9a0671b6009f | 109 | /** Get pressure from a previous measurement |
spiridion | 0:9a0671b6009f | 110 | * |
spiridion | 0:9a0671b6009f | 111 | * @returns |
spiridion | 0:9a0671b6009f | 112 | * pressure (hPa) |
spiridion | 0:9a0671b6009f | 113 | */ |
spiridion | 0:9a0671b6009f | 114 | float GetPressure() {return m_pressure;}; |
spiridion | 0:9a0671b6009f | 115 | |
spiridion | 0:9a0671b6009f | 116 | protected: |
spiridion | 0:9a0671b6009f | 117 | |
spiridion | 0:9a0671b6009f | 118 | /** Perform temperature measurement |
spiridion | 0:9a0671b6009f | 119 | * |
spiridion | 0:9a0671b6009f | 120 | * @returns |
spiridion | 0:9a0671b6009f | 121 | * temperature (C) |
spiridion | 0:9a0671b6009f | 122 | */ |
spiridion | 0:9a0671b6009f | 123 | int ReadRawTemperature(long* pUt); |
spiridion | 0:9a0671b6009f | 124 | |
spiridion | 0:9a0671b6009f | 125 | /** Perform pressure measurement |
spiridion | 0:9a0671b6009f | 126 | * |
spiridion | 0:9a0671b6009f | 127 | * @returns |
spiridion | 0:9a0671b6009f | 128 | * temperature (C) |
spiridion | 0:9a0671b6009f | 129 | */ |
spiridion | 0:9a0671b6009f | 130 | int ReadRawPressure(long* pUp); |
spiridion | 0:9a0671b6009f | 131 | |
spiridion | 0:9a0671b6009f | 132 | /** Calculation of the temperature from the digital output |
spiridion | 0:9a0671b6009f | 133 | */ |
spiridion | 0:9a0671b6009f | 134 | float TrueTemperature(long ut); |
spiridion | 0:9a0671b6009f | 135 | |
spiridion | 0:9a0671b6009f | 136 | /** Calculation of the pressure from the digital output |
spiridion | 0:9a0671b6009f | 137 | */ |
spiridion | 0:9a0671b6009f | 138 | float TruePressure(long up); |
spiridion | 0:9a0671b6009f | 139 | |
spiridion | 0:9a0671b6009f | 140 | int m_oss; |
spiridion | 0:9a0671b6009f | 141 | float m_temperature; |
spiridion | 0:9a0671b6009f | 142 | float m_pressure; |
spiridion | 0:9a0671b6009f | 143 | float m_altitude; |
spiridion | 0:9a0671b6009f | 144 | |
spiridion | 0:9a0671b6009f | 145 | I2C m_i2c; |
spiridion | 0:9a0671b6009f | 146 | int m_addr; |
spiridion | 0:9a0671b6009f | 147 | char m_data[4]; |
spiridion | 0:9a0671b6009f | 148 | |
spiridion | 0:9a0671b6009f | 149 | short ac1, ac2, ac3; |
spiridion | 0:9a0671b6009f | 150 | unsigned short ac4, ac5, ac6; |
spiridion | 0:9a0671b6009f | 151 | short b1, b2; |
spiridion | 0:9a0671b6009f | 152 | short mb, mc, md; |
spiridion | 0:9a0671b6009f | 153 | long x1, x2, x3, b3, b5, b6; |
spiridion | 0:9a0671b6009f | 154 | unsigned long b4, b7; |
spiridion | 0:9a0671b6009f | 155 | |
spiridion | 0:9a0671b6009f | 156 | }; |
spiridion | 0:9a0671b6009f | 157 | |
spiridion | 0:9a0671b6009f | 158 | #endif |