GroupZ - 05012018 1511

Committer:
mslade
Date:
Tue Jan 09 15:08:58 2018 +0000
Revision:
3:da9b27c64089
Parent:
1:84581acd1333
full version;

Who changed what in which revision?

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