shenzhi xu / Mbed 2 deprecated temperature

Dependencies:   mbed

Committer:
xuszdd
Date:
Sun May 10 20:47:53 2015 +0000
Revision:
0:ee32d3554f6f
temperature and pressure

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xuszdd 0:ee32d3554f6f 1 /**
xuszdd 0:ee32d3554f6f 2 @file BMP180.h
xuszdd 0:ee32d3554f6f 3
xuszdd 0:ee32d3554f6f 4 @brief Header file containing member functions and variables
xuszdd 0:ee32d3554f6f 5
xuszdd 0:ee32d3554f6f 6 */
xuszdd 0:ee32d3554f6f 7
xuszdd 0:ee32d3554f6f 8 #ifndef BMP180_H
xuszdd 0:ee32d3554f6f 9 #define BMP180_H
xuszdd 0:ee32d3554f6f 10
xuszdd 0:ee32d3554f6f 11 // BMP180 address (5.2 datasheet)
xuszdd 0:ee32d3554f6f 12 #define BMP180_W_ADDRESS 0xEE
xuszdd 0:ee32d3554f6f 13 #define BMP180_R_ADDRESS 0xEF
xuszdd 0:ee32d3554f6f 14 // Register Descriptions (4 datasheet)
xuszdd 0:ee32d3554f6f 15 #define ID_REG 0xD0
xuszdd 0:ee32d3554f6f 16 #define EEPROM_REG_ADD 0xAA
xuszdd 0:ee32d3554f6f 17
xuszdd 0:ee32d3554f6f 18 // uncomment line below to run in debug mode and compare to datasheet values
xuszdd 0:ee32d3554f6f 19 //#define DEBUG
xuszdd 0:ee32d3554f6f 20
xuszdd 0:ee32d3554f6f 21 // Struct to store calibration data
xuszdd 0:ee32d3554f6f 22 typedef struct Calibration Calibration;
xuszdd 0:ee32d3554f6f 23 struct Calibration {
xuszdd 0:ee32d3554f6f 24 int16_t AC1;
xuszdd 0:ee32d3554f6f 25 int16_t AC2;
xuszdd 0:ee32d3554f6f 26 int16_t AC3;
xuszdd 0:ee32d3554f6f 27 uint16_t AC4;
xuszdd 0:ee32d3554f6f 28 uint16_t AC5;
xuszdd 0:ee32d3554f6f 29 uint16_t AC6;
xuszdd 0:ee32d3554f6f 30 int16_t B1;
xuszdd 0:ee32d3554f6f 31 int16_t B2;
xuszdd 0:ee32d3554f6f 32 int16_t MB;
xuszdd 0:ee32d3554f6f 33 int16_t MC;
xuszdd 0:ee32d3554f6f 34 int16_t MD;
xuszdd 0:ee32d3554f6f 35 };
xuszdd 0:ee32d3554f6f 36
xuszdd 0:ee32d3554f6f 37 // Struct for measurement (temperature and pressure)
xuszdd 0:ee32d3554f6f 38 typedef struct Measurement Measurement;
xuszdd 0:ee32d3554f6f 39 struct Measurement {
xuszdd 0:ee32d3554f6f 40 float temperature;
xuszdd 0:ee32d3554f6f 41 float pressure;
xuszdd 0:ee32d3554f6f 42 };
xuszdd 0:ee32d3554f6f 43
xuszdd 0:ee32d3554f6f 44 #include "mbed.h"
xuszdd 0:ee32d3554f6f 45
xuszdd 0:ee32d3554f6f 46 /**
xuszdd 0:ee32d3554f6f 47 @brief Library for interfacing with BMP180 Barometer
xuszdd 0:ee32d3554f6f 48 @see https://www.bosch-sensortec.com/en/homepage/products_3/environmental_sensors_1/bmp180_1/bmp180
xuszdd 0:ee32d3554f6f 49 @see https://www.sparkfun.com/products/11824
xuszdd 0:ee32d3554f6f 50
xuszdd 0:ee32d3554f6f 51 @brief Revision 1.0
xuszdd 0:ee32d3554f6f 52
xuszdd 0:ee32d3554f6f 53 @author Craig A. Evans
xuszdd 0:ee32d3554f6f 54 @date March 2015
xuszdd 0:ee32d3554f6f 55 *
xuszdd 0:ee32d3554f6f 56 * Example:
xuszdd 0:ee32d3554f6f 57 * @code
xuszdd 0:ee32d3554f6f 58
xuszdd 0:ee32d3554f6f 59 #include "mbed.h"
xuszdd 0:ee32d3554f6f 60 #include "BMP180.h"
xuszdd 0:ee32d3554f6f 61
xuszdd 0:ee32d3554f6f 62 BMP180 bmp180(p28,p27); // SDA, SCL
xuszdd 0:ee32d3554f6f 63 Serial serial(USBTX,USBRX);
xuszdd 0:ee32d3554f6f 64
xuszdd 0:ee32d3554f6f 65 int main() {
xuszdd 0:ee32d3554f6f 66
xuszdd 0:ee32d3554f6f 67 // initiliase barometer
xuszdd 0:ee32d3554f6f 68 bmp180.init();
xuszdd 0:ee32d3554f6f 69
xuszdd 0:ee32d3554f6f 70 Measurement measurement; // measurement structure declared in BMP180 class
xuszdd 0:ee32d3554f6f 71
xuszdd 0:ee32d3554f6f 72 while(1) {
xuszdd 0:ee32d3554f6f 73
xuszdd 0:ee32d3554f6f 74 // read values (T in Celsius and P in mb) and print over serial port
xuszdd 0:ee32d3554f6f 75 measurement = bmp180.readValues();
xuszdd 0:ee32d3554f6f 76 serial.printf("T = %.2f C P = %.2f mb\n",measurement.temperature,measurement.pressure);
xuszdd 0:ee32d3554f6f 77 wait(1.0); // short delau until next reading
xuszdd 0:ee32d3554f6f 78
xuszdd 0:ee32d3554f6f 79 }
xuszdd 0:ee32d3554f6f 80 }
xuszdd 0:ee32d3554f6f 81
xuszdd 0:ee32d3554f6f 82
xuszdd 0:ee32d3554f6f 83 * @endcode
xuszdd 0:ee32d3554f6f 84 */
xuszdd 0:ee32d3554f6f 85
xuszdd 0:ee32d3554f6f 86 class BMP180
xuszdd 0:ee32d3554f6f 87 {
xuszdd 0:ee32d3554f6f 88 public:
xuszdd 0:ee32d3554f6f 89
xuszdd 0:ee32d3554f6f 90 /** Create a BMP180 object connected to the specified I2C pins
xuszdd 0:ee32d3554f6f 91 *
xuszdd 0:ee32d3554f6f 92 * @param sdaPin - mbed SDA pin
xuszdd 0:ee32d3554f6f 93 * @param sclPin - mbed SCL pin
xuszdd 0:ee32d3554f6f 94 *
xuszdd 0:ee32d3554f6f 95 */
xuszdd 0:ee32d3554f6f 96 BMP180(PinName sdaPin, PinName sclPin);
xuszdd 0:ee32d3554f6f 97
xuszdd 0:ee32d3554f6f 98 /** Initialise barometer - reads factory calibration data
xuszdd 0:ee32d3554f6f 99 *
xuszdd 0:ee32d3554f6f 100 */
xuszdd 0:ee32d3554f6f 101 void init();
xuszdd 0:ee32d3554f6f 102
xuszdd 0:ee32d3554f6f 103 /** Read current temperature and pressure values
xuszdd 0:ee32d3554f6f 104 *
xuszdd 0:ee32d3554f6f 105 * @returns Measurement structure. Memebers are temperature in C (float) and pressure in mbar (float)
xuszdd 0:ee32d3554f6f 106 *
xuszdd 0:ee32d3554f6f 107 */
xuszdd 0:ee32d3554f6f 108 Measurement readValues();
xuszdd 0:ee32d3554f6f 109
xuszdd 0:ee32d3554f6f 110 private:
xuszdd 0:ee32d3554f6f 111 void error();
xuszdd 0:ee32d3554f6f 112 int32_t readUncompensatedTemperatureValue();
xuszdd 0:ee32d3554f6f 113 int32_t readUncompensatedPressureValue();
xuszdd 0:ee32d3554f6f 114 int32_t calcTrueTemperature(int32_t UT);
xuszdd 0:ee32d3554f6f 115 int32_t calcTruePressure(int32_t UP);
xuszdd 0:ee32d3554f6f 116 void sendByteToRegister(char byte,char reg);
xuszdd 0:ee32d3554f6f 117 char readByteFromRegister(char reg);
xuszdd 0:ee32d3554f6f 118 void readBytesFromRegister(char reg,int numberOfBytes,char bytes[]);
xuszdd 0:ee32d3554f6f 119 void readCalibrationData();
xuszdd 0:ee32d3554f6f 120
xuszdd 0:ee32d3554f6f 121
xuszdd 0:ee32d3554f6f 122 private: // private variables
xuszdd 0:ee32d3554f6f 123 I2C* i2c;
xuszdd 0:ee32d3554f6f 124 BusOut* leds;
xuszdd 0:ee32d3554f6f 125 Calibration calibration; // variable to store calibration data
xuszdd 0:ee32d3554f6f 126 // variables for calculation
xuszdd 0:ee32d3554f6f 127 int32_t X1,X2,X3,B3,B5,B6;
xuszdd 0:ee32d3554f6f 128 uint32_t B4,B7;
xuszdd 0:ee32d3554f6f 129 int oss; // oversampling setting
xuszdd 0:ee32d3554f6f 130 };
xuszdd 0:ee32d3554f6f 131
xuszdd 0:ee32d3554f6f 132 #endif