Library for Bosch BMP280 temperature and pressure sensor
Dependents: ELEC350-LCD-DEMO ELEC350-CWTEMPLATE-2017 ELEC351_v1 ELEC350-CWTEMPLATE-2017 ... more
Fork of BMP280 by
Library for Temperature and Pressure Sensor Bosch BMP280.
Breakoutboard for example from Adafruit
BMP280.cpp@8:d22ecbef9b90, 2017-05-25 (annotated)
- Committer:
- charly
- Date:
- Thu May 25 20:22:32 2017 +0000
- Revision:
- 8:d22ecbef9b90
- Parent:
- 7:c72b726c7dc9
Buxfix in readTemperature
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MACRUM | 1:763a4018aaec | 1 | /** |
12104404 | 7:c72b726c7dc9 | 2 | * BMP280 Combined humidity and pressure sensor library |
MACRUM | 1:763a4018aaec | 3 | * |
MACRUM | 1:763a4018aaec | 4 | * @author Toyomasa Watarai |
MACRUM | 1:763a4018aaec | 5 | * @version 1.0 |
MACRUM | 1:763a4018aaec | 6 | * @date 06-April-2015 |
MACRUM | 1:763a4018aaec | 7 | * |
charly | 8:d22ecbef9b90 | 8 | * bugfixing by charly |
charly | 8:d22ecbef9b90 | 9 | * |
12104404 | 7:c72b726c7dc9 | 10 | * Library for "BMP280 temperature, humidity and pressure sensor module" from Switch Science |
MACRUM | 1:763a4018aaec | 11 | * https://www.switch-science.com/catalog/2236/ |
MACRUM | 1:763a4018aaec | 12 | * |
12104404 | 7:c72b726c7dc9 | 13 | * For more information about the BMP280: |
12104404 | 7:c72b726c7dc9 | 14 | * http://ae-bst.resource.bosch.com/media/products/dokumente/BMP280/BST-BMP280_DS001-10.pdf |
MACRUM | 1:763a4018aaec | 15 | */ |
MACRUM | 1:763a4018aaec | 16 | |
MACRUM | 0:ade9be832910 | 17 | #include "mbed.h" |
12104404 | 5:7139d5accbc3 | 18 | #include "BMP280.h" |
MACRUM | 0:ade9be832910 | 19 | |
12104404 | 7:c72b726c7dc9 | 20 | BMP280::BMP280(PinName sda, PinName scl, char slave_adr) |
MACRUM | 0:ade9be832910 | 21 | : |
MACRUM | 0:ade9be832910 | 22 | i2c_p(new I2C(sda, scl)), |
MACRUM | 0:ade9be832910 | 23 | i2c(*i2c_p), |
charly | 8:d22ecbef9b90 | 24 | address(slave_adr<<1), |
MACRUM | 0:ade9be832910 | 25 | t_fine(0) |
MACRUM | 0:ade9be832910 | 26 | { |
MACRUM | 0:ade9be832910 | 27 | initialize(); |
MACRUM | 0:ade9be832910 | 28 | } |
MACRUM | 0:ade9be832910 | 29 | |
12104404 | 7:c72b726c7dc9 | 30 | BMP280::BMP280(I2C &i2c_obj, char slave_adr) |
MACRUM | 0:ade9be832910 | 31 | : |
MACRUM | 0:ade9be832910 | 32 | i2c_p(NULL), |
MACRUM | 0:ade9be832910 | 33 | i2c(i2c_obj), |
charly | 8:d22ecbef9b90 | 34 | address(slave_adr<<1), |
MACRUM | 0:ade9be832910 | 35 | t_fine(0) |
MACRUM | 0:ade9be832910 | 36 | { |
MACRUM | 0:ade9be832910 | 37 | initialize(); |
MACRUM | 0:ade9be832910 | 38 | } |
MACRUM | 0:ade9be832910 | 39 | |
12104404 | 7:c72b726c7dc9 | 40 | BMP280::~BMP280() |
MACRUM | 0:ade9be832910 | 41 | { |
MACRUM | 0:ade9be832910 | 42 | if (NULL != i2c_p) |
MACRUM | 0:ade9be832910 | 43 | delete i2c_p; |
MACRUM | 0:ade9be832910 | 44 | } |
MACRUM | 0:ade9be832910 | 45 | |
12104404 | 7:c72b726c7dc9 | 46 | void BMP280::initialize() |
MACRUM | 0:ade9be832910 | 47 | { |
MACRUM | 0:ade9be832910 | 48 | char cmd[18]; |
MACRUM | 0:ade9be832910 | 49 | |
charly | 8:d22ecbef9b90 | 50 | //cmd[0] = 0xf2; // ctrl_hum |
charly | 8:d22ecbef9b90 | 51 | //cmd[1] = 0x01; // Humidity oversampling x1 |
charly | 8:d22ecbef9b90 | 52 | //i2c.write(address, cmd, 2); |
MACRUM | 0:ade9be832910 | 53 | |
MACRUM | 0:ade9be832910 | 54 | cmd[0] = 0xf4; // ctrl_meas |
charly | 8:d22ecbef9b90 | 55 | //cmd[1] = 0x27; // Temparature oversampling x1, Pressure oversampling x1, Normal mode |
charly | 8:d22ecbef9b90 | 56 | cmd[1] = 0b01010111; // Temparature oversampling x2 010, Pressure oversampling x16 101, Normal mode 11 |
MACRUM | 0:ade9be832910 | 57 | i2c.write(address, cmd, 2); |
MACRUM | 0:ade9be832910 | 58 | |
MACRUM | 0:ade9be832910 | 59 | cmd[0] = 0xf5; // config |
charly | 8:d22ecbef9b90 | 60 | cmd[1] = 0b10111100; // Standby 1000ms, Filter x16 |
MACRUM | 0:ade9be832910 | 61 | i2c.write(address, cmd, 2); |
MACRUM | 0:ade9be832910 | 62 | |
MACRUM | 0:ade9be832910 | 63 | cmd[0] = 0x88; // read dig_T regs |
MACRUM | 0:ade9be832910 | 64 | i2c.write(address, cmd, 1); |
MACRUM | 0:ade9be832910 | 65 | i2c.read(address, cmd, 6); |
MACRUM | 0:ade9be832910 | 66 | |
MACRUM | 0:ade9be832910 | 67 | dig_T1 = (cmd[1] << 8) | cmd[0]; |
MACRUM | 0:ade9be832910 | 68 | dig_T2 = (cmd[3] << 8) | cmd[2]; |
MACRUM | 0:ade9be832910 | 69 | dig_T3 = (cmd[5] << 8) | cmd[4]; |
MACRUM | 0:ade9be832910 | 70 | |
charly | 8:d22ecbef9b90 | 71 | DEBUG_PRINT("dig_T = 0x%x, 0x%x, 0x%x\n\r", dig_T1, dig_T2, dig_T3); |
charly | 8:d22ecbef9b90 | 72 | DEBUG_PRINT("dig_T = %d, %d, %d\n\r", dig_T1, dig_T2, dig_T3); |
MACRUM | 0:ade9be832910 | 73 | |
MACRUM | 0:ade9be832910 | 74 | cmd[0] = 0x8E; // read dig_P regs |
MACRUM | 0:ade9be832910 | 75 | i2c.write(address, cmd, 1); |
MACRUM | 0:ade9be832910 | 76 | i2c.read(address, cmd, 18); |
MACRUM | 0:ade9be832910 | 77 | |
MACRUM | 0:ade9be832910 | 78 | dig_P1 = (cmd[ 1] << 8) | cmd[ 0]; |
MACRUM | 0:ade9be832910 | 79 | dig_P2 = (cmd[ 3] << 8) | cmd[ 2]; |
MACRUM | 0:ade9be832910 | 80 | dig_P3 = (cmd[ 5] << 8) | cmd[ 4]; |
MACRUM | 0:ade9be832910 | 81 | dig_P4 = (cmd[ 7] << 8) | cmd[ 6]; |
MACRUM | 0:ade9be832910 | 82 | dig_P5 = (cmd[ 9] << 8) | cmd[ 8]; |
MACRUM | 0:ade9be832910 | 83 | dig_P6 = (cmd[11] << 8) | cmd[10]; |
MACRUM | 0:ade9be832910 | 84 | dig_P7 = (cmd[13] << 8) | cmd[12]; |
MACRUM | 0:ade9be832910 | 85 | dig_P8 = (cmd[15] << 8) | cmd[14]; |
MACRUM | 0:ade9be832910 | 86 | dig_P9 = (cmd[17] << 8) | cmd[16]; |
MACRUM | 0:ade9be832910 | 87 | |
MACRUM | 0:ade9be832910 | 88 | DEBUG_PRINT("dig_P = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_P1, dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9); |
MACRUM | 0:ade9be832910 | 89 | |
12104404 | 6:d03826fe1062 | 90 | /* cmd[0] = 0xA1; // read dig_H regs |
MACRUM | 0:ade9be832910 | 91 | i2c.write(address, cmd, 1); |
MACRUM | 2:c35f637c28ef | 92 | i2c.read(address, cmd, 1); |
MACRUM | 2:c35f637c28ef | 93 | cmd[1] = 0xE1; // read dig_H regs |
MACRUM | 2:c35f637c28ef | 94 | i2c.write(address, &cmd[1], 1); |
MACRUM | 2:c35f637c28ef | 95 | i2c.read(address, &cmd[1], 7); |
MACRUM | 2:c35f637c28ef | 96 | |
MACRUM | 0:ade9be832910 | 97 | dig_H1 = cmd[0]; |
MACRUM | 0:ade9be832910 | 98 | dig_H2 = (cmd[2] << 8) | cmd[1]; |
MACRUM | 0:ade9be832910 | 99 | dig_H3 = cmd[3]; |
MACRUM | 0:ade9be832910 | 100 | dig_H4 = (cmd[4] << 4) | (cmd[5] & 0x0f); |
MACRUM | 2:c35f637c28ef | 101 | dig_H5 = (cmd[6] << 4) | ((cmd[5]>>4) & 0x0f); |
MACRUM | 2:c35f637c28ef | 102 | dig_H6 = cmd[7]; |
MACRUM | 0:ade9be832910 | 103 | |
MACRUM | 0:ade9be832910 | 104 | DEBUG_PRINT("dig_H = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_H1, dig_H2, dig_H3, dig_H4, dig_H5, dig_H6); |
12104404 | 6:d03826fe1062 | 105 | */ |
MACRUM | 0:ade9be832910 | 106 | } |
MACRUM | 0:ade9be832910 | 107 | |
12104404 | 7:c72b726c7dc9 | 108 | float BMP280::getTemperature() |
MACRUM | 0:ade9be832910 | 109 | { |
charly | 8:d22ecbef9b90 | 110 | int32_t temp_raw; |
MACRUM | 0:ade9be832910 | 111 | float tempf; |
MACRUM | 0:ade9be832910 | 112 | char cmd[4]; |
MACRUM | 0:ade9be832910 | 113 | |
MACRUM | 0:ade9be832910 | 114 | cmd[0] = 0xfa; // temp_msb |
MACRUM | 0:ade9be832910 | 115 | i2c.write(address, cmd, 1); |
takafuminaka | 3:d4eb81284ea0 | 116 | i2c.read(address, &cmd[1], 3); |
MACRUM | 0:ade9be832910 | 117 | |
MACRUM | 0:ade9be832910 | 118 | temp_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4); |
charly | 8:d22ecbef9b90 | 119 | DEBUG_PRINT("\r\ntemp_raw:%d",temp_raw); |
MACRUM | 0:ade9be832910 | 120 | |
charly | 8:d22ecbef9b90 | 121 | int32_t temp1, temp2,temp; |
MACRUM | 0:ade9be832910 | 122 | |
charly | 8:d22ecbef9b90 | 123 | temp1 =((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11; |
charly | 8:d22ecbef9b90 | 124 | temp2 =(((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14; |
charly | 8:d22ecbef9b90 | 125 | DEBUG_PRINT(" temp1:%d temp2:%d",temp1, temp2); |
charly | 8:d22ecbef9b90 | 126 | t_fine = temp1+temp2; |
charly | 8:d22ecbef9b90 | 127 | DEBUG_PRINT(" t_fine:%d",t_fine); |
charly | 8:d22ecbef9b90 | 128 | temp = (t_fine * 5 + 128) >> 8; |
MACRUM | 0:ade9be832910 | 129 | tempf = (float)temp; |
charly | 8:d22ecbef9b90 | 130 | DEBUG_PRINT(" tempf:%f",tempf); |
charly | 8:d22ecbef9b90 | 131 | |
MACRUM | 0:ade9be832910 | 132 | return (tempf/100.0f); |
MACRUM | 0:ade9be832910 | 133 | } |
MACRUM | 0:ade9be832910 | 134 | |
12104404 | 7:c72b726c7dc9 | 135 | float BMP280::getPressure() |
MACRUM | 0:ade9be832910 | 136 | { |
MACRUM | 0:ade9be832910 | 137 | uint32_t press_raw; |
MACRUM | 0:ade9be832910 | 138 | float pressf; |
MACRUM | 0:ade9be832910 | 139 | char cmd[4]; |
MACRUM | 0:ade9be832910 | 140 | |
MACRUM | 0:ade9be832910 | 141 | cmd[0] = 0xf7; // press_msb |
MACRUM | 0:ade9be832910 | 142 | i2c.write(address, cmd, 1); |
takafuminaka | 3:d4eb81284ea0 | 143 | i2c.read(address, &cmd[1], 3); |
MACRUM | 0:ade9be832910 | 144 | |
MACRUM | 0:ade9be832910 | 145 | press_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4); |
MACRUM | 0:ade9be832910 | 146 | |
MACRUM | 0:ade9be832910 | 147 | int32_t var1, var2; |
MACRUM | 0:ade9be832910 | 148 | uint32_t press; |
MACRUM | 0:ade9be832910 | 149 | |
MACRUM | 0:ade9be832910 | 150 | var1 = (t_fine >> 1) - 64000; |
MACRUM | 0:ade9be832910 | 151 | var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6; |
MACRUM | 0:ade9be832910 | 152 | var2 = var2 + ((var1 * dig_P5) << 1); |
MACRUM | 0:ade9be832910 | 153 | var2 = (var2 >> 2) + (dig_P4 << 16); |
MACRUM | 0:ade9be832910 | 154 | var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18; |
MACRUM | 0:ade9be832910 | 155 | var1 = ((32768 + var1) * dig_P1) >> 15; |
MACRUM | 0:ade9be832910 | 156 | if (var1 == 0) { |
MACRUM | 0:ade9be832910 | 157 | return 0; |
MACRUM | 0:ade9be832910 | 158 | } |
MACRUM | 0:ade9be832910 | 159 | press = (((1048576 - press_raw) - (var2 >> 12))) * 3125; |
MACRUM | 0:ade9be832910 | 160 | if(press < 0x80000000) { |
MACRUM | 0:ade9be832910 | 161 | press = (press << 1) / var1; |
MACRUM | 0:ade9be832910 | 162 | } else { |
MACRUM | 0:ade9be832910 | 163 | press = (press / var1) * 2; |
MACRUM | 0:ade9be832910 | 164 | } |
MACRUM | 0:ade9be832910 | 165 | var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12; |
MACRUM | 0:ade9be832910 | 166 | var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13; |
MACRUM | 0:ade9be832910 | 167 | press = (press + ((var1 + var2 + dig_P7) >> 4)); |
MACRUM | 0:ade9be832910 | 168 | |
MACRUM | 0:ade9be832910 | 169 | pressf = (float)press; |
MACRUM | 0:ade9be832910 | 170 | return (pressf/100.0f); |
12104404 | 7:c72b726c7dc9 | 171 | } |