BMP180 Pressure/Temperature Sensor library

Dependents:   GliderFuncTest1 DropTest

Fork of BMP180 by Spiridion Mbed

Committer:
chasefarmer2808
Date:
Mon Apr 24 21:34:02 2017 +0000
Revision:
3:ae101ea7bd90
Parent:
2:1dad52218be5
accurat;

Who changed what in which revision?

UserRevisionLine numberNew 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
chasefarmer2808 3:ae101ea7bd90 33 #define SENSORS_PRESSURE_SEALEVELHPA (1013.25F) /**< Average sea level pressure is 1013.25 hPa */
spiridion 0:9a0671b6009f 34
spiridion 0:9a0671b6009f 35 /** BMP180 class.
spiridion 0:9a0671b6009f 36 * Read Pressure and temperature from the BMP180 Breakout I2C sensor
spiridion 0:9a0671b6009f 37 *
spiridion 0:9a0671b6009f 38 * Example:
spiridion 0:9a0671b6009f 39 * @code
spiridion 0:9a0671b6009f 40 * #include "mbed.h"
spiridion 0:9a0671b6009f 41 * #include "BMP180.h"
spiridion 0:9a0671b6009f 42 *
spiridion 0:9a0671b6009f 43 * #if defined(TARGET_LPC1768)
spiridion 0:9a0671b6009f 44 * #define PIN_SDA p9
spiridion 0:9a0671b6009f 45 * #define PIN_SCL p10
spiridion 0:9a0671b6009f 46 * #elif defined(TARGET_KL25Z) // watch out for the PTE0/PTE1 mixed up in the KL25Z doc
spiridion 0:9a0671b6009f 47 * #define PIN_SDA PTE0
spiridion 0:9a0671b6009f 48 * #define PIN_SCL PTE1
spiridion 0:9a0671b6009f 49 * #endif
spiridion 0:9a0671b6009f 50 *
spiridion 0:9a0671b6009f 51 * int main()
spiridion 0:9a0671b6009f 52 * {
spiridion 0:9a0671b6009f 53 * BMP180 bmp180(PIN_SDA, PIN_SCL);
spiridion 0:9a0671b6009f 54 * float pressure, temperature;
spiridion 0:9a0671b6009f 55 *
spiridion 1:072073c79cfd 56 * // bmp180.Initialize(); // no altitude compensation and normal oversampling
spiridion 1:072073c79cfd 57 * bmp180.Initialize(64, BMP180_OSS_ULTRA_LOW_POWER); // 64m altitude compensation and low power oversampling
spiridion 0:9a0671b6009f 58 *
spiridion 1:072073c79cfd 59 * while(1)
spiridion 1:072073c79cfd 60 * {
spiridion 0:9a0671b6009f 61 * if (bmp180.ReadData(&pressure, &temperature))
spiridion 0:9a0671b6009f 62 * printf("Pressure(hPa): %8.2f \t Temperature(C): %8.2f\n", pressure, temperature);
spiridion 0:9a0671b6009f 63 * wait(1);
spiridion 0:9a0671b6009f 64 * }
spiridion 0:9a0671b6009f 65 * }
spiridion 0:9a0671b6009f 66 * @endcode
spiridion 0:9a0671b6009f 67 */
spiridion 0:9a0671b6009f 68 class BMP180
spiridion 0:9a0671b6009f 69 {
spiridion 0:9a0671b6009f 70
spiridion 0:9a0671b6009f 71 public:
spiridion 0:9a0671b6009f 72
spiridion 0:9a0671b6009f 73 /** Create a BMP180 instance
spiridion 0:9a0671b6009f 74 * @param sda pin
spiridion 0:9a0671b6009f 75 * @param scl pin
spiridion 0:9a0671b6009f 76 * @param address: I2C slave address
spiridion 0:9a0671b6009f 77 */
spiridion 0:9a0671b6009f 78 BMP180(PinName sda, PinName scl, int address = BMP180_I2C_ADDRESS);
spiridion 0:9a0671b6009f 79
spiridion 0:9a0671b6009f 80 /** Create a BMP180 instance
spiridion 0:9a0671b6009f 81 * @param i2c object
spiridion 0:9a0671b6009f 82 * @param address: I2C slave address
spiridion 0:9a0671b6009f 83 */
spiridion 0:9a0671b6009f 84 BMP180(I2C& i2c, int address = BMP180_I2C_ADDRESS);
spiridion 0:9a0671b6009f 85
spiridion 0:9a0671b6009f 86 /** Initialization: set member values and read BMP180 calibration parameters
spiridion 0:9a0671b6009f 87 * @param altitude (in meter)
spiridion 0:9a0671b6009f 88 * @param overSamplingSetting
spiridion 0:9a0671b6009f 89 */
spiridion 0:9a0671b6009f 90 int Initialize(float altitude = 0.F, int overSamplingSetting = BMP180_OSS_NORMAL);
spiridion 0:9a0671b6009f 91
spiridion 0:9a0671b6009f 92 /** Read pressure and temperature from the BMP180.
spiridion 0:9a0671b6009f 93 * @param pressure (hPa)
spiridion 0:9a0671b6009f 94 * @param temperature (C)
spiridion 0:9a0671b6009f 95 * @returns
spiridion 0:9a0671b6009f 96 * 1 on success,
spiridion 0:9a0671b6009f 97 * 0 on error
spiridion 0:9a0671b6009f 98 */
chasefarmer2808 2:1dad52218be5 99 int ReadData(float* pTemperature = NULL, float* pPressure = NULL, float* pAltitude = NULL);
chasefarmer2808 2:1dad52218be5 100
chasefarmer2808 2:1dad52218be5 101 float getAltitude(float* p);
spiridion 0:9a0671b6009f 102
spiridion 0:9a0671b6009f 103 /** Get temperature from a previous measurement
spiridion 0:9a0671b6009f 104 *
spiridion 0:9a0671b6009f 105 * @returns
spiridion 0:9a0671b6009f 106 * temperature (C)
spiridion 0:9a0671b6009f 107 */
spiridion 0:9a0671b6009f 108 float GetTemperature() {return m_temperature;};
spiridion 0:9a0671b6009f 109
spiridion 0:9a0671b6009f 110 /** Get pressure from a previous measurement
spiridion 0:9a0671b6009f 111 *
spiridion 0:9a0671b6009f 112 * @returns
spiridion 0:9a0671b6009f 113 * pressure (hPa)
spiridion 0:9a0671b6009f 114 */
spiridion 0:9a0671b6009f 115 float GetPressure() {return m_pressure;};
spiridion 0:9a0671b6009f 116
spiridion 0:9a0671b6009f 117 protected:
spiridion 0:9a0671b6009f 118
spiridion 0:9a0671b6009f 119 /** Perform temperature measurement
spiridion 0:9a0671b6009f 120 *
spiridion 0:9a0671b6009f 121 * @returns
spiridion 0:9a0671b6009f 122 * temperature (C)
spiridion 0:9a0671b6009f 123 */
spiridion 0:9a0671b6009f 124 int ReadRawTemperature(long* pUt);
spiridion 0:9a0671b6009f 125
spiridion 0:9a0671b6009f 126 /** Perform pressure measurement
spiridion 0:9a0671b6009f 127 *
spiridion 0:9a0671b6009f 128 * @returns
spiridion 0:9a0671b6009f 129 * temperature (C)
spiridion 0:9a0671b6009f 130 */
spiridion 0:9a0671b6009f 131 int ReadRawPressure(long* pUp);
spiridion 0:9a0671b6009f 132
spiridion 0:9a0671b6009f 133 /** Calculation of the temperature from the digital output
spiridion 0:9a0671b6009f 134 */
spiridion 0:9a0671b6009f 135 float TrueTemperature(long ut);
spiridion 0:9a0671b6009f 136
spiridion 0:9a0671b6009f 137 /** Calculation of the pressure from the digital output
spiridion 0:9a0671b6009f 138 */
spiridion 0:9a0671b6009f 139 float TruePressure(long up);
spiridion 0:9a0671b6009f 140
spiridion 0:9a0671b6009f 141 int m_oss;
spiridion 0:9a0671b6009f 142 float m_temperature;
spiridion 0:9a0671b6009f 143 float m_pressure;
spiridion 0:9a0671b6009f 144 float m_altitude;
spiridion 0:9a0671b6009f 145
spiridion 0:9a0671b6009f 146 I2C m_i2c;
spiridion 0:9a0671b6009f 147 int m_addr;
spiridion 0:9a0671b6009f 148 char m_data[4];
spiridion 0:9a0671b6009f 149
spiridion 0:9a0671b6009f 150 short ac1, ac2, ac3;
spiridion 0:9a0671b6009f 151 unsigned short ac4, ac5, ac6;
spiridion 0:9a0671b6009f 152 short b1, b2;
spiridion 0:9a0671b6009f 153 short mb, mc, md;
spiridion 0:9a0671b6009f 154 long x1, x2, x3, b3, b5, b6;
spiridion 0:9a0671b6009f 155 unsigned long b4, b7;
spiridion 0:9a0671b6009f 156
spiridion 0:9a0671b6009f 157 };
spiridion 0:9a0671b6009f 158
spiridion 0:9a0671b6009f 159 #endif