Created
September 29, 2019 12:49
-
-
Save matpalm/85ced5c6899cf20a282168e7a8e34329 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// set one shot reading | |
register_data = MPL3115A2_CTRL_REG1_OST | | |
MPL3115A2_CTRL_REG1_BAR; | |
Wire.beginTransmission(MPL3115A2_ADDRESS); | |
Wire.write(MPL3115A2_CTRL_REG1); | |
Wire.write(register_data); | |
Wire.endTransmission(true); | |
// wait for one shot bit to auto clear | |
bool ready = false; | |
while(!ready) { | |
Wire.beginTransmission(MPL3115A2_ADDRESS); | |
Wire.write(MPL3115A2_CTRL_REG1); | |
Wire.endTransmission(false); | |
Wire.requestFrom(MPL3115A2_ADDRESS, 1, true); | |
register_data = Wire.read(); | |
ready = !(register_data & MPL3115A2_CTRL_REG1_OST); | |
} | |
// read 3 bytes for barometer + 2 bytes for temp | |
Wire.beginTransmission(MPL3115A2_ADDRESS); | |
Wire.write(0x01); | |
Wire.endTransmission(false); | |
Wire.requestFrom(MPL3115A2_ADDRESS, 5, true); | |
uint32_t _pressure = Wire.read(); // OUT_P_MSB | |
_pressure <<= 8; | |
_pressure |= Wire.read(); // OUT_P_CSB | |
_pressure <<= 8; | |
_pressure |= Wire.read(); // OUT_P_LSB | |
_pressure >>= 4; | |
float pressure = _pressure; | |
pressure /= 4.0; | |
int16_t _temp = Wire.read(); // OUT_T_MSB | |
_temp <<= 8; | |
_temp |= Wire.read(); // OUT_T_LSB | |
_temp >>= 4; | |
if (_temp & 0x800) { | |
_temp |= 0xF000; | |
} | |
float temp = _temp; | |
temp /= 16; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment