Updated

Dependents:   PROJECTTEST

Committer:
Swabey89
Date:
Sat Jan 05 15:03:12 2019 +0000
Revision:
0:68909f7f98bc
Updated

Who changed what in which revision?

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