adafruit / Adafruit_BME280_Library

Arduino Library for BME280 sensors
Other
333 stars 304 forks source link

Burst reading from the same data sample, properly done #14

Closed mmehr2 closed 7 years ago

mmehr2 commented 8 years ago

The existing code runs separate burst reads for Temperature and Pressure/Humidity. This could lead to the values coming from different samples, if a new one became available in between the reads.

In order to fix this (see BME280 Datasheet section 4 and 4.1), I added a new private function based on read24() to read all the data registers at once. I allow the calling code to specify how many values need to be read (1, 2, or 3, in order). Then, I modified the calling methods for the compensation code so that it can be called separately. Finally, I rewrote the readX() code to call the new function. And I added a new public function readAll() to allow more than one value to be read from the same sample at once.

Since this is based only on the behavior described in the data sheet, it should run on all platforms that the original code ran on. In my tests, it adds 310 bytes of program storage when compiled for the Uno.

My editor (Atom) also seems to have trimmed some blanks or tabs on a few lines without my intention.

NOTE: The code in here should be compatible with PR#13 from @mozzbozz that is in progress. (See our discussions there.)

sramilli commented 8 years ago

Great! Thank you :) Would you please consider to do the same for the little brother BMP280? https://github.com/adafruit/Adafruit_BMP280_Library Best regards

mmehr2 commented 8 years ago

Thanks, @sramilli , and yes, I'm working on it, and the Unified version as well. Hope to post them soon. Regards, Mike

mmehr2 commented 7 years ago

Well I need to apologize for my eagerness. I have discovered a major error in this PR, in that I got the order of registers wrong in burst mode (is TPH but should be PTH), making the code just not work. I have the fixes ready, but can't provide tested code until I get an actual Arduino (I've been testing on an NXP LPC-1769 which is Cortex-M3 and using a different version of the library written in C). I will close this PR and open a new one when I can provide the tested version. Plus I've only been testing SPI mode until now, and I really should get I2C tested as well. If anyone wants to help, let me know.

The same error is in my proposed PR for the BMP280 library, but I provided two PR's there so it might take me a bit longer to sort that one out.

If it's any consolation, the new code works on the NXP board, not sure why I never noticed the old code was getting bogus results for pressure and altitude. Reading non-existent registers must return consistent results from the Bosch sensors, at any rate. I need to write a better test program too, which I hope to include somehow. Back to the drawing board!