BMP180 Pressure/Temperature Sensor library

Dependents:   GliderFuncTest1 DropTest

Fork of BMP180 by Spiridion Mbed

Committer:
spiridion
Date:
Sat Mar 08 21:42:40 2014 +0000
Revision:
0:9a0671b6009f
Child:
1:072073c79cfd
first release

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 0:9a0671b6009f 14 ...............
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 0:9a0671b6009f 55 * bmp180.Initialize(64, BMP180_OSS_NORMAL); // 64m altitude compensation and normal oversampling
spiridion 0:9a0671b6009f 56 *
spiridion 0:9a0671b6009f 57 * while(1) {
spiridion 0:9a0671b6009f 58 * if (bmp180.ReadData(&pressure, &temperature))
spiridion 0:9a0671b6009f 59 * printf("Pressure(hPa): %8.2f \t Temperature(C): %8.2f\n", pressure, temperature);
spiridion 0:9a0671b6009f 60 * wait(1);
spiridion 0:9a0671b6009f 61 * }
spiridion 0:9a0671b6009f 62 * }
spiridion 0:9a0671b6009f 63 * @endcode
spiridion 0:9a0671b6009f 64 */
spiridion 0:9a0671b6009f 65 class BMP180
spiridion 0:9a0671b6009f 66 {
spiridion 0:9a0671b6009f 67
spiridion 0:9a0671b6009f 68 public:
spiridion 0:9a0671b6009f 69
spiridion 0:9a0671b6009f 70 /** Create a BMP180 instance
spiridion 0:9a0671b6009f 71 * @param sda pin
spiridion 0:9a0671b6009f 72 * @param scl pin
spiridion 0:9a0671b6009f 73 * @param address: I2C slave address
spiridion 0:9a0671b6009f 74 */
spiridion 0:9a0671b6009f 75 BMP180(PinName sda, PinName scl, int address = BMP180_I2C_ADDRESS);
spiridion 0:9a0671b6009f 76
spiridion 0:9a0671b6009f 77 /** Create a BMP180 instance
spiridion 0:9a0671b6009f 78 * @param i2c object
spiridion 0:9a0671b6009f 79 * @param address: I2C slave address
spiridion 0:9a0671b6009f 80 */
spiridion 0:9a0671b6009f 81 BMP180(I2C& i2c, int address = BMP180_I2C_ADDRESS);
spiridion 0:9a0671b6009f 82
spiridion 0:9a0671b6009f 83 /** Initialization: set member values and read BMP180 calibration parameters
spiridion 0:9a0671b6009f 84 * @param altitude (in meter)
spiridion 0:9a0671b6009f 85 * @param overSamplingSetting
spiridion 0:9a0671b6009f 86 */
spiridion 0:9a0671b6009f 87 int Initialize(float altitude = 0.F, int overSamplingSetting = BMP180_OSS_NORMAL);
spiridion 0:9a0671b6009f 88
spiridion 0:9a0671b6009f 89 /** Read pressure and temperature from the BMP180.
spiridion 0:9a0671b6009f 90 * @param pressure (hPa)
spiridion 0:9a0671b6009f 91 * @param temperature (C)
spiridion 0:9a0671b6009f 92 * @returns
spiridion 0:9a0671b6009f 93 * 1 on success,
spiridion 0:9a0671b6009f 94 * 0 on error
spiridion 0:9a0671b6009f 95 */
spiridion 0:9a0671b6009f 96 int ReadData(float* pTemperature = NULL, float* pPressure = NULL);
spiridion 0:9a0671b6009f 97
spiridion 0:9a0671b6009f 98 /** Get temperature from a previous measurement
spiridion 0:9a0671b6009f 99 *
spiridion 0:9a0671b6009f 100 * @returns
spiridion 0:9a0671b6009f 101 * temperature (C)
spiridion 0:9a0671b6009f 102 */
spiridion 0:9a0671b6009f 103 float GetTemperature() {return m_temperature;};
spiridion 0:9a0671b6009f 104
spiridion 0:9a0671b6009f 105 /** Get pressure from a previous measurement
spiridion 0:9a0671b6009f 106 *
spiridion 0:9a0671b6009f 107 * @returns
spiridion 0:9a0671b6009f 108 * pressure (hPa)
spiridion 0:9a0671b6009f 109 */
spiridion 0:9a0671b6009f 110 float GetPressure() {return m_pressure;};
spiridion 0:9a0671b6009f 111
spiridion 0:9a0671b6009f 112 protected:
spiridion 0:9a0671b6009f 113
spiridion 0:9a0671b6009f 114 /** Perform temperature measurement
spiridion 0:9a0671b6009f 115 *
spiridion 0:9a0671b6009f 116 * @returns
spiridion 0:9a0671b6009f 117 * temperature (C)
spiridion 0:9a0671b6009f 118 */
spiridion 0:9a0671b6009f 119 int ReadRawTemperature(long* pUt);
spiridion 0:9a0671b6009f 120
spiridion 0:9a0671b6009f 121 /** Perform pressure measurement
spiridion 0:9a0671b6009f 122 *
spiridion 0:9a0671b6009f 123 * @returns
spiridion 0:9a0671b6009f 124 * temperature (C)
spiridion 0:9a0671b6009f 125 */
spiridion 0:9a0671b6009f 126 int ReadRawPressure(long* pUp);
spiridion 0:9a0671b6009f 127
spiridion 0:9a0671b6009f 128 /** Calculation of the temperature from the digital output
spiridion 0:9a0671b6009f 129 */
spiridion 0:9a0671b6009f 130 float TrueTemperature(long ut);
spiridion 0:9a0671b6009f 131
spiridion 0:9a0671b6009f 132 /** Calculation of the pressure from the digital output
spiridion 0:9a0671b6009f 133 */
spiridion 0:9a0671b6009f 134 float TruePressure(long up);
spiridion 0:9a0671b6009f 135
spiridion 0:9a0671b6009f 136 int m_oss;
spiridion 0:9a0671b6009f 137 float m_temperature;
spiridion 0:9a0671b6009f 138 float m_pressure;
spiridion 0:9a0671b6009f 139 float m_altitude;
spiridion 0:9a0671b6009f 140
spiridion 0:9a0671b6009f 141 I2C m_i2c;
spiridion 0:9a0671b6009f 142 int m_addr;
spiridion 0:9a0671b6009f 143 char m_data[4];
spiridion 0:9a0671b6009f 144
spiridion 0:9a0671b6009f 145 short ac1, ac2, ac3;
spiridion 0:9a0671b6009f 146 unsigned short ac4, ac5, ac6;
spiridion 0:9a0671b6009f 147 short b1, b2;
spiridion 0:9a0671b6009f 148 short mb, mc, md;
spiridion 0:9a0671b6009f 149 long x1, x2, x3, b3, b5, b6;
spiridion 0:9a0671b6009f 150 unsigned long b4, b7;
spiridion 0:9a0671b6009f 151
spiridion 0:9a0671b6009f 152 };
spiridion 0:9a0671b6009f 153
spiridion 0:9a0671b6009f 154 #endif