Professional Documents
Culture Documents
BME280 V 1
BME280 V 1
cpp/////////////////////////////////////////
#include "mbed.h"
#include "BME280.h"
BME280::~BME280()
{
if (NULL != i2c_p)
delete i2c_p;
}
void BME280::initialize()
{
char cmd[18];
cmd[0] = 0x88; // read dig_T regs Table 16: Compensation parameter storage,
naming and data type
i2c.write(address, cmd, 1);
i2c.read(address, cmd, 6);
cmd[0] = 0x8E; // read dig_P regs Table 16: Compensation parameter storage,
naming and data type
i2c.write(address, cmd, 1);
i2c.read(address, cmd, 18);
cmd[0] = 0xA1; // read dig_H regs Table 16: Compensation parameter storage,
naming and data type
i2c.write(address, cmd, 1);
i2c.read(address, cmd, 1);
cmd[1] = 0xE1; // read dig_H regs Table 16: Compensation parameter storage,
naming and data type
i2c.write(address, &cmd[1], 1);
i2c.read(address, &cmd[1], 7);
dig_H1 = cmd[0];
dig_H2 = (cmd[2] << 8) | cmd[1];
dig_H3 = cmd[3];
dig_H4 = (cmd[4] << 4) | (cmd[5] & 0x0f);
dig_H5 = (cmd[6] << 4) | ((cmd[5]>>4) & 0x0f);
dig_H6 = cmd[7];
float BME280::ReadTemperature()
{
uint32_t adc_T;
float T;
char cmd[4];
float BME280::ReadPressure()
{
uint32_t p;
float pressf;
char cmd[4];
}
press = (((1048576 - p) - (var2 >> 12))) * 3125;
if(press < 0x80000000) {
press = (press << 1) / var1;
} else {
press = (press / var1) * 2;
}
var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >>
12;
var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13;
press = (press + ((var1 + var2 + dig_P7) >> 4));
pressf = (float)press;
return (pressf/256); //// Output value of �24674867� represents 24674867/256 =
96386.2 Pa = 963.862 hPa
}
float BME280::ReadHumidity()
{
uint32_t hum_raw;
float humf;
char cmd[4];
int32_t v_x1;
return (humf/1024);
// Returns humidity in %RH as unsigned 32 bit integer in Q22.10 format (22
integer and 10 fractional bits).
// Output value of �47445� represents 47445/1024 = 46.333 %RH
}
////////////////////////////////////////////////////h//////////////////////////////
//////////////////////////////////
#ifndef MBED_BME280_H
#define MBED_BME280_H
#include "mbed.h"
class BME280
{
public:
BME280(PinName sda, PinName sck, char slave_adr = DEFAULT_SLAVE_ADDRESS);
virtual ~BME280();
void initialize(void);
float ReadTemperature(void);
float ReadPressure(void);
float ReadHumidity(void);
private:
I2C &i2c;
I2C *i2c_p;
char address;
uint16_t dig_T1;
int16_t dig_T2, dig_T3;
uint16_t dig_P1;
int16_t dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9;
uint16_t dig_H1, dig_H3;
int16_t dig_H2, dig_H4, dig_H5, dig_H6;
int32_t t_fine;
};
#endif // MBED_BME280_H