CPS-Lab1 / Lab7-ReadReg

Fork of MPL3115A2 by NXP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MPL3115.cpp Source File

MPL3115.cpp

00001 /*
00002  * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
00003  * Copyright 2016-2017 NXP
00004  *
00005  * Redistribution and use in source and binary forms, with or without modification,
00006  * are permitted provided that the following conditions are met:
00007  *
00008  * o Redistributions of source code must retain the above copyright notice, this list
00009  *   of conditions and the following disclaimer.
00010  *
00011  * o Redistributions in binary form must reproduce the above copyright notice, this
00012  *   list of conditions and the following disclaimer in the documentation and/or
00013  *   other materials provided with the distribution.
00014  *
00015  * o Neither the name of the copyright holder nor the names of its
00016  *   contributors may be used to endorse or promote products derived from this
00017  *   software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00020  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00023  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00026  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  */
00030 
00031 #include "MPL3115.h"
00032  #include "mbed.h"
00033 
00034 MPL3115::MPL3115(PinName sda, PinName scl) : MPL3115_i2c(sda,scl)
00035  {
00036        
00037  }
00038     
00039  void MPL3115::MPL3115_config(void)
00040  {
00041    char d[2]; 
00042    d[0] = MPL3115_CTRL_REG1;                     //Puts device in Standby mode
00043    d[1] = 0x00; 
00044    MPL3115_i2c.write(MPL3115_I2C_ADDRESS, d,2);   
00045           
00046    
00047    d[0] = MPL3115_CTRL_REG1;                     //Puts device in Active mode and in altimeter mode
00048    d[1] = 0x81;
00049    MPL3115_i2c.write(MPL3115_I2C_ADDRESS, d, 2);   
00050       
00051  }
00052  
00053  int MPL3115::readReg(int address) { 
00054     int length = 8;
00055     char d[1];
00056     char readData[length];
00057     
00058     d[0] = MPL3115_STATUS;
00059     MPL3115_i2c.write(MPL3115_I2C_ADDRESS,d,1,true);
00060     MPL3115_i2c.read(MPL3115_I2C_ADDRESS,readData,length);
00061     for (int i = 0; i < length; i++) { 
00062         if (readData[i]) { 
00063             printf("The readData of %d is 1\r\n",i);
00064         } else {
00065             printf("The readData of %d is 0\r\n",i);
00066         }
00067     }
00068     
00069     //printf("The readData is : %s\r\n", readData);
00070     int returnValue = 0;
00071     for (int i = 0; i < length; i++) {
00072         returnValue << readData[i];
00073     }
00074     
00075     return returnValue;
00076 
00077 }
00078  
00079   
00080  
00081  void MPL3115::acquire_MPL3115_data_Altitude_in_m(float * alt_data)
00082  {
00083   
00084    char data_bytes[7];
00085    char d[1];
00086    d[0]=MPL3115_STATUS;
00087    MPL3115_i2c.write(MPL3115_I2C_ADDRESS,d,1,true);  // Read the 6 data bytes - LSB and MSB for X, Y and Z Axes.
00088    MPL3115_i2c.read(MPL3115_I2C_ADDRESS,data_bytes,5);
00089    alt_data[0]= (float)((short)((data_bytes[1] << 8 ) | data_bytes[2])) + (float)(data_bytes[3]>>4) * 0.0625;
00090   
00091   
00092  }