STMicroelectronics LPS331AP, LPS25H SPI Library. This library is base on https://developer.mbed.org/users/nyamfg/code/LPS331_I2C/
Dependents: LPS331_SPI_Test main_SPC
Fork of LPS331_I2C by
Diff: LPS331_I2C.cpp
- Revision:
- 1:b7d3d6e82049
- Parent:
- 0:3fd57444bc65
--- a/LPS331_I2C.cpp Sun Oct 20 15:22:55 2013 +0000 +++ b/LPS331_I2C.cpp Mon Oct 21 16:39:58 2013 +0000 @@ -65,10 +65,14 @@ float LPS331_I2C::getPressure() { + char data[3]; float pressure = 0; - pressure += _read(0x28); - pressure += _read(0x29) << 8; - pressure += _read(0x2a) << 16; + + _read_multibyte(0x28, data, 3); + + pressure = data[0]; + pressure += data[1] << 8; + pressure += data[2] << 16; pressure /= 4096.0; return pressure; @@ -76,9 +80,13 @@ float LPS331_I2C::getTemperature() { + char data[2]; short temp = 0; - temp = _read(0x2b); - temp |= _read(0x2c) << 8; + + _read_multibyte(0x2b, data, 2); + + temp = data[0]; + temp |= data[1] << 8; return (float)(42.5 + temp / 480.0); } @@ -110,3 +118,19 @@ return result; } +void LPS331_I2C::_read_multibyte(char startsubaddress, char* data, char count) +{ + _i2c.start(); + _i2c.write(_address); + _i2c.write(startsubaddress | 0x80); + + _i2c.start(); + _i2c.write(_address | 1); + + for(int i = 0; i < count; i++) { + data[i] = _i2c.read((i == count - 1) ? 0 : 1); + } + + _i2c.stop(); +} +