BMP 280 library files

Dependents:   ELEC350_Project2

Committer:
Swabey89
Date:
Sun Nov 04 23:12:04 2018 +0000
Revision:
0:eef68cb8c174
BMP280 library files added

Who changed what in which revision?

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