Llibrary for the WiGo MPL3115A2, I2C Precision Altimeter sensor.
Dependents: KL25Z_Batt_Test WIGO_MPL3115A2 Multi-Sensor SPACEmk2 ... more
30/05/2013 Added and tested the data acquisition using Interrupt. Added code for Altimeter trigger Interrupt but not yet tested.
Very basic library. Under development. Need to add in order: 1. IRQ configuration. 2. FIFO mode configuration.
Diff: MPL3115A2.cpp
- Revision:
- 2:a2fcfb7ff611
- Parent:
- 0:cfecfabc5e23
- Child:
- 3:a2f1752add9a
--- a/MPL3115A2.cpp Thu May 23 12:42:45 2013 +0000 +++ b/MPL3115A2.cpp Wed May 29 05:57:15 2013 +0000 @@ -1,8 +1,10 @@ #include "MPL3115A2.h" -#define REG_WHO_AM_I 0x0C // 0xC4 by default +#define REG_WHO_AM_I 0x0C // return 0xC4 by default #define REG_STATUS 0x00 #define REG_CTRL_REG_1 0x26 +#define REG_CTRL_REG_4 0x29 +#define REG_CTRL_REG_5 0x2A #define REG_PRESSURE_MSB 0x01 // 3 byte pressure data #define REG_ALTIMETER_MSB 0x01 // 3 byte altimeter data #define REG_TEMP_MSB 0x04 // 2 byte temperature data @@ -10,26 +12,94 @@ #define UINT14_MAX 16383 +void (*user2_fptr)(void); // Pointers to user function called after +void (*user1_fptr)(void); // IRQ assertion. + +// +InterruptIn MPL3115A2_Int1( PTD4); // INT1 +InterruptIn MPL3115A2_Int2( PTA12); // INT2 + MPL3115A2::MPL3115A2(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) { MPL3115A2_mode = BAROMETRIC_MODE; MPL3115A2_oversampling = OVERSAMPLE_RATIO_1; } +void MPL3115A2::Reset( void) +{ + unsigned char t; + + // soft reset... + readRegs( REG_CTRL_REG_1, &t, 1); + unsigned char data[2] = { REG_CTRL_REG_1, t|0x04}; + writeRegs(data, 2); + wait( 0.1); + +} + +void MPL3115A2::DataReady( void(*fptr)(void), unsigned char OS) +{ + unsigned char dt[5]; + unsigned char data[2]; + + Reset(); + + /* + ** Read contents of CTRL_REG_1 + ** Clear SBYB mask while holding all other values of CTRL_REG_1. + ** To put part into Standby mode + */ + Standby(); + + /* + ** Clear all interrupts by reading the output registers. + ** Activate sensor in Altimeter mode. + */ + readRegs( REG_ALTIMETER_MSB, &dt[0], 5); + getStatus(); + // Enable Interrupt fot data ready + data[0] = REG_CTRL_REG_4; + data[1] = 0x80; + writeRegs(data, 2); + // Configure Interrupt route to INT2 + data[0] = REG_CTRL_REG_5; + data[1] = 0x00; + writeRegs(data, 2); + + /* + ** Write a 1 to the ALT and SBYB bits to go into Active Altimeter mode + */ + // Configure the OverSampling rate, Altimeter mode and set the sensor Active + data[0] = REG_CTRL_REG_1; + data[1] = 0x81 | (OS<<3); + writeRegs(data, 2); + + user2_fptr = fptr; + MPL3115A2_Int2.fall( this, &MPL3115A2::DataReady_IRQ); + +} + +void MPL3115A2::DataReady_IRQ( void) +{ + // Clear the IRQ flag + getStatus(); + // Run the user supplied function + user2_fptr(); +} + void MPL3115A2::Barometric_Mode( void) { unsigned char t; + unsigned char data[2]; Standby(); - readRegs( REG_CTRL_REG_1, &t, 1); // soft reset... - unsigned char data[2] = { REG_CTRL_REG_1, t|0x04}; - writeRegs(data, 2); - wait( 0.2); - + Reset(); + Standby(); readRegs( REG_CTRL_REG_1, &t, 1); + // Set the Barometric mode data[0] = REG_CTRL_REG_1; data[1] = t&0x7F; writeRegs(data, 2); @@ -48,14 +118,12 @@ void MPL3115A2::Altimeter_Mode( void) { unsigned char t; - + unsigned char data[2]; + Standby(); - readRegs( REG_CTRL_REG_1, &t, 1); // soft reset... - unsigned char data[2] = { REG_CTRL_REG_1, t|0x04}; - writeRegs(data, 2); - wait( 0.2); + Reset(); Standby(); readRegs( REG_CTRL_REG_1, &t, 1);