Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/I2C_DeltaAltitude/I2C_DeltaAltitude.ino
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void setup()
Serial.print("Starting BME280... result of .begin(): 0x");
delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up.
//Calling .begin() causes the settings to be loaded
Serial.println(mySensor.begin(), HEX);
Serial.println(mySensor.begin(), HEX); //mySensor.begin(&Wire), use &Wire1, &Wire2 for different Wire Busses

Serial.println();

Expand Down
2 changes: 1 addition & 1 deletion examples/I2C_ReadAllData/I2C_ReadAllData.ino
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void setup()

//Calling .begin() causes the settings to be loaded
delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up.
Serial.println(mySensor.begin(), HEX);
Serial.println(mySensor.begin(), HEX); //mySensor.begin(&Wire), use &Wire1, &Wire2 for different Wire Busses

Serial.print("Displaying ID, reset and ctrl regs\n");

Expand Down
4 changes: 2 additions & 2 deletions examples/I2C_and_SPI_Multisensor/I2C_and_SPI_Multisensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ void setup()
delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up.
//Calling .begin() causes the settings to be loaded
Serial.print("Sensor A: 0x");
Serial.println(mySensorA.begin(), HEX);
Serial.println(mySensorA.begin(), HEX); //mySensor.begin(&Wire), use &Wire1, &Wire2 for different Wire Busses
Serial.print("Sensor B: 0x");
Serial.println(mySensorB.begin(), HEX);
Serial.println(mySensorB.begin(), HEX); //SensorB is in SPI Mode


}
Expand Down
59 changes: 34 additions & 25 deletions src/SparkFunBME280.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ May 20, 2015
https://github.com/sparkfun/BME280_Breakout

Resources:
Uses Wire.h for i2c operation
Uses wire -> h for i2c operation
Uses SPI.h for SPI operation

Development environment specifics:
Expand All @@ -24,7 +24,7 @@ Distributed as-is; no warranty is given.
#include "stdint.h"
#include <math.h>

#include "Wire.h"
#include "wire -> h"
#include "SPI.h"

//****************************************************************************//
Expand Down Expand Up @@ -61,15 +61,18 @@ BME280::BME280( void )
// configure before calling .begin();
//
//****************************************************************************//
uint8_t BME280::begin()
uint8_t BME280::begin(TwoWire *theWire)
{
//Set wire bus to be used if in i2c mode, will default to Wire if not specified
wire = theWire;

//Check the settings structure values to determine how to setup the device
uint8_t dataToWrite = 0; //Temporary variable
switch (settings.commInterface)
{

case I2C_MODE:
Wire.begin();
wire -> begin();
break;

case SPI_MODE:
Expand Down Expand Up @@ -123,7 +126,7 @@ uint8_t BME280::begin()
calibration.dig_H3 = ((uint8_t)(readRegister(BME280_DIG_H3_REG)));
calibration.dig_H4 = ((int16_t)((readRegister(BME280_DIG_H4_MSB_REG) << 4) + (readRegister(BME280_DIG_H4_LSB_REG) & 0x0F)));
calibration.dig_H5 = ((int16_t)((readRegister(BME280_DIG_H5_MSB_REG) << 4) + ((readRegister(BME280_DIG_H4_LSB_REG) >> 4) & 0x0F)));
calibration.dig_H6 = ((uint8_t)readRegister(BME280_DIG_H6_REG));
calibration.dig_H6 = ((int8_t)readRegister(BME280_DIG_H6_REG));

//Set the oversampling control words.
//config will only be writeable in sleep mode, so first insure that.
Expand Down Expand Up @@ -168,7 +171,9 @@ float BME280::readFloatPressure( void )

// Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24 integer bits and 8 fractional bits).
// Output value of “24674867” represents 24674867/256 = 96386.2 Pa = 963.862 hPa
int32_t adc_P = ((uint32_t)readRegister(BME280_PRESSURE_MSB_REG) << 12) | ((uint32_t)readRegister(BME280_PRESSURE_LSB_REG) << 4) | ((readRegister(BME280_PRESSURE_XLSB_REG) >> 4) & 0x0F);
uint8_t buffer[3];
readRegisterRegion(buffer, BME280_PRESSURE_MSB_REG, 3);
int32_t adc_P = ((uint32_t)buffer[0] << 12) | ((uint32_t)buffer[1] << 4) | ((buffer[2] >> 4) & 0x0F);

int64_t var1, var2, p_acc;
var1 = ((int64_t)t_fine) - 128000;
Expand Down Expand Up @@ -219,7 +224,9 @@ float BME280::readFloatHumidity( void )

// 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
int32_t adc_H = ((uint32_t)readRegister(BME280_HUMIDITY_MSB_REG) << 8) | ((uint32_t)readRegister(BME280_HUMIDITY_LSB_REG));
uint8_t buffer[2];
readRegisterRegion(buffer, BME280_HUMIDITY_MSB_REG, 2);
int32_t adc_H = ((uint32_t)buffer[0] << 8) | ((uint32_t)buffer[1]);

int32_t var1;
var1 = (t_fine - ((int32_t)76800));
Expand Down Expand Up @@ -248,7 +255,9 @@ float BME280::readTempC( void )
// t_fine carries fine temperature as global value

//get the reading (adc_T);
int32_t adc_T = ((uint32_t)readRegister(BME280_TEMPERATURE_MSB_REG) << 12) | ((uint32_t)readRegister(BME280_TEMPERATURE_LSB_REG) << 4) | ((readRegister(BME280_TEMPERATURE_XLSB_REG) >> 4) & 0x0F);
uint8_t buffer[3];
readRegisterRegion(buffer, BME280_TEMPERATURE_MSB_REG, 3);
int32_t adc_T = ((uint32_t)buffer[0] << 12) | ((uint32_t)buffer[1] << 4) | ((buffer[2] >> 4) & 0x0F);

//By datasheet, calibrate
int64_t var1, var2;
Expand Down Expand Up @@ -287,15 +296,15 @@ void BME280::readRegisterRegion(uint8_t *outputPointer , uint8_t offset, uint8_t
{

case I2C_MODE:
Wire.beginTransmission(settings.I2CAddress);
Wire.write(offset);
Wire.endTransmission();
wire -> beginTransmission(settings.I2CAddress);
wire -> write(offset);
wire -> endTransmission();

// request bytes from slave device
Wire.requestFrom(settings.I2CAddress, length);
while ( (Wire.available()) && (i < length)) // slave may send less than requested
wire -> requestFrom(settings.I2CAddress, length);
while ( (wire -> available()) && (i < length)) // slave may send less than requested
{
c = Wire.read(); // receive a byte as character
c = wire -> read(); // receive a byte as character
*outputPointer = c;
outputPointer++;
i++;
Expand Down Expand Up @@ -332,14 +341,14 @@ uint8_t BME280::readRegister(uint8_t offset)
switch (settings.commInterface) {

case I2C_MODE:
Wire.beginTransmission(settings.I2CAddress);
Wire.write(offset);
Wire.endTransmission();
wire -> beginTransmission(settings.I2CAddress);
wire -> write(offset);
wire -> endTransmission();

Wire.requestFrom(settings.I2CAddress, numBytes);
while ( Wire.available() ) // slave may send less than requested
wire -> requestFrom(settings.I2CAddress, numBytes);
while ( wire -> available() ) // slave may send less than requested
{
result = Wire.read(); // receive a byte as a proper uint8_t
result = wire -> read(); // receive a byte as a proper uint8_t
}
break;

Expand Down Expand Up @@ -375,10 +384,10 @@ void BME280::writeRegister(uint8_t offset, uint8_t dataToWrite)
{
case I2C_MODE:
//Write the byte
Wire.beginTransmission(settings.I2CAddress);
Wire.write(offset);
Wire.write(dataToWrite);
Wire.endTransmission();
wire -> beginTransmission(settings.I2CAddress);
wire -> write(offset);
wire -> write(dataToWrite);
wire -> endTransmission();
break;

case SPI_MODE:
Expand All @@ -396,4 +405,4 @@ void BME280::writeRegister(uint8_t offset, uint8_t dataToWrite)
default:
break;
}
}
}
9 changes: 6 additions & 3 deletions src/SparkFunBME280.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Distributed as-is; no warranty is given.
#define __BME280_H__

#include "stdint.h"
#include "Wire.h"

#define I2C_MODE 0
#define SPI_MODE 1
Expand Down Expand Up @@ -129,7 +130,7 @@ struct SensorCalibration
uint8_t dig_H3;
int16_t dig_H4;
int16_t dig_H5;
uint8_t dig_H6;
int8_t dig_H6;

};

Expand All @@ -150,7 +151,7 @@ class BME280

//Call to apply SensorSettings.
//This also gets the SensorCalibration constants
uint8_t begin( void );
uint8_t begin(TwoWire *theWire = &Wire);

//Software reset routine
void reset( void );
Expand Down Expand Up @@ -178,7 +179,9 @@ class BME280
int16_t readRegisterInt16( uint8_t offset );
//Writes a byte;
void writeRegister(uint8_t, uint8_t);


private:
TwoWire *wire;
};


Expand Down