MPL3115A2 driver
Dependents: lmic_NAmote_GPS_tjm lmic_NAmote_GPS_tjm Senet NAMote scpi_sx127x ... more
Revision 2:0eb8b0ad292b, committed 2015-08-31
- Comitter:
- dudmuck
- Date:
- Mon Aug 31 21:37:41 2015 +0000
- Parent:
- 1:3cd1f21925e8
- Commit message:
- correct altitude reading
Changed in this revision
mpl3115a2.cpp | Show annotated file Show diff for this revision Revisions of this file |
mpl3115a2.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 3cd1f21925e8 -r 0eb8b0ad292b mpl3115a2.cpp --- a/mpl3115a2.cpp Fri May 08 01:32:04 2015 +0000 +++ b/mpl3115a2.cpp Mon Aug 31 21:37:41 2015 +0000 @@ -13,46 +13,59 @@ void MPL3115A2::init() { - uint8_t regVal; - //MPL3115Reset( ); - write(CTRL_REG1, 4); + ctrl_reg1.octet = 0; + ctrl_reg1.bits.RST = 1; + write(CTRL_REG1, /*4*/ ctrl_reg1.octet); wait(0.05); do { // Wait for the RST bit to clear - regVal = read( CTRL_REG1); - //printf("ctrl_reg1:%02x\n", regVal); - } while( regVal ); + wait(0.01); + ctrl_reg1.octet = read(CTRL_REG1); + } while (ctrl_reg1.octet); write( PT_DATA_CFG_REG, 0x07 ); // Enable data flags write( CTRL_REG3, 0x11 ); // Open drain, active low interrupts write( CTRL_REG4, 0x80 ); // Enable DRDY interrupt write( CTRL_REG5, 0x00 ); // DRDY interrupt routed to INT2 - PTD3 - write( CTRL_REG1, 0xA9 ); // Active altitude mode, OSR = 32 - - write( OFF_H_REG, 0xB0 ); // Altitude data offset + + ctrl_reg1.bits.ALT = 1; // altitude mode + ctrl_reg1.bits.OS = 5; // OSR = 32 + ctrl_reg1.bits.SBYB = 1; // Active + write(CTRL_REG1, ctrl_reg1.octet); SetModeActive( ); } +void MPL3115A2::setOSR(uint8_t osr) +{ + ctrl_reg1.bits.OS = osr; + write(CTRL_REG1, ctrl_reg1.octet); +} + +uint8_t MPL3115A2::getOSR(void) +{ + ctrl_reg1.octet = read(CTRL_REG1); + return ctrl_reg1.bits.OS; +} + bool MPL3115A2::GetModeActive( ) { - return read(CTRL_REG1) & 1; + ctrl_reg1.octet = read(CTRL_REG1); + return ctrl_reg1.bits.SBYB; } void MPL3115A2::SetModeActive( ) { - uint8_t val = read(CTRL_REG1); - val |= 1; // set SBYB - write(CTRL_REG1, val); + ctrl_reg1.bits.SBYB = 1; + write(CTRL_REG1, ctrl_reg1.octet); } void MPL3115A2::SetModeStandby( ) { - uint8_t val = read(CTRL_REG1); - val &= ~1; // clear SBYB - write(CTRL_REG1, val); + ctrl_reg1.bits.SBYB = 0; + write(CTRL_REG1, ctrl_reg1.octet); } void MPL3115A2::write(uint8_t a, uint8_t d) @@ -85,6 +98,29 @@ return cmd[0]; } +float MPL3115A2::ReadBarometer(void) +{ + uint32_t pasc; + volatile uint8_t stat; + + SetModeBarometer(); + ToggleOneShot( ); + + stat = read(STATUS_REG); + while( (stat & 0x04) != 0x04 ) { + wait(0.01); + stat = read(STATUS_REG); + } + + pasc = read(OUT_P_MSB_REG); + pasc <<= 8; + pasc |= read(OUT_P_CSB_REG); + pasc <<= 8; + pasc |= read(OUT_P_LSB_REG); + + return pasc / 64.0; +} + float MPL3115A2::ReadAltitude( void ) { uint8_t counter = 0; @@ -142,30 +178,33 @@ void MPL3115A2::SetModeAltimeter( void ) { - uint8_t val; - SetModeStandby( ); - val = read( CTRL_REG1); - val |= 0x80; //Set ALT bit - write( CTRL_REG1, val ); + ctrl_reg1.bits.ALT = 1; + write(CTRL_REG1, ctrl_reg1.octet); + + SetModeActive( ); +} - SetModeActive( ); +void MPL3115A2::SetModeBarometer(void) +{ + SetModeStandby( ); + + ctrl_reg1.bits.ALT = 0; + write(CTRL_REG1, ctrl_reg1.octet); + + SetModeActive( ); } void MPL3115A2::ToggleOneShot( void ) { - uint8_t val; - SetModeStandby( ); - - val = read( CTRL_REG1); - val &= ~(0x02); //Clear OST bit - write( CTRL_REG1, val ); - - val = read( CTRL_REG1); - val |= 0x02; //Set OST bit - write( CTRL_REG1, val ); + + ctrl_reg1.bits.OST = 0; + write(CTRL_REG1, ctrl_reg1.octet); + + ctrl_reg1.bits.OST = 1; + write(CTRL_REG1, ctrl_reg1.octet); SetModeActive( ); }
diff -r 3cd1f21925e8 -r 0eb8b0ad292b mpl3115a2.h --- a/mpl3115a2.h Fri May 08 01:32:04 2015 +0000 +++ b/mpl3115a2.h Mon Aug 31 21:37:41 2015 +0000 @@ -63,6 +63,19 @@ #define OFF_T_REG 0x2C // Temperature data offset #define OFF_H_REG 0x2D // Altitude data offset + +typedef union { + struct { // at 0x26 + uint8_t SBYB : 1; // 0 0==standby, 1=active + uint8_t OST : 1; // 1 initiate measurement now + uint8_t RST : 1; // 2 software reset + uint8_t OS : 3; // 3,4,5 oversampling ratio + uint8_t RAW : 1; // 6 raw output mode + uint8_t ALT : 1; // 7 0=barometer 1=altitude + } bits; + uint8_t octet; +} mpl_ctrl_reg1_t; + typedef union { struct { // at 0x uint8_t SRC_TCHG : 1; // 0 @@ -87,13 +100,19 @@ void SetModeActive(void); bool GetModeActive(void); void SetModeStandby(void); - float ReadAltitude( void ); - float ReadTemperature( void ); + float ReadAltitude( void ); // returns meters above sea level + float ReadBarometer(void); // returns pascals + float ReadTemperature( void ); // returns celcius void SetModeAltimeter(void); + void SetModeBarometer(void); void ToggleOneShot( void ); + void setOSR(uint8_t); + uint8_t getOSR(void); float Altitude; float Temperature; void service(void); + + mpl_ctrl_reg1_t ctrl_reg1; private: I2C& m_i2c;