Browse Source

hw SPI for bme

main
Hendrik Langer 7 years ago
parent
commit
9e5b331844
  1. 34
      src/BME280.cpp

34
src/BME280.cpp

@ -10,24 +10,26 @@ BME280::BME280() {
} }
bool BME280::begin(void) { bool BME280::begin(void) {
// SPI.begin(BME_SCK, BME_MISO, BME_MOSI, BME_CS); SPI.begin(BME_SCK, BME_MISO, BME_MOSI, BME_CS);
pinMode(18, OUTPUT); pinMode(18, OUTPUT);
digitalWrite(18, HIGH); // disable LoRa_CS digitalWrite(18, HIGH); // disable LoRa_CS
pinMode(BME_CS, OUTPUT); pinMode(BME_CS, OUTPUT);
digitalWrite(BME_CS, LOW); // enable bme280 CS digitalWrite(BME_CS, LOW); // enable bme280 CS
delay(100); delay(100);
bme = new Adafruit_BME280(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI // bme = new Adafruit_BME280(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
valid = bme->begin(); bme = new Adafruit_BME280(BME_CS); // hardware SPI
if (!valid) bme->init();
if (!valid) return false;
bme->setSampling(Adafruit_BME280::sensor_mode::MODE_NORMAL, bme->setSampling(Adafruit_BME280::sensor_mode::MODE_NORMAL,
Adafruit_BME280::sensor_sampling::SAMPLING_X16, Adafruit_BME280::sensor_sampling::SAMPLING_X8,
Adafruit_BME280::sensor_sampling::SAMPLING_X16, Adafruit_BME280::sensor_sampling::SAMPLING_X8,
Adafruit_BME280::sensor_sampling::SAMPLING_X16, Adafruit_BME280::sensor_sampling::SAMPLING_X8,
Adafruit_BME280::sensor_filter::FILTER_OFF, Adafruit_BME280::sensor_filter::FILTER_OFF,
Adafruit_BME280::standby_duration::STANDBY_MS_250); Adafruit_BME280::standby_duration::STANDBY_MS_250);
valid = bme->begin();
if (!valid) bme->init();
if (!valid) return false;
return true; return true;
} }
@ -44,20 +46,28 @@ bool BME280::reinit() {
pinMode(18, OUTPUT); pinMode(18, OUTPUT);
digitalWrite(18, HIGH); // disable LoRa_CS digitalWrite(18, HIGH); // disable LoRa_CS
pinMode(BME_CS, OUTPUT); pinMode(BME_CS, OUTPUT);
digitalWrite(BME_CS, LOW); // enable bme280 CS // digitalWrite(BME_CS, LOW); // enable bme280 CS
int retries = 0; valid = false; int retries = 0; valid = false;
while (!valid) { while (!valid) {
if (retries > 9) break; if (retries > 9) break;
/* reset chip */
Serial.println("resetting BME280");
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
digitalWrite(BME_CS, LOW);
constexpr byte BME280_REGISTER_SOFTRESET = 0xE0;
SPI.transfer(BME280_REGISTER_SOFTRESET & ~0x80);
SPI.transfer(0xB6);
digitalWrite(BME_CS, HIGH);
SPI.endTransaction(); // release the SPI bus
delay(300); // wait for chip to wake up
valid = bme->init(); valid = bme->init();
retries++; retries++;
delay(100);
float p = bme->readPressure() / 100.0F; float p = bme->readPressure() / 100.0F;
if (p < 300 || p > 1100) valid = false; if (p < 300 || p > 1100) valid = false;
// constexpr byte BME280_REGISTER_SOFTRESET = 0xE0;
// bme->write8(BME280_REGISTER_SOFTRESET, 0xB6);
} }
if (!valid) { if (!valid) {
Serial.println("Could not find a valid BME280 sensor, check wiring!"); Serial.println("Could not find a valid BME280 sensor, check wiring!");

Loading…
Cancel
Save