MPL3115A2 Fork
Fork of MPL3115A2 by
Diff: MPL3115A2.cpp
- Revision:
- 1:6fdcf150410e
- Parent:
- 0:752c9dbed2fa
- Child:
- 2:12223b4c88b1
--- a/MPL3115A2.cpp Sun Dec 13 08:57:16 2015 +0000 +++ b/MPL3115A2.cpp Thu May 18 06:30:44 2017 +0000 @@ -77,6 +77,9 @@ MPL3115A2::MPL3115A2(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr<<1) { // activate the peripheral + standby() ; + setCTRL_REG1( 0x38 ) ; /* oversample 32 */ + activate() ; } MPL3115A2::~MPL3115A2() { } @@ -100,40 +103,52 @@ /* * getAltitude returns the altitude in meters times 65536 */ -uint32_t MPL3115A2::getAltitude(void) +double MPL3115A2::getAltitude(void) { uint8_t tmp[3] ; - uint32_t data ; - + uint16_t sample_time ; + int32_t data ; + + standby() ; + modeAlt() ; + sample_time = getSampleTime() ; + activate() ; + oneShot() ; + wait_ms(sample_time) ; readRegs(MPL_OUT_P_MSB, tmp, 3) ; data = (tmp[0]<<24)|(tmp[1]<<16)|(tmp[2]<<8) ; - return( data ) ; + return( ((double)data)/65536.0 ) ; } /* * getPressure returns the pressure in Pascals times 64 */ -uint32_t MPL3115A2::getPressure(void) +double MPL3115A2::getPressure(void) { uint8_t tmp[3] ; uint32_t data ; - + uint16_t sample_time ; + + standby() ; + modeBar() ; + sample_time = getSampleTime() ; + wait_ms(sample_time) ; readRegs(MPL_OUT_P_MSB, tmp, 3) ; - data = (tmp[0]<<16)|(tmp[1]<<8)|(tmp[2]) ; - return( data ) ; + data = ((tmp[0]<<16)|(tmp[1]<<8)|(tmp[2])) >> 6 ; + return(((double)data) / 100.0 ) ; } /* * getTemperature returns the temperature in c-degree times 256 */ -uint16_t MPL3115A2::getTemperature(void) +double MPL3115A2::getTemperature(void) { uint8_t tmp[2] ; uint16_t data ; readRegs(MPL_OUT_T_MSB, tmp, 2) ; data = (tmp[0]<<8)|(tmp[1]) ; - return( data ) ; + return( ((double)data)/256.0 ) ; } /* @@ -478,7 +493,18 @@ writeRegs(tmp, 2) ; } -uint16_t MPL3115A2::OneShot(void) +void MPL3115A2::oneShot(void) +{ + uint8_t tmp[2] ; + tmp[0] = MPL_CTRL_REG1 ; + readRegs(tmp[0], &tmp[1], 1) ; + tmp[1] &= 0xFD ; /* clear OST */ + writeRegs(tmp, 2) ; + tmp[1] |= 0x02 ; /* set OST */ + writeRegs(tmp, 2) ; +} + +uint16_t MPL3115A2::getSampleTime(void) { uint8_t tmp[2] ; uint16_t sample_time = 6 ;