85 lines
1.9 KiB
C++
85 lines
1.9 KiB
C++
|
||
#include "BME280.h"
|
||
|
||
// I2C read 1 byte
|
||
uint8_t readRegister(uint8_t reg) {
|
||
Wire.beginTransmission(BME280_ADDRESS);
|
||
Wire.write(reg);
|
||
Wire.endTransmission();
|
||
Wire.requestFrom(BME280_ADDRESS, 1);
|
||
return Wire.read();
|
||
}
|
||
|
||
// I2C write 1 byte
|
||
void writeRegister(uint8_t reg, uint8_t value) {
|
||
Wire.beginTransmission(BME280_ADDRESS);
|
||
Wire.write(reg);
|
||
Wire.write(value);
|
||
Wire.endTransmission();
|
||
}
|
||
|
||
// Public functions
|
||
uint8_t BME280_GetID() {
|
||
return readRegister(BME280_REG_ID);
|
||
}
|
||
|
||
void BME280_Reset() {
|
||
writeRegister(BME280_REG_RESET, BME280_RESET_CMD);
|
||
}
|
||
|
||
uint8_t BME280_CtrlHum() {
|
||
return readRegister(BME280_REG_CTRL_HUM);
|
||
}
|
||
|
||
void BME280_CtrlHum(uint8_t bitpattern) {
|
||
writeRegister(BME280_REG_CTRL_HUM, bitpattern);
|
||
}
|
||
|
||
uint8_t BME280_CtrlMeas() {
|
||
return readRegister(BME280_REG_CTRL_MEAS);
|
||
}
|
||
|
||
void BME280_CtrlMeas(uint8_t bitpattern) {
|
||
writeRegister(BME280_REG_CTRL_MEAS, bitpattern);
|
||
}
|
||
|
||
long BME280_ReadTemperature() {
|
||
Wire.beginTransmission(BME280_ADDRESS);
|
||
Wire.write(BME280_REG_TEMP_MSB);
|
||
Wire.endTransmission();
|
||
Wire.requestFrom(BME280_ADDRESS, 3);
|
||
|
||
long msb = Wire.read();
|
||
long lsb = Wire.read();
|
||
long xlsb = Wire.read();
|
||
long adc_T = ((msb << 16) | (lsb << 8) | xlsb) >> 4;
|
||
|
||
return adc_T; // Raw value (compensation needed)
|
||
}
|
||
|
||
int BME280_ReadHumidity() {
|
||
Wire.beginTransmission(BME280_ADDRESS);
|
||
Wire.write(BME280_REG_HUM_MSB);
|
||
Wire.endTransmission();
|
||
Wire.requestFrom(BME280_ADDRESS, 2);
|
||
|
||
int msb = Wire.read();
|
||
int lsb = Wire.read();
|
||
int adc_H = (msb << 8) | lsb;
|
||
|
||
return adc_H; // Raw value
|
||
}
|
||
|
||
long BME280_ReadPressure() {
|
||
Wire.beginTransmission(BME280_ADDRESS);
|
||
Wire.write(BME280_REG_PRESS_MSB);
|
||
Wire.endTransmission();
|
||
Wire.requestFrom(BME280_ADDRESS, 3);
|
||
|
||
long msb = Wire.read();
|
||
long lsb = Wire.read();
|
||
long xlsb = Wire.read();
|
||
long adc_P = ((msb << 16) | (lsb << 8) | xlsb) >> 4;
|
||
|
||
return adc_P; // Raw value
|
||
} |