Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- BMP1810 by Bosch
- i2c address 0x77
- oss (oversampling setting) :
- 0 = Ultra low power maximum conversion time: 4.5ms
- 1 = Standard maximum conversion time: 7.5ms
- 2 = High resolution maximum conversion time: 13.5ms
- 3 = Ultra high resolution maximum conversion time: 25.5ms
- * oss is always 0 for temperature measurements
- */
- #define BMP180_ADDRESS 0x77 // I2C address of the BMP180
- #define AC1 (int16_t)7819 // EEPROM registers 0xAA,0xAB
- #define AC2 (int16_t)-997 // EEPROM registers 0xAC,0xAD
- #define AC3 (int16_t)-14468 // EEPROM registers 0xAE,0xAF
- #define AC4 (uint16_t)33992 // EEPROM registers 0xB0,0xB1
- #define AC5 (uint16_t)25359 // EEPROM registers 0xB2,0xB3
- #define AC6 (uint16_t)15534 // EEPROM registers 0xB4,0xB5
- #define B1 (int16_t)6515 // EEPROM registers 0xB6,0xB7
- #define B2 (int16_t)32 // EEPROM registers 0xB8,0xB9
- #define MB (int16_t)-32768 // EEPROM registers 0xBA,0xBB
- #define MC (int16_t)-11786 // EEPROM registers 0xBC,0xBD
- #define MD (int16_t)2601 // EEPROM registers 0xBE,0xBF
- #define oss 0 //Oversampling setting
- long temp_raw;
- long temp_calc;
- float temperature;
- long pressure_raw;
- long pressure_calc;
- float pressure;
- void writeI2Cregister(uint8_t address, uint8_t register_number, uint8_t newRegisterState) {
- Wire.beginTransmission(address);
- Wire.write(register_number);
- Wire.write(newRegisterState);
- Wire.endTransmission(true);
- }
- void BMP180_readEEPROM() {
- // Read factory calibration values from EEPROM
- Wire.beginTransmission(BMP180_ADDRESS);
- Wire.write(0xAA);
- Wire.endTransmission(false);
- Wire.requestFrom(BMP180_ADDRESS, 22, true);
- int16_t AC1 = Wire.read() << 8 | Wire.read();
- int16_t AC2 = Wire.read() << 8 | Wire.read();
- int16_t AC3 = Wire.read() << 8 | Wire.read();
- uint16_t AC4 = Wire.read() << 8 | Wire.read();
- uint16_t AC5 = Wire.read() << 8 | Wire.read();
- uint16_t AC6 = Wire.read() << 8 | Wire.read();
- int16_t B1 = Wire.read() << 8 | Wire.read();
- int16_t B2 = Wire.read() << 8 | Wire.read();
- int16_t MB = Wire.read() << 8 | Wire.read();
- int16_t MC = Wire.read() << 8 | Wire.read();
- int16_t MD = Wire.read() << 8 | Wire.read();
- }
- long BMP180_read() {
- // Start new temperature measurement
- writeI2Cregister(BMP180_ADDRESS, 0xF4, 0 << 6 | 1 << 5 | 0b01110);
- delay(5); // Max conversion time is 4.5ms
- // Read temperature
- Wire.beginTransmission(BMP180_ADDRESS);
- Wire.write(0xF6);
- Wire.endTransmission(false);
- Wire.requestFrom(BMP180_ADDRESS, 2, true);
- temp_raw = Wire.read() << 8 | Wire.read();
- // Start new pressure measurement
- writeI2Cregister(BMP180_ADDRESS, 0xF4, oss << 6 | 1 << 5 | 0b10100);
- delay(5); // Max conversion time is 4.5ms (oss = 0)
- // Read pressure
- Wire.beginTransmission(BMP180_ADDRESS);
- Wire.write(0xF6);
- Wire.endTransmission(false);
- Wire.requestFrom(BMP180_ADDRESS, 3, true);
- long msb = Wire.read();
- long lsb = Wire.read();
- long xlsb = Wire.read();
- pressure_raw = (msb << 16 | lsb << 8 | xlsb) >> (8 - oss);
- }
- void BMP180_convert() {
- long X1;
- long X2;
- long X3;
- long B3;
- unsigned long B4;
- long B5;
- long B6;
- unsigned long B7;
- // Convert temperature
- X1 = (temp_raw - AC6) * AC5 / 32768;
- X2 = MC * (long)2048 / (X1 + MD);
- B5 = X1 + X2;
- temp_calc = (B5 + 8) / 16;
- temperature = temp_calc / 10.0;
- // Convert pressure
- B6 = B5 - 4000;
- X1 = (B2 * (B6 * B6 / 4096)) / 2048;
- X2 = AC2 * B6 / 2048;
- X3 = X1 + X2;
- B3 = ((((long)AC1 * 4 + X3) << oss) + 2) / 4;
- X1 = AC3 * B6 / 8192;
- X2 = (B1 * (B6 * B6 / 4096)) / 65536;
- X3 = ((X1 + X2) + 2) / 4;
- B4 = AC4 * (unsigned long)(X3 + 32768) / 32768;
- B7 = ((unsigned long)pressure_raw - B3) * (50000 >> oss);
- if (B7 < 0x80000000) {
- pressure_calc = (B7 * 2) / B4;
- }
- else {
- pressure_calc = (B7 / B4) * 2;
- }
- X1 = (pressure_calc / 256) * (pressure_calc / 256);
- X1 = (X1 * 3038) / 65536;
- X2 = (-7357 * pressure_calc) / 65536;
- pressure_calc = pressure_calc + (X1 + X2 + 3791) / 16;
- pressure = pressure_calc / 100.0f;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement