Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API TMP_nrf51 mbed nRF51822
Fork of VTT_NODEV3_BMP180_Si7021 by
BMP180.h@3:101b0f081435, 2016-05-27 (annotated)
- Committer:
- JarkkoS
- Date:
- Fri May 27 07:38:42 2016 +0000
- Revision:
- 3:101b0f081435
- Parent:
- 2:b221ba23b37f
VTT TinyNodeV2, slightly modified code from example (https://developer.mbed.org/users/jejuho/code/VTT_NODEV3_BMP180_Si7021)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jejuho | 2:b221ba23b37f | 1 | #ifndef BMP180_H |
jejuho | 2:b221ba23b37f | 2 | #define BMP180_H |
jejuho | 2:b221ba23b37f | 3 | |
jejuho | 2:b221ba23b37f | 4 | #include "mbed.h" |
jejuho | 2:b221ba23b37f | 5 | |
jejuho | 2:b221ba23b37f | 6 | #define BMP180_ADDRESS 0x77<<1 // I2C address of BMP180, eight bit address on mbed |
jejuho | 2:b221ba23b37f | 7 | #define BMP180_WHO_AM_I 0xD0 // WHO_AM_I id of BMP180, should return 0x55 |
jejuho | 2:b221ba23b37f | 8 | #define BMP180_RESET 0xE0 |
jejuho | 2:b221ba23b37f | 9 | #define BMP180_CONTROL 0xF4 |
jejuho | 2:b221ba23b37f | 10 | #define BMP180_OUT_MSB 0xF6 |
jejuho | 2:b221ba23b37f | 11 | #define BMP180_OUT_LSB 0xF7 |
jejuho | 2:b221ba23b37f | 12 | #define BMP180_OUT_XLSB 0xF8 |
jejuho | 2:b221ba23b37f | 13 | |
jejuho | 2:b221ba23b37f | 14 | // Set initial input parameters |
jejuho | 2:b221ba23b37f | 15 | |
jejuho | 2:b221ba23b37f | 16 | enum OSS { // BMP-085 sampling rate |
jejuho | 2:b221ba23b37f | 17 | OSS_0 = 0, // 4.5 ms conversion time |
jejuho | 2:b221ba23b37f | 18 | OSS_1, // 7.5 |
jejuho | 2:b221ba23b37f | 19 | OSS_2, // 13.5 |
jejuho | 2:b221ba23b37f | 20 | OSS_3 // 25.5 |
jejuho | 2:b221ba23b37f | 21 | }; |
jejuho | 2:b221ba23b37f | 22 | |
jejuho | 2:b221ba23b37f | 23 | uint8_t OSS = OSS_3; // maximum pressure resolution |
jejuho | 2:b221ba23b37f | 24 | |
jejuho | 2:b221ba23b37f | 25 | // These are constants used to calculate the temperature and pressure from the BMP-180 sensor |
jejuho | 2:b221ba23b37f | 26 | int16_t ac1, ac2, ac3, b1, b2, mb, mc, md, b5; |
jejuho | 2:b221ba23b37f | 27 | uint16_t ac4, ac5, ac6; |
jejuho | 2:b221ba23b37f | 28 | |
jejuho | 2:b221ba23b37f | 29 | class BMP180 { |
jejuho | 2:b221ba23b37f | 30 | |
jejuho | 2:b221ba23b37f | 31 | private: |
jejuho | 2:b221ba23b37f | 32 | //Set up I2C, (SDA,SCL) |
jejuho | 2:b221ba23b37f | 33 | I2C i2c; |
jejuho | 2:b221ba23b37f | 34 | |
jejuho | 2:b221ba23b37f | 35 | protected: |
jejuho | 2:b221ba23b37f | 36 | |
jejuho | 2:b221ba23b37f | 37 | public: |
jejuho | 2:b221ba23b37f | 38 | |
jejuho | 2:b221ba23b37f | 39 | BMP180(PinName sda, PinName scl) : i2c(sda, scl) { |
jejuho | 2:b221ba23b37f | 40 | |
jejuho | 2:b221ba23b37f | 41 | } |
jejuho | 2:b221ba23b37f | 42 | |
jejuho | 2:b221ba23b37f | 43 | |
jejuho | 2:b221ba23b37f | 44 | |
jejuho | 2:b221ba23b37f | 45 | //=================================================================================================================== |
jejuho | 2:b221ba23b37f | 46 | //====== Set of useful function to access pressure and temperature data |
jejuho | 2:b221ba23b37f | 47 | //=================================================================================================================== |
jejuho | 2:b221ba23b37f | 48 | |
jejuho | 2:b221ba23b37f | 49 | void writeByte(uint8_t address, uint8_t subAddress, uint8_t data) |
jejuho | 2:b221ba23b37f | 50 | { |
jejuho | 2:b221ba23b37f | 51 | char data_write[2]; |
jejuho | 2:b221ba23b37f | 52 | data_write[0] = subAddress; |
jejuho | 2:b221ba23b37f | 53 | data_write[1] = data; |
jejuho | 2:b221ba23b37f | 54 | i2c.write(address, data_write, 2, 0); |
jejuho | 2:b221ba23b37f | 55 | } |
jejuho | 2:b221ba23b37f | 56 | |
jejuho | 2:b221ba23b37f | 57 | char readByte(uint8_t address, uint8_t subAddress) |
jejuho | 2:b221ba23b37f | 58 | { |
jejuho | 2:b221ba23b37f | 59 | char data[1]; // `data` will store the register data |
jejuho | 2:b221ba23b37f | 60 | char data_write[1]; |
jejuho | 2:b221ba23b37f | 61 | data_write[0] = subAddress; |
jejuho | 2:b221ba23b37f | 62 | i2c.write(address, data_write, 1, 1); // no stop |
jejuho | 2:b221ba23b37f | 63 | i2c.read(address, data, 1, 0); |
jejuho | 2:b221ba23b37f | 64 | return data[0]; |
jejuho | 2:b221ba23b37f | 65 | } |
jejuho | 2:b221ba23b37f | 66 | |
jejuho | 2:b221ba23b37f | 67 | void readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest) |
jejuho | 2:b221ba23b37f | 68 | { |
jejuho | 2:b221ba23b37f | 69 | char data[14]; |
jejuho | 2:b221ba23b37f | 70 | char data_write[1]; |
jejuho | 2:b221ba23b37f | 71 | data_write[0] = subAddress; |
jejuho | 2:b221ba23b37f | 72 | i2c.write(address, data_write, 1, 1); // no stop |
jejuho | 2:b221ba23b37f | 73 | i2c.read(address, data, count, 0); |
jejuho | 2:b221ba23b37f | 74 | for(int ii = 0; ii < count; ii++) { |
jejuho | 2:b221ba23b37f | 75 | dest[ii] = data[ii]; |
jejuho | 2:b221ba23b37f | 76 | } |
jejuho | 2:b221ba23b37f | 77 | } |
jejuho | 2:b221ba23b37f | 78 | |
jejuho | 2:b221ba23b37f | 79 | |
jejuho | 2:b221ba23b37f | 80 | // Stores all of the BMP180's calibration values into global variables |
jejuho | 2:b221ba23b37f | 81 | // Calibration values are required to calculate temp and pressure |
jejuho | 2:b221ba23b37f | 82 | // This function should be called at the beginning of the program |
jejuho | 2:b221ba23b37f | 83 | // These BMP-180 functions were adapted from Jim Lindblom of SparkFun Electronics |
jejuho | 2:b221ba23b37f | 84 | void BMP180Calibration() |
jejuho | 2:b221ba23b37f | 85 | { |
jejuho | 2:b221ba23b37f | 86 | ac1 = readByte(BMP180_ADDRESS, 0xAA) << 8 | readByte(BMP180_ADDRESS, 0xAB); |
jejuho | 2:b221ba23b37f | 87 | ac2 = readByte(BMP180_ADDRESS, 0xAC) << 8 | readByte(BMP180_ADDRESS, 0xAD); |
jejuho | 2:b221ba23b37f | 88 | ac3 = readByte(BMP180_ADDRESS, 0xAE) << 8 | readByte(BMP180_ADDRESS, 0xAF); |
jejuho | 2:b221ba23b37f | 89 | ac4 = readByte(BMP180_ADDRESS, 0xB0) << 8 | readByte(BMP180_ADDRESS, 0xB1); |
jejuho | 2:b221ba23b37f | 90 | ac5 = readByte(BMP180_ADDRESS, 0xB2) << 8 | readByte(BMP180_ADDRESS, 0xB3); |
jejuho | 2:b221ba23b37f | 91 | ac6 = readByte(BMP180_ADDRESS, 0xB4) << 8 | readByte(BMP180_ADDRESS, 0xB5); |
jejuho | 2:b221ba23b37f | 92 | b1 = readByte(BMP180_ADDRESS, 0xB6) << 8 | readByte(BMP180_ADDRESS, 0xB7); |
jejuho | 2:b221ba23b37f | 93 | b2 = readByte(BMP180_ADDRESS, 0xB8) << 8 | readByte(BMP180_ADDRESS, 0xB9); |
jejuho | 2:b221ba23b37f | 94 | mb = readByte(BMP180_ADDRESS, 0xBA) << 8 | readByte(BMP180_ADDRESS, 0xBB); |
jejuho | 2:b221ba23b37f | 95 | mc = readByte(BMP180_ADDRESS, 0xBC) << 8 | readByte(BMP180_ADDRESS, 0xBD); |
jejuho | 2:b221ba23b37f | 96 | md = readByte(BMP180_ADDRESS, 0xBE) << 8 | readByte(BMP180_ADDRESS, 0xBF); |
jejuho | 2:b221ba23b37f | 97 | } |
jejuho | 2:b221ba23b37f | 98 | |
jejuho | 2:b221ba23b37f | 99 | // Temperature returned will be in units of 0.1 deg C |
jejuho | 2:b221ba23b37f | 100 | int16_t BMP180GetTemperature() |
jejuho | 2:b221ba23b37f | 101 | { |
jejuho | 2:b221ba23b37f | 102 | int16_t ut = 0; |
jejuho | 2:b221ba23b37f | 103 | writeByte(BMP180_ADDRESS, 0xF4, 0x2E); // start temperature measurement |
jejuho | 2:b221ba23b37f | 104 | wait(0.005); |
jejuho | 2:b221ba23b37f | 105 | |
jejuho | 2:b221ba23b37f | 106 | uint8_t rawData[2] = {0, 0}; |
jejuho | 2:b221ba23b37f | 107 | readBytes(BMP180_ADDRESS, 0xF6, 2, &rawData[0]); // read raw temperature measurement |
jejuho | 2:b221ba23b37f | 108 | ut = (((int16_t) rawData[0] << 8) | rawData[1]); |
jejuho | 2:b221ba23b37f | 109 | |
jejuho | 2:b221ba23b37f | 110 | long x1, x2; |
jejuho | 2:b221ba23b37f | 111 | |
jejuho | 2:b221ba23b37f | 112 | x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15; |
jejuho | 2:b221ba23b37f | 113 | x2 = ((long)mc << 11)/(x1 + md); |
jejuho | 2:b221ba23b37f | 114 | b5 = x1 + x2; |
jejuho | 2:b221ba23b37f | 115 | |
jejuho | 2:b221ba23b37f | 116 | return ((b5 + 8)>>4); |
jejuho | 2:b221ba23b37f | 117 | } |
jejuho | 2:b221ba23b37f | 118 | |
jejuho | 2:b221ba23b37f | 119 | // Calculate pressure read calibration values |
jejuho | 2:b221ba23b37f | 120 | // b5 is also required so BMP180GetTemperature() must be called first. |
jejuho | 2:b221ba23b37f | 121 | // Value returned will be pressure in units of Pa. |
jejuho | 2:b221ba23b37f | 122 | long BMP180GetPressure() |
jejuho | 2:b221ba23b37f | 123 | { |
jejuho | 2:b221ba23b37f | 124 | long up = 0; |
jejuho | 2:b221ba23b37f | 125 | writeByte(BMP180_ADDRESS, 0xF4, 0x34 | OSS << 6); // Configure pressure measurement for highest resolution |
jejuho | 2:b221ba23b37f | 126 | wait((5.0f + 8.0f*3.0f)/1000.0f); |
jejuho | 2:b221ba23b37f | 127 | uint8_t rawData[3] = {0, 0, 0}; |
jejuho | 2:b221ba23b37f | 128 | readBytes(BMP180_ADDRESS, 0xF6, 3, &rawData[0]); // read raw pressure measurement of 19 bits |
jejuho | 2:b221ba23b37f | 129 | up = (((long) rawData[0] << 16) | ((long)rawData[1] << 8) | rawData[2]) >> (8 - OSS); |
jejuho | 2:b221ba23b37f | 130 | |
jejuho | 2:b221ba23b37f | 131 | long x1, x2, x3, b3, b6, p; |
jejuho | 2:b221ba23b37f | 132 | unsigned long b4, b7; |
jejuho | 2:b221ba23b37f | 133 | |
jejuho | 2:b221ba23b37f | 134 | b6 = b5 - 4000; |
jejuho | 2:b221ba23b37f | 135 | // Calculate B3 |
jejuho | 2:b221ba23b37f | 136 | x1 = (b2 * (b6 * b6)>>12)>>11; |
jejuho | 2:b221ba23b37f | 137 | x2 = (ac2 * b6)>>11; |
jejuho | 2:b221ba23b37f | 138 | x3 = x1 + x2; |
jejuho | 2:b221ba23b37f | 139 | b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2; |
jejuho | 2:b221ba23b37f | 140 | |
jejuho | 2:b221ba23b37f | 141 | // Calculate B4 |
jejuho | 2:b221ba23b37f | 142 | x1 = (ac3 * b6)>>13; |
jejuho | 2:b221ba23b37f | 143 | x2 = (b1 * ((b6 * b6)>>12))>>16; |
jejuho | 2:b221ba23b37f | 144 | x3 = ((x1 + x2) + 2)>>2; |
jejuho | 2:b221ba23b37f | 145 | b4 = (ac4 * (unsigned long)(x3 + 32768))>>15; |
jejuho | 2:b221ba23b37f | 146 | |
jejuho | 2:b221ba23b37f | 147 | b7 = ((unsigned long)(up - b3) * (50000>>OSS)); |
jejuho | 2:b221ba23b37f | 148 | if (b7 < 0x80000000) |
jejuho | 2:b221ba23b37f | 149 | p = (b7<<1)/b4; |
jejuho | 2:b221ba23b37f | 150 | else |
jejuho | 2:b221ba23b37f | 151 | p = (b7/b4)<<1; |
jejuho | 2:b221ba23b37f | 152 | |
jejuho | 2:b221ba23b37f | 153 | x1 = (p>>8) * (p>>8); |
jejuho | 2:b221ba23b37f | 154 | x1 = (x1 * 3038)>>16; |
jejuho | 2:b221ba23b37f | 155 | x2 = (-7357 * p)>>16; |
jejuho | 2:b221ba23b37f | 156 | p += (x1 + x2 + 3791)>>4; |
jejuho | 2:b221ba23b37f | 157 | |
jejuho | 2:b221ba23b37f | 158 | return p; |
jejuho | 2:b221ba23b37f | 159 | } |
jejuho | 2:b221ba23b37f | 160 | |
jejuho | 2:b221ba23b37f | 161 | |
jejuho | 2:b221ba23b37f | 162 | |
jejuho | 2:b221ba23b37f | 163 | }; |
jejuho | 2:b221ba23b37f | 164 | #endif |