MPL3115A2 Pressure sensor library
Dependents: FRDM-FXS-MULTI2-B_SensorShield_HelloWorld
MPL3115.cpp@1:423fad528a24, 2017-04-26 (annotated)
- Committer:
- AswinSivakumar
- Date:
- Wed Apr 26 00:20:55 2017 +0000
- Revision:
- 1:423fad528a24
- Parent:
- 0:2cc0f6ef330f
updated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AswinSivakumar | 1:423fad528a24 | 1 | /* |
AswinSivakumar | 1:423fad528a24 | 2 | * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc. |
AswinSivakumar | 1:423fad528a24 | 3 | * Copyright 2016-2017 NXP |
AswinSivakumar | 1:423fad528a24 | 4 | * |
AswinSivakumar | 1:423fad528a24 | 5 | * Redistribution and use in source and binary forms, with or without modification, |
AswinSivakumar | 1:423fad528a24 | 6 | * are permitted provided that the following conditions are met: |
AswinSivakumar | 1:423fad528a24 | 7 | * |
AswinSivakumar | 1:423fad528a24 | 8 | * o Redistributions of source code must retain the above copyright notice, this list |
AswinSivakumar | 1:423fad528a24 | 9 | * of conditions and the following disclaimer. |
AswinSivakumar | 1:423fad528a24 | 10 | * |
AswinSivakumar | 1:423fad528a24 | 11 | * o Redistributions in binary form must reproduce the above copyright notice, this |
AswinSivakumar | 1:423fad528a24 | 12 | * list of conditions and the following disclaimer in the documentation and/or |
AswinSivakumar | 1:423fad528a24 | 13 | * other materials provided with the distribution. |
AswinSivakumar | 1:423fad528a24 | 14 | * |
AswinSivakumar | 1:423fad528a24 | 15 | * o Neither the name of the copyright holder nor the names of its |
AswinSivakumar | 1:423fad528a24 | 16 | * contributors may be used to endorse or promote products derived from this |
AswinSivakumar | 1:423fad528a24 | 17 | * software without specific prior written permission. |
AswinSivakumar | 1:423fad528a24 | 18 | * |
AswinSivakumar | 1:423fad528a24 | 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
AswinSivakumar | 1:423fad528a24 | 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
AswinSivakumar | 1:423fad528a24 | 21 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
AswinSivakumar | 1:423fad528a24 | 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
AswinSivakumar | 1:423fad528a24 | 23 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
AswinSivakumar | 1:423fad528a24 | 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
AswinSivakumar | 1:423fad528a24 | 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
AswinSivakumar | 1:423fad528a24 | 26 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
AswinSivakumar | 1:423fad528a24 | 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
AswinSivakumar | 1:423fad528a24 | 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
AswinSivakumar | 1:423fad528a24 | 29 | */ |
AswinSivakumar | 0:2cc0f6ef330f | 30 | |
AswinSivakumar | 0:2cc0f6ef330f | 31 | #include "MPL3115.h" |
AswinSivakumar | 0:2cc0f6ef330f | 32 | #include "mbed.h" |
AswinSivakumar | 0:2cc0f6ef330f | 33 | |
AswinSivakumar | 0:2cc0f6ef330f | 34 | MPL3115::MPL3115(PinName sda, PinName scl) : MPL3115_i2c(sda,scl) |
AswinSivakumar | 0:2cc0f6ef330f | 35 | { |
AswinSivakumar | 0:2cc0f6ef330f | 36 | |
AswinSivakumar | 0:2cc0f6ef330f | 37 | } |
AswinSivakumar | 0:2cc0f6ef330f | 38 | |
AswinSivakumar | 0:2cc0f6ef330f | 39 | void MPL3115::MPL3115_config(void) |
AswinSivakumar | 0:2cc0f6ef330f | 40 | { |
AswinSivakumar | 0:2cc0f6ef330f | 41 | char d[2]; |
AswinSivakumar | 0:2cc0f6ef330f | 42 | d[0] = MPL3115_CTRL_REG1; //Puts device in Standby mode |
AswinSivakumar | 0:2cc0f6ef330f | 43 | d[1] = 0x00; |
AswinSivakumar | 0:2cc0f6ef330f | 44 | MPL3115_i2c.write(MPL3115_I2C_ADDRESS, d,2); |
AswinSivakumar | 0:2cc0f6ef330f | 45 | |
AswinSivakumar | 0:2cc0f6ef330f | 46 | |
AswinSivakumar | 0:2cc0f6ef330f | 47 | d[0] = MPL3115_CTRL_REG1; //Puts device in Active mode and in altimeter mode |
AswinSivakumar | 0:2cc0f6ef330f | 48 | d[1] = 0x81; |
AswinSivakumar | 0:2cc0f6ef330f | 49 | MPL3115_i2c.write(MPL3115_I2C_ADDRESS, d, 2); |
AswinSivakumar | 0:2cc0f6ef330f | 50 | |
AswinSivakumar | 0:2cc0f6ef330f | 51 | } |
AswinSivakumar | 0:2cc0f6ef330f | 52 | |
AswinSivakumar | 0:2cc0f6ef330f | 53 | |
AswinSivakumar | 0:2cc0f6ef330f | 54 | |
AswinSivakumar | 0:2cc0f6ef330f | 55 | void MPL3115::acquire_MPL3115_data_Altitude_in_m(float * alt_data) |
AswinSivakumar | 0:2cc0f6ef330f | 56 | { |
AswinSivakumar | 0:2cc0f6ef330f | 57 | |
AswinSivakumar | 0:2cc0f6ef330f | 58 | char data_bytes[7]; |
AswinSivakumar | 0:2cc0f6ef330f | 59 | char d[1]; |
AswinSivakumar | 0:2cc0f6ef330f | 60 | d[0]=MPL3115_STATUS; |
AswinSivakumar | 0:2cc0f6ef330f | 61 | MPL3115_i2c.write(MPL3115_I2C_ADDRESS,d,1,true); // Read the 6 data bytes - LSB and MSB for X, Y and Z Axes. |
AswinSivakumar | 0:2cc0f6ef330f | 62 | MPL3115_i2c.read(MPL3115_I2C_ADDRESS,data_bytes,5); |
AswinSivakumar | 0:2cc0f6ef330f | 63 | alt_data[0]= (float)((short)((data_bytes[1] << 8 ) | data_bytes[2])) + (float)(data_bytes[3]>>4) * 0.0625; |
AswinSivakumar | 0:2cc0f6ef330f | 64 | |
AswinSivakumar | 0:2cc0f6ef330f | 65 | |
AswinSivakumar | 0:2cc0f6ef330f | 66 | } |