pressure
MPL3115.cpp@0:01659d3a8c37, 2016-12-11 (annotated)
- Committer:
- otis22894
- Date:
- Sun Dec 11 21:12:09 2016 +0000
- Revision:
- 0:01659d3a8c37
First commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
otis22894 | 0:01659d3a8c37 | 1 | /* Copyright (c) 2015 NXP Semiconductors. MIT License |
otis22894 | 0:01659d3a8c37 | 2 | * |
otis22894 | 0:01659d3a8c37 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
otis22894 | 0:01659d3a8c37 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
otis22894 | 0:01659d3a8c37 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
otis22894 | 0:01659d3a8c37 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
otis22894 | 0:01659d3a8c37 | 7 | * Software is furnished to do so, subject to the following conditions: |
otis22894 | 0:01659d3a8c37 | 8 | * |
otis22894 | 0:01659d3a8c37 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
otis22894 | 0:01659d3a8c37 | 10 | * substantial portions of the Software. |
otis22894 | 0:01659d3a8c37 | 11 | * |
otis22894 | 0:01659d3a8c37 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
otis22894 | 0:01659d3a8c37 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
otis22894 | 0:01659d3a8c37 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
otis22894 | 0:01659d3a8c37 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
otis22894 | 0:01659d3a8c37 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
otis22894 | 0:01659d3a8c37 | 17 | */ |
otis22894 | 0:01659d3a8c37 | 18 | |
otis22894 | 0:01659d3a8c37 | 19 | #include "MPL3115.h" |
otis22894 | 0:01659d3a8c37 | 20 | #include "mbed.h" |
otis22894 | 0:01659d3a8c37 | 21 | #define REG_PRESSURE_MSB 0x01 |
otis22894 | 0:01659d3a8c37 | 22 | |
otis22894 | 0:01659d3a8c37 | 23 | MPL3115::MPL3115(PinName sda, PinName scl, int addr) : MPL3115_i2c(sda,scl), m_addr(addr) |
otis22894 | 0:01659d3a8c37 | 24 | { |
otis22894 | 0:01659d3a8c37 | 25 | |
otis22894 | 0:01659d3a8c37 | 26 | } |
otis22894 | 0:01659d3a8c37 | 27 | |
otis22894 | 0:01659d3a8c37 | 28 | void MPL3115::config(void) |
otis22894 | 0:01659d3a8c37 | 29 | { |
otis22894 | 0:01659d3a8c37 | 30 | char d[2]; |
otis22894 | 0:01659d3a8c37 | 31 | d[0] = MPL3115_CTRL_REG1; //Puts device in Standby mode |
otis22894 | 0:01659d3a8c37 | 32 | d[1] = 0x00; |
otis22894 | 0:01659d3a8c37 | 33 | MPL3115_i2c.write(MPL3115_I2C_ADDRESS, d,2); |
otis22894 | 0:01659d3a8c37 | 34 | |
otis22894 | 0:01659d3a8c37 | 35 | |
otis22894 | 0:01659d3a8c37 | 36 | d[0] = MPL3115_CTRL_REG1; //Puts device in Active mode and in altimeter mode |
otis22894 | 0:01659d3a8c37 | 37 | d[1] = 0x81; |
otis22894 | 0:01659d3a8c37 | 38 | MPL3115_i2c.write(MPL3115_I2C_ADDRESS, d, 2); |
otis22894 | 0:01659d3a8c37 | 39 | |
otis22894 | 0:01659d3a8c37 | 40 | } |
otis22894 | 0:01659d3a8c37 | 41 | |
otis22894 | 0:01659d3a8c37 | 42 | |
otis22894 | 0:01659d3a8c37 | 43 | float MPL3115::getPressure(void) |
otis22894 | 0:01659d3a8c37 | 44 | { |
otis22894 | 0:01659d3a8c37 | 45 | float a; |
otis22894 | 0:01659d3a8c37 | 46 | |
otis22894 | 0:01659d3a8c37 | 47 | a = getPressure(REG_PRESSURE_MSB); |
otis22894 | 0:01659d3a8c37 | 48 | return a; |
otis22894 | 0:01659d3a8c37 | 49 | } |
otis22894 | 0:01659d3a8c37 | 50 | |
otis22894 | 0:01659d3a8c37 | 51 | float MPL3115::getPressure(unsigned char reg) |
otis22894 | 0:01659d3a8c37 | 52 | { |
otis22894 | 0:01659d3a8c37 | 53 | unsigned char dt[3]; |
otis22894 | 0:01659d3a8c37 | 54 | unsigned int prs; |
otis22894 | 0:01659d3a8c37 | 55 | int tmp; |
otis22894 | 0:01659d3a8c37 | 56 | float fprs; |
otis22894 | 0:01659d3a8c37 | 57 | |
otis22894 | 0:01659d3a8c37 | 58 | /* |
otis22894 | 0:01659d3a8c37 | 59 | * dt[0] = Bits 12-19 of 20-bit real-time Pressure sample. (b7-b0) |
otis22894 | 0:01659d3a8c37 | 60 | * dt[1] = Bits 4-11 of 20-bit real-time Pressure sample. (b7-b0) |
otis22894 | 0:01659d3a8c37 | 61 | * dt[2] = Bits 0-3 of 20-bit real-time Pressure sample (b7-b4) |
otis22894 | 0:01659d3a8c37 | 62 | */ |
otis22894 | 0:01659d3a8c37 | 63 | readRegs( reg, &dt[0], 3); |
otis22894 | 0:01659d3a8c37 | 64 | prs = ((dt[0]<<10) | (dt[1]<<2) | (dt[2]>>6)); |
otis22894 | 0:01659d3a8c37 | 65 | // |
otis22894 | 0:01659d3a8c37 | 66 | if ( dt[0] > 0x7f) { |
otis22894 | 0:01659d3a8c37 | 67 | // negative number |
otis22894 | 0:01659d3a8c37 | 68 | if ( dt[0] & 0x80) |
otis22894 | 0:01659d3a8c37 | 69 | prs |= 0xFFFC0000; |
otis22894 | 0:01659d3a8c37 | 70 | else |
otis22894 | 0:01659d3a8c37 | 71 | prs |= 0xFFFE0000; |
otis22894 | 0:01659d3a8c37 | 72 | tmp = ~prs + 1; // make the complemets. At this point all the bits are inverted. |
otis22894 | 0:01659d3a8c37 | 73 | fprs = (float)tmp * -1.0f; |
otis22894 | 0:01659d3a8c37 | 74 | } else { |
otis22894 | 0:01659d3a8c37 | 75 | fprs = (float)prs * 1.0f; |
otis22894 | 0:01659d3a8c37 | 76 | } |
otis22894 | 0:01659d3a8c37 | 77 | |
otis22894 | 0:01659d3a8c37 | 78 | if ( dt[2] & 0x10) |
otis22894 | 0:01659d3a8c37 | 79 | fprs += 0.25f; |
otis22894 | 0:01659d3a8c37 | 80 | if ( dt[2] & 0x20) |
otis22894 | 0:01659d3a8c37 | 81 | fprs += 0.5f; |
otis22894 | 0:01659d3a8c37 | 82 | |
otis22894 | 0:01659d3a8c37 | 83 | return fprs; |
otis22894 | 0:01659d3a8c37 | 84 | } |
otis22894 | 0:01659d3a8c37 | 85 | void MPL3115::readRegs(int addr, uint8_t * data, int len) { |
otis22894 | 0:01659d3a8c37 | 86 | char t[1] = {addr}; |
otis22894 | 0:01659d3a8c37 | 87 | MPL3115_i2c.write(m_addr, t, 1, true); |
otis22894 | 0:01659d3a8c37 | 88 | MPL3115_i2c.read(m_addr, (char *)data, len); |
otis22894 | 0:01659d3a8c37 | 89 | } |
otis22894 | 0:01659d3a8c37 | 90 |