Ryan Williams / MPL3115

Dependents:   xtrinsic_sensors

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MPL3115.cpp Source File

MPL3115.cpp

00001  /* Copyright (c) 2015 NXP Semiconductors. MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 */
00018 
00019 #include "MPL3115.h"
00020 #include "mbed.h"
00021 #define REG_PRESSURE_MSB    0x01
00022 
00023 MPL3115::MPL3115(PinName sda, PinName scl, int addr) : MPL3115_i2c(sda,scl), m_addr(addr)
00024  {
00025        
00026  }
00027     
00028  void MPL3115::config(void)
00029  {
00030    char d[2]; 
00031    d[0] = MPL3115_CTRL_REG1;                     //Puts device in Standby mode
00032    d[1] = 0x00; 
00033    MPL3115_i2c.write(MPL3115_I2C_ADDRESS, d,2);   
00034           
00035    
00036    d[0] = MPL3115_CTRL_REG1;                     //Puts device in Active mode and in altimeter mode
00037    d[1] = 0x81;
00038    MPL3115_i2c.write(MPL3115_I2C_ADDRESS, d, 2);   
00039       
00040  }
00041  
00042 
00043  float MPL3115::getPressure(void)
00044 {
00045     float a;
00046     
00047     a = getPressure(REG_PRESSURE_MSB);
00048     return a;
00049 }
00050  
00051  float MPL3115::getPressure(unsigned char reg)
00052 {
00053     unsigned char dt[3];
00054     unsigned int prs;
00055     int tmp;
00056     float fprs;
00057     
00058     /*
00059     * dt[0] = Bits 12-19 of 20-bit real-time Pressure sample. (b7-b0)
00060     * dt[1] = Bits 4-11 of 20-bit real-time Pressure sample. (b7-b0)
00061     * dt[2] = Bits 0-3 of 20-bit real-time Pressure sample (b7-b4)
00062     */
00063     readRegs( reg, &dt[0], 3);
00064     prs = ((dt[0]<<10) | (dt[1]<<2) | (dt[2]>>6));
00065     //
00066     if ( dt[0] > 0x7f) {
00067         // negative number
00068         if ( dt[0] & 0x80)
00069             prs |= 0xFFFC0000;      
00070         else
00071             prs |= 0xFFFE0000;
00072         tmp = ~prs + 1;             // make the complemets. At this point all the bits are inverted.
00073         fprs = (float)tmp * -1.0f;  
00074     } else {
00075         fprs = (float)prs * 1.0f;
00076     }
00077  
00078     if ( dt[2] & 0x10)              
00079         fprs += 0.25f;             
00080     if ( dt[2] & 0x20)
00081         fprs += 0.5f;
00082         
00083     return fprs;
00084 }
00085  void MPL3115::readRegs(int addr, uint8_t * data, int len) {
00086     char t[1] = {addr};
00087     MPL3115_i2c.write(m_addr, t, 1, true);
00088     MPL3115_i2c.read(m_addr, (char *)data, len);
00089 }
00090