MPL sensor
Fork of MPL3115A2 by
MPL3115A2.h@4:fdf14a259af8, 2013-05-30 (annotated)
- Committer:
- clemente
- Date:
- Thu May 30 07:27:24 2013 +0000
- Revision:
- 4:fdf14a259af8
- Parent:
- 3:a2f1752add9a
- Child:
- 5:9edec5ee8bf4
Added and tested the data acquisition using Interrupt. Added code for Altimeter trigger Interrupt but not yet tested.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
clemente | 0:cfecfabc5e23 | 1 | #ifndef MPL3115A2_H |
clemente | 0:cfecfabc5e23 | 2 | #define MPL3115A2_H |
clemente | 0:cfecfabc5e23 | 3 | |
clemente | 0:cfecfabc5e23 | 4 | #include "mbed.h" |
clemente | 0:cfecfabc5e23 | 5 | |
clemente | 3:a2f1752add9a | 6 | // Oversampling value and minimum time between sample |
clemente | 3:a2f1752add9a | 7 | #define OVERSAMPLE_RATIO_1 0 // 6 ms |
clemente | 3:a2f1752add9a | 8 | #define OVERSAMPLE_RATIO_2 1 // 10 ms |
clemente | 3:a2f1752add9a | 9 | #define OVERSAMPLE_RATIO_4 2 // 18 ms |
clemente | 3:a2f1752add9a | 10 | #define OVERSAMPLE_RATIO_8 3 // 34 ms |
clemente | 3:a2f1752add9a | 11 | #define OVERSAMPLE_RATIO_16 4 // 66 ms |
clemente | 3:a2f1752add9a | 12 | #define OVERSAMPLE_RATIO_32 5 // 130 ms |
clemente | 3:a2f1752add9a | 13 | #define OVERSAMPLE_RATIO_64 6 // 258 ms |
clemente | 3:a2f1752add9a | 14 | #define OVERSAMPLE_RATIO_128 7 // 512 ms |
clemente | 0:cfecfabc5e23 | 15 | |
clemente | 0:cfecfabc5e23 | 16 | /* Mode */ |
clemente | 0:cfecfabc5e23 | 17 | #define ALTIMETER_MODE 1 |
clemente | 0:cfecfabc5e23 | 18 | #define BAROMETRIC_MODE 2 |
clemente | 0:cfecfabc5e23 | 19 | |
clemente | 0:cfecfabc5e23 | 20 | /** |
clemente | 0:cfecfabc5e23 | 21 | * MPL3115A2 Altimeter example |
clemente | 0:cfecfabc5e23 | 22 | * |
clemente | 0:cfecfabc5e23 | 23 | * @code |
clemente | 0:cfecfabc5e23 | 24 | * #include "mbed.h" |
clemente | 0:cfecfabc5e23 | 25 | * #include "MPL3115A2.h" |
clemente | 0:cfecfabc5e23 | 26 | * |
clemente | 0:cfecfabc5e23 | 27 | * #define MPL3115A2_I2C_ADDRESS (0x60<<1) |
clemente | 1:30a04f651efe | 28 | * MPL3115A2 wigo_sensor1(PTE0, PTE1, MPL3115A2_I2C_ADDRESS); |
clemente | 0:cfecfabc5e23 | 29 | * Serial pc(USBTX, USBRX); |
clemente | 0:cfecfabc5e23 | 30 | * |
clemente | 1:30a04f651efe | 31 | * // pos [0] = altitude or pressure value |
clemente | 1:30a04f651efe | 32 | * // pos [1] = temperature value |
clemente | 1:30a04f651efe | 33 | * float sensor_data[2]; |
clemente | 1:30a04f651efe | 34 | * |
clemente | 0:cfecfabc5e23 | 35 | * int main(void) { |
clemente | 0:cfecfabc5e23 | 36 | * |
clemente | 0:cfecfabc5e23 | 37 | * pc.baud( 230400); |
clemente | 0:cfecfabc5e23 | 38 | * pc.printf("MPL3115A2 Altimeter mode. [%d]\r\n", wigo_sensor1.getDeviceID()); |
clemente | 0:cfecfabc5e23 | 39 | * |
clemente | 0:cfecfabc5e23 | 40 | * wigo_sensor1.Oversample_Ratio( OVERSAMPLE_RATIO_32); |
clemente | 0:cfecfabc5e23 | 41 | * |
clemente | 0:cfecfabc5e23 | 42 | * while(1) { |
clemente | 0:cfecfabc5e23 | 43 | * // |
clemente | 0:cfecfabc5e23 | 44 | * if ( wigo_sensor1.isDataAvailable()) { |
clemente | 0:cfecfabc5e23 | 45 | * wigo_sensor1.getAllData( &sensor_data[0]); |
clemente | 1:30a04f651efe | 46 | * pc.printf("\tAltitude: %f\tTemperature: %f\r\n", sensor_data[0], sensor_data[1]); |
clemente | 0:cfecfabc5e23 | 47 | * } |
clemente | 0:cfecfabc5e23 | 48 | * // |
clemente | 0:cfecfabc5e23 | 49 | * wait( 0.001); |
clemente | 0:cfecfabc5e23 | 50 | * } |
clemente | 0:cfecfabc5e23 | 51 | * |
clemente | 0:cfecfabc5e23 | 52 | * } |
clemente | 0:cfecfabc5e23 | 53 | * @endcode |
clemente | 0:cfecfabc5e23 | 54 | */ |
clemente | 0:cfecfabc5e23 | 55 | class MPL3115A2 |
clemente | 0:cfecfabc5e23 | 56 | { |
clemente | 0:cfecfabc5e23 | 57 | public: |
clemente | 2:a2fcfb7ff611 | 58 | /** |
clemente | 2:a2fcfb7ff611 | 59 | * MPL3115A2 constructor |
clemente | 2:a2fcfb7ff611 | 60 | * |
clemente | 2:a2fcfb7ff611 | 61 | * @param sda SDA pin |
clemente | 2:a2fcfb7ff611 | 62 | * @param sdl SCL pin |
clemente | 2:a2fcfb7ff611 | 63 | * @param addr addr of the I2C peripheral |
clemente | 2:a2fcfb7ff611 | 64 | */ |
clemente | 2:a2fcfb7ff611 | 65 | MPL3115A2(PinName sda, PinName scl, int addr); |
clemente | 2:a2fcfb7ff611 | 66 | |
clemente | 2:a2fcfb7ff611 | 67 | /** |
clemente | 2:a2fcfb7ff611 | 68 | * Get the value of the WHO_AM_I register |
clemente | 2:a2fcfb7ff611 | 69 | * |
clemente | 2:a2fcfb7ff611 | 70 | * @returns DEVICE_ID value == 0xC4 |
clemente | 2:a2fcfb7ff611 | 71 | */ |
clemente | 2:a2fcfb7ff611 | 72 | uint8_t getDeviceID(); |
clemente | 2:a2fcfb7ff611 | 73 | |
clemente | 2:a2fcfb7ff611 | 74 | /** |
clemente | 2:a2fcfb7ff611 | 75 | * Return the STATUS register value |
clemente | 2:a2fcfb7ff611 | 76 | * |
clemente | 2:a2fcfb7ff611 | 77 | * @returns STATUS register value |
clemente | 2:a2fcfb7ff611 | 78 | */ |
clemente | 2:a2fcfb7ff611 | 79 | unsigned char getStatus( void); |
clemente | 2:a2fcfb7ff611 | 80 | |
clemente | 2:a2fcfb7ff611 | 81 | /** |
clemente | 2:a2fcfb7ff611 | 82 | * Get the altimeter value |
clemente | 2:a2fcfb7ff611 | 83 | * |
clemente | 2:a2fcfb7ff611 | 84 | * @returns altimeter value as float |
clemente | 2:a2fcfb7ff611 | 85 | */ |
clemente | 2:a2fcfb7ff611 | 86 | float getAltimeter( void); |
clemente | 2:a2fcfb7ff611 | 87 | |
clemente | 2:a2fcfb7ff611 | 88 | /** |
clemente | 2:a2fcfb7ff611 | 89 | * Get the pressure value |
clemente | 2:a2fcfb7ff611 | 90 | * |
clemente | 2:a2fcfb7ff611 | 91 | * @returns pressure value as float |
clemente | 2:a2fcfb7ff611 | 92 | */ |
clemente | 2:a2fcfb7ff611 | 93 | float getPressure( void); |
clemente | 2:a2fcfb7ff611 | 94 | |
clemente | 2:a2fcfb7ff611 | 95 | /** |
clemente | 2:a2fcfb7ff611 | 96 | * Get the temperature value |
clemente | 2:a2fcfb7ff611 | 97 | * |
clemente | 2:a2fcfb7ff611 | 98 | * @returns temperature value as float |
clemente | 2:a2fcfb7ff611 | 99 | */ |
clemente | 2:a2fcfb7ff611 | 100 | float getTemperature( void); |
clemente | 2:a2fcfb7ff611 | 101 | |
clemente | 2:a2fcfb7ff611 | 102 | /** |
clemente | 2:a2fcfb7ff611 | 103 | * Set the Altimeter Mode |
clemente | 2:a2fcfb7ff611 | 104 | * |
clemente | 2:a2fcfb7ff611 | 105 | * @returns none |
clemente | 2:a2fcfb7ff611 | 106 | */ |
clemente | 2:a2fcfb7ff611 | 107 | void Altimeter_Mode( void); |
clemente | 2:a2fcfb7ff611 | 108 | |
clemente | 2:a2fcfb7ff611 | 109 | /** |
clemente | 2:a2fcfb7ff611 | 110 | * Set the Barometric Mode |
clemente | 2:a2fcfb7ff611 | 111 | * |
clemente | 2:a2fcfb7ff611 | 112 | * @returns none |
clemente | 2:a2fcfb7ff611 | 113 | */ |
clemente | 2:a2fcfb7ff611 | 114 | void Barometric_Mode( void); |
clemente | 2:a2fcfb7ff611 | 115 | |
clemente | 2:a2fcfb7ff611 | 116 | /** |
clemente | 2:a2fcfb7ff611 | 117 | * Get the altimeter, pressure and temperature values |
clemente | 2:a2fcfb7ff611 | 118 | * |
clemente | 2:a2fcfb7ff611 | 119 | * @param array of float f[3] |
clemente | 2:a2fcfb7ff611 | 120 | * @returns none |
clemente | 2:a2fcfb7ff611 | 121 | */ |
clemente | 2:a2fcfb7ff611 | 122 | void getAllData( float *f); |
clemente | 2:a2fcfb7ff611 | 123 | |
clemente | 2:a2fcfb7ff611 | 124 | /** |
clemente | 2:a2fcfb7ff611 | 125 | * Return if there are date available |
clemente | 2:a2fcfb7ff611 | 126 | * |
clemente | 2:a2fcfb7ff611 | 127 | * @return 1 for data available, 0 for no data available |
clemente | 2:a2fcfb7ff611 | 128 | */ |
clemente | 2:a2fcfb7ff611 | 129 | unsigned int isDataAvailable( void); |
clemente | 2:a2fcfb7ff611 | 130 | |
clemente | 2:a2fcfb7ff611 | 131 | /** |
clemente | 2:a2fcfb7ff611 | 132 | * Set the oversampling rate value |
clemente | 2:a2fcfb7ff611 | 133 | * |
clemente | 2:a2fcfb7ff611 | 134 | * @param oversampling values. See MPL3115A2.h |
clemente | 2:a2fcfb7ff611 | 135 | * @return none |
clemente | 2:a2fcfb7ff611 | 136 | */ |
clemente | 2:a2fcfb7ff611 | 137 | void Oversample_Ratio( unsigned int ratio); |
clemente | 0:cfecfabc5e23 | 138 | |
clemente | 2:a2fcfb7ff611 | 139 | /** |
clemente | 2:a2fcfb7ff611 | 140 | * Configure the sensor to streaming data using Interrupt |
clemente | 2:a2fcfb7ff611 | 141 | * |
clemente | 2:a2fcfb7ff611 | 142 | * @param user functin callback, oversampling values. See MPL3115A2.h |
clemente | 2:a2fcfb7ff611 | 143 | * @return none |
clemente | 2:a2fcfb7ff611 | 144 | */ |
clemente | 2:a2fcfb7ff611 | 145 | void DataReady( void(*fptr)(void), unsigned char OS); |
clemente | 3:a2f1752add9a | 146 | |
clemente | 3:a2f1752add9a | 147 | /** |
clemente | 4:fdf14a259af8 | 148 | * Configure the sensor to generate an Interrupt crossing the center threshold |
clemente | 3:a2f1752add9a | 149 | * |
clemente | 4:fdf14a259af8 | 150 | * @param user functin callback, level in meter |
clemente | 3:a2f1752add9a | 151 | * @return none |
clemente | 3:a2f1752add9a | 152 | */ |
clemente | 3:a2f1752add9a | 153 | void AltitudeTrigger( void(*fptr)(void), unsigned short level); |
clemente | 2:a2fcfb7ff611 | 154 | |
clemente | 2:a2fcfb7ff611 | 155 | /** |
clemente | 2:a2fcfb7ff611 | 156 | * Soft Reset |
clemente | 2:a2fcfb7ff611 | 157 | * |
clemente | 2:a2fcfb7ff611 | 158 | * @param none |
clemente | 2:a2fcfb7ff611 | 159 | * @return none |
clemente | 2:a2fcfb7ff611 | 160 | */ |
clemente | 2:a2fcfb7ff611 | 161 | void Reset( void); |
clemente | 0:cfecfabc5e23 | 162 | |
clemente | 0:cfecfabc5e23 | 163 | private: |
clemente | 2:a2fcfb7ff611 | 164 | I2C m_i2c; |
clemente | 2:a2fcfb7ff611 | 165 | int m_addr; |
clemente | 2:a2fcfb7ff611 | 166 | unsigned char MPL3115A2_mode; |
clemente | 2:a2fcfb7ff611 | 167 | unsigned char MPL3115A2_oversampling; |
clemente | 2:a2fcfb7ff611 | 168 | void DataReady_IRQ( void); |
clemente | 3:a2f1752add9a | 169 | void AltitudeTrg_IRQ( void); |
clemente | 2:a2fcfb7ff611 | 170 | |
clemente | 2:a2fcfb7ff611 | 171 | /** Set the device in active mode |
clemente | 2:a2fcfb7ff611 | 172 | */ |
clemente | 2:a2fcfb7ff611 | 173 | void Active( void); |
clemente | 2:a2fcfb7ff611 | 174 | |
clemente | 2:a2fcfb7ff611 | 175 | /** Set the device in standby mode |
clemente | 2:a2fcfb7ff611 | 176 | */ |
clemente | 2:a2fcfb7ff611 | 177 | void Standby( void); |
clemente | 2:a2fcfb7ff611 | 178 | |
clemente | 2:a2fcfb7ff611 | 179 | void readRegs(int addr, uint8_t * data, int len); |
clemente | 2:a2fcfb7ff611 | 180 | void writeRegs(uint8_t * data, int len); |
clemente | 0:cfecfabc5e23 | 181 | |
clemente | 0:cfecfabc5e23 | 182 | }; |
clemente | 0:cfecfabc5e23 | 183 | |
clemente | 0:cfecfabc5e23 | 184 | #endif |