Class for using BMP180 Bosch Pressure sensor

Dependents:   ILI9341_Clock_Nucleo IoT-Polytech-Upmc

Committer:
harrypowers
Date:
Tue Nov 26 20:14:34 2013 +0000
Revision:
4:01c74f1b5f4d
Parent:
3:f9ac42af2020
small doc update!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
harrypowers 0:b899fe37ce17 1 #ifndef BMP180_H
harrypowers 0:b899fe37ce17 2 #define BMP180_H
harrypowers 0:b899fe37ce17 3
harrypowers 0:b899fe37ce17 4 /* BMP180 Digital Pressure Sensor Class for use with Mbed LPC1768 and other platforms
harrypowers 0:b899fe37ce17 5 * BMP180 from Bosch Sensortec
harrypowers 0:b899fe37ce17 6 * Copyright (c) 2013 Philip King Smith
harrypowers 0:b899fe37ce17 7 *
harrypowers 0:b899fe37ce17 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
harrypowers 0:b899fe37ce17 9 * of this software and associated documentation files (the "Software"), to deal
harrypowers 0:b899fe37ce17 10 * in the Software without restriction, including without limitation the rights
harrypowers 0:b899fe37ce17 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
harrypowers 0:b899fe37ce17 12 * copies of the Software, and to permit persons to whom the Software is
harrypowers 0:b899fe37ce17 13 * furnished to do so, subject to the following conditions:
harrypowers 0:b899fe37ce17 14 *
harrypowers 0:b899fe37ce17 15 * The above copyright notice and this permission notice shall be included in
harrypowers 0:b899fe37ce17 16 * all copies or substantial portions of the Software.
harrypowers 0:b899fe37ce17 17 *
harrypowers 0:b899fe37ce17 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
harrypowers 0:b899fe37ce17 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
harrypowers 0:b899fe37ce17 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
harrypowers 0:b899fe37ce17 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
harrypowers 0:b899fe37ce17 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
harrypowers 0:b899fe37ce17 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
harrypowers 0:b899fe37ce17 24 * THE SOFTWARE.
harrypowers 2:4749ef781396 25 *
harrypowers 2:4749ef781396 26 * Bosch data sheet at: http://ae-bst.resource.bosch.com/media/products/dokumente/bmp180/BST-BMP180-DS000-09.pdf
harrypowers 2:4749ef781396 27 * Some parts of the calculations used are infact from Rom Clement published Mbed code here: https://mbed.org/users/Rom/code/Barometer_bmp085/
harrypowers 2:4749ef781396 28 * I only used snippets of the Rom's code because i was making this into a class and so this is structured totaly different then his code example is.
harrypowers 2:4749ef781396 29 * I also used the Bosch data sheet showing the calculations and adjusted everything accordingly!
harrypowers 0:b899fe37ce17 30 */
harrypowers 0:b899fe37ce17 31
harrypowers 0:b899fe37ce17 32 #include "mbed.h"
harrypowers 0:b899fe37ce17 33
harrypowers 1:4c6b41f1203d 34 #define EEprom 22 // The EEPROM has 176bits of calibration data (176/8 = 22 Bytes)
harrypowers 1:4c6b41f1203d 35 #define BMP180ADDR 0xEF // I2C address of BMP180 device
harrypowers 0:b899fe37ce17 36 #define BMP180FREQ 1000000 // Data sheet says 3.4 MHz is max but not sure what mbed can do here!
harrypowers 0:b899fe37ce17 37 #define CMD_READ_VALUE 0xF6
harrypowers 0:b899fe37ce17 38 #define CMD_READ_CALIBRATION 0xAA
harrypowers 2:4749ef781396 39 #define OVERSAMPLING_ULTRA_LOW_POWER 0 // these are the constants used in the oversample variable in the below code!
harrypowers 0:b899fe37ce17 40 #define OVERSAMPLING_STANDARD 1
harrypowers 0:b899fe37ce17 41 #define OVERSAMPLING_HIGH_RESOLUTION 2
harrypowers 0:b899fe37ce17 42 #define OVERSAMPLING_ULTRA_HIGH_RESOLUTION 3
harrypowers 0:b899fe37ce17 43
harrypowers 2:4749ef781396 44
harrypowers 2:4749ef781396 45 /** BMP180 Digital Pressure Sensor class using mbed's i2c class
harrypowers 2:4749ef781396 46 *
harrypowers 2:4749ef781396 47 * Example:
harrypowers 2:4749ef781396 48 * @code
harrypowers 2:4749ef781396 49 * // show how the BMP180 class works
harrypowers 2:4749ef781396 50 * #include "mbed.h"
harrypowers 2:4749ef781396 51 * #include "BMP180.h"
harrypowers 2:4749ef781396 52 *
harrypowers 2:4749ef781396 53 * int main()
harrypowers 2:4749ef781396 54 * {
harrypowers 2:4749ef781396 55 *
harrypowers 2:4749ef781396 56 * long temp ;
harrypowers 2:4749ef781396 57 * long pressure;
harrypowers 2:4749ef781396 58 * int error ;
harrypowers 2:4749ef781396 59 * BMP180 mybmp180(p9,p10);
harrypowers 2:4749ef781396 60 * while(1) {
harrypowers 2:4749ef781396 61 * error = mybmp180.readTP(&temp,&pressure,OVERSAMPLING_ULTRA_HIGH_RESOLUTION);
harrypowers 2:4749ef781396 62 * printf("Temp is %ld\r\n",temp);
harrypowers 2:4749ef781396 63 * printf("Pressure is %ld\r\n",pressure);
harrypowers 2:4749ef781396 64 * printf("Error is %d\r\n\r\n",error);
harrypowers 2:4749ef781396 65 * wait(2);
harrypowers 2:4749ef781396 66 * }
harrypowers 2:4749ef781396 67 * }
harrypowers 2:4749ef781396 68 * @endcode
harrypowers 2:4749ef781396 69 */
harrypowers 2:4749ef781396 70
harrypowers 0:b899fe37ce17 71 class BMP180
harrypowers 0:b899fe37ce17 72 {
harrypowers 0:b899fe37ce17 73 public:
harrypowers 0:b899fe37ce17 74 /** Create object connected to BMP180 pins ( remember both pins need pull up resisters)
harrypowers 0:b899fe37ce17 75 *
harrypowers 0:b899fe37ce17 76 * Ensure the pull up resistors are used on these pins. Also note there is no checking on
harrypowers 2:4749ef781396 77 * if you use these pins p9, p10, p27, p28 so ensure you only use these ones on the LPC1768 device
harrypowers 0:b899fe37ce17 78 *
harrypowers 2:4749ef781396 79 * @param sda pin that BMP180 connected to (p9 or p28 as defined on LPC1768)
harrypowers 2:4749ef781396 80 * @param slc pin that BMP180 connected to (p10 or p27 ad defined on LPC1768)
harrypowers 0:b899fe37ce17 81 */
harrypowers 2:4749ef781396 82 BMP180(PinName sda, PinName slc); // Constructor
harrypowers 2:4749ef781396 83
harrypowers 2:4749ef781396 84 ~BMP180(); // Destructor
harrypowers 0:b899fe37ce17 85
harrypowers 2:4749ef781396 86 /** Read Temperature and Pressure at the same time
harrypowers 2:4749ef781396 87 *
harrypowers 2:4749ef781396 88 * This function will only return when it has readings. This means that the time will vary depending on oversample setting!
harrypowers 2:4749ef781396 89 * Note if your code can not wait for these readings to be taken and calculated you should use the functions below.
harrypowers 2:4749ef781396 90 * These other functions are designed to allow your code to do other things then get the final readings.
harrypowers 2:4749ef781396 91 * This function is only designed as a one shot give me the answer function.
harrypowers 2:4749ef781396 92 *
harrypowers 2:4749ef781396 93 * @param t the temperature fully compensated value is returned in this variable. Degrees celsius with one decimal so 253 is 25.3 C.
harrypowers 2:4749ef781396 94 * @param p the barometric pressure fully compensated value is returned in this variable. Pressure is in Pa so 88007 is 88.007 kPa.
harrypowers 2:4749ef781396 95 * @param oversample is the method method for reading sensor. OVERSAMPLING_ULTRA_HIGH_RESOLUTION is used if an incorrect value is passed to this function.
harrypowers 2:4749ef781396 96 * @param returns 0 for no errors during i2c communication. Any other number is just a i2c communication failure of some kind!
harrypowers 2:4749ef781396 97 */
harrypowers 0:b899fe37ce17 98 int readTP(long *t, long *p, int oversample); // get both temperature and pressure fully compensated values! Note this only returns when measurements are complete
harrypowers 0:b899fe37ce17 99
harrypowers 2:4749ef781396 100 /** Start the temperature reading process but return after the commands are issued to BMP180
harrypowers 2:4749ef781396 101 *
harrypowers 2:4749ef781396 102 * This function is ment to start the temperature reading process but will return to allow other code to run then a reading could be made at a later time.
harrypowers 2:4749ef781396 103 * Note the maximum time needed for this measurment is 4.5 ms.
harrypowers 2:4749ef781396 104 *
harrypowers 2:4749ef781396 105 * @param returns 0 for no errors during i2c communication. Any other number is just a i2c communication failure of some kind!
harrypowers 2:4749ef781396 106 */
harrypowers 0:b899fe37ce17 107 int startTemperature(); // Start temperature measurement
harrypowers 2:4749ef781396 108
harrypowers 2:4749ef781396 109 /** Reads the last temperature reading that was started with startTemperature() function
harrypowers 2:4749ef781396 110 *
harrypowers 2:4749ef781396 111 * This function will return the fully compensated value of the temperature in Degrees celsius with one decimal so 253 is 25.3 C.
harrypowers 2:4749ef781396 112 * Note this function should normaly follow the startTemperature() function and should also preceed the startPressure() and readPressure() commands!
harrypowers 4:01c74f1b5f4d 113 * Note this function should follow startTemperature() after 4.5 ms minimum has elapsed or reading will be incorrect.
harrypowers 2:4749ef781396 114 *
harrypowers 2:4749ef781396 115 * @param t the temperature fully compensated value is returned in this variable.
harrypowers 2:4749ef781396 116 * @param returns 0 for no errors during i2c communication. Any other number is just a i2c communication failure of some kind!
harrypowers 2:4749ef781396 117 */
harrypowers 0:b899fe37ce17 118 int readTemperature(long *t); // Get the temperature reading that was taken in startTemperature() but ensure 4.5 ms time has elapsed
harrypowers 2:4749ef781396 119
harrypowers 2:4749ef781396 120 /** Start the pressure reading process but return after the commands are issued to BMP180
harrypowers 2:4749ef781396 121 *
harrypowers 2:4749ef781396 122 * This function is ment to start the pressure reading process but will return to allow other code to run then a reading could be made at a later time.
harrypowers 2:4749ef781396 123 * Note the time needed for this reading pressure process will depend on oversample setting. The maximum time is 25.5 ms and minimum time is 4.5 ms.
harrypowers 2:4749ef781396 124 *
harrypowers 2:4749ef781396 125 * @param oversample is the method for reading sensor. OVERSAMPLING_ULTRA_HIGH_RESOLUTION is used if an incorrect value is passed to this function.
harrypowers 2:4749ef781396 126 * @param returns 0 for no errors during i2c communication. Any other number is just a i2c communication failure of some kind!
harrypowers 2:4749ef781396 127 */
harrypowers 0:b899fe37ce17 128 int startPressure(int oversample); // Start pressure measurement! Note oversample will vary the time to complete this measurement. See defines above for oversampling constants to use!
harrypowers 2:4749ef781396 129
harrypowers 3:f9ac42af2020 130 /** Reads the last barometric pressure reading that was started with startPressure() function
harrypowers 2:4749ef781396 131 *
harrypowers 2:4749ef781396 132 * This function will return the fully compensated value of the barometric pressure in Pa.
harrypowers 2:4749ef781396 133 * Note this function should follow startPressure() after the time needed to read the pressure. This time will vary but maximum time is 25.5 ms and minimum time is 4.5 ms.
harrypowers 2:4749ef781396 134 * Note that this reading is dependent on temperature so the startTemperature() and readTemperature() functions should proceed this function or the pressure value will be incorrect!
harrypowers 2:4749ef781396 135 *
harrypowers 2:4749ef781396 136 * @param p the barometric pressure fully compensated value is returned in this variable. Pressure is in Pa so 88007 is 88.007 kPa.
harrypowers 2:4749ef781396 137 * @param returns 0 for no errors during i2c communication. Any other number is just a i2c communication failure of some kind!
harrypowers 2:4749ef781396 138 */
harrypowers 0:b899fe37ce17 139 int readPressure(long *p); // Get the pressure reading that was taken in startPressure() but ensure time for the measurement to complete
harrypowers 0:b899fe37ce17 140
harrypowers 0:b899fe37ce17 141 protected:
harrypowers 0:b899fe37ce17 142 long x1;
harrypowers 0:b899fe37ce17 143 long x2;
harrypowers 0:b899fe37ce17 144 long x3;
harrypowers 0:b899fe37ce17 145 short ac1;
harrypowers 0:b899fe37ce17 146 short ac2;
harrypowers 0:b899fe37ce17 147 short ac3;
harrypowers 0:b899fe37ce17 148 unsigned short ac4;
harrypowers 0:b899fe37ce17 149 unsigned short ac5;
harrypowers 0:b899fe37ce17 150 unsigned short ac6;
harrypowers 0:b899fe37ce17 151 short b1;
harrypowers 0:b899fe37ce17 152 short b2;
harrypowers 0:b899fe37ce17 153 long b3;
harrypowers 0:b899fe37ce17 154 unsigned long b4;
harrypowers 0:b899fe37ce17 155 long b5;
harrypowers 0:b899fe37ce17 156 long b6;
harrypowers 0:b899fe37ce17 157 unsigned long b7;
harrypowers 0:b899fe37ce17 158 short mb;
harrypowers 0:b899fe37ce17 159 short mc;
harrypowers 0:b899fe37ce17 160 short md;
harrypowers 0:b899fe37ce17 161 int oversampling_setting;
harrypowers 0:b899fe37ce17 162 char rReg[3];
harrypowers 0:b899fe37ce17 163 char wReg[2];
harrypowers 0:b899fe37ce17 164 char cmd;
harrypowers 0:b899fe37ce17 165 char data[EEprom];
harrypowers 0:b899fe37ce17 166 char w[2];
harrypowers 0:b899fe37ce17 167
harrypowers 0:b899fe37ce17 168 I2C bmp180i2c; // the I2C class for this bmp180 communication.
harrypowers 1:4c6b41f1203d 169 int oversampleCheck(int oversample);
harrypowers 0:b899fe37ce17 170 };
harrypowers 0:b899fe37ce17 171
harrypowers 0:b899fe37ce17 172 #endif