MSS / MMA8452Q

Dependents:   test_MMA8452Q HelloWorld testSensor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MMA8452Q.h Source File

MMA8452Q.h

00001 /* Copyright (c) 2013-2015 Design Methodology Lab
00002 */
00003 /*
00004 * Copyright (c) 2010-2011 mbed.org, MIT License
00005 *
00006 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00007 * and associated documentation files (the "Software"), to deal in the Software without
00008 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00009 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00010 * Software is furnished to do so, subject to the following conditions:
00011 *
00012 * The above copyright notice and this permission notice shall be included in all copies or
00013 * substantial portions of the Software.
00014 *
00015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00016 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00017 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00018 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00020 */
00021 
00022 #ifndef MMA8452Q_H
00023 #define MMA8452Q_H
00024 
00025 #include "mbed.h"
00026 
00027 /**
00028 * MMA8452Q accelerometer example
00029 *
00030 * @code
00031 #include "mbed.h"
00032 #include "MMA8452Q.h"
00033 
00034 #if defined (TARGET_KL25Z)
00035 #define PIN_SCL  PTE1
00036 #define PIN_SDA  PTE0
00037 #elif defined (TARGET_KL46Z)
00038 #define PIN_SCL  PTE1
00039 #define PIN_SDA  PTE0
00040 #elif defined (TARGET_K64F)
00041 #define PIN_SCL  PTE24
00042 #define PIN_SDA  PTE25
00043 #elif defined (TARGET_K22F)
00044 #define PIN_SCL  PTE1
00045 #define PIN_SDA  PTE0
00046 #elif defined (TARGET_KL05Z)
00047 #define PIN_SCL  PTB3
00048 #define PIN_SDA  PTB4
00049 #elif define (TARGET_F411RE)
00050 #define PIN_SCL  PB_8
00051 #define PIN_SDA  PB_9
00052 #else
00053  #error TARGET NOT DEFINED
00054 #endif
00055  
00056 #define MMA8452_I2C_ADDRESS (0x1d)
00057  
00058 int main(void) {
00059      float x, y, z ;
00060 
00061      MMA8452Q acc(PIN_SDA, PIN_SCL, MMA8452_I2C_ADDRESS);
00062  
00063      while (true) {       
00064          x = acc.getAccX() ;
00065          y = acc.getAccY() ;
00066          z = acc.getAccZ() ;
00067          printf("X[%.2f] Y[%.2f] Z[%.2f]\n",x, y, z) ;
00068          wait(0.1);
00069     }
00070 }
00071 * @endcode
00072 */
00073 class MMA8452Q
00074 {
00075 public:
00076   /**
00077   * MMA8452Q constructor
00078   *
00079   * @param sda SDA pin
00080   * @param sdl SCL pin
00081   * @param addr addr of the I2C peripheral
00082   */
00083   MMA8452Q(PinName sda, PinName scl, int addr);
00084 
00085   /**
00086   * MMA8452Q destructor
00087   */
00088   ~MMA8452Q();
00089 
00090   /**
00091    * Get the value of the WHO_AM_I register
00092    *
00093    * @returns WHO_AM_I value
00094    */
00095   uint8_t getWhoAmI();
00096 
00097   /**
00098    * Get X axis acceleration
00099    *
00100    * @returns X axis acceleration
00101    */
00102   float getAccX();
00103 
00104   /**
00105    * Get Y axis acceleration
00106    *
00107    * @returns Y axis acceleration
00108    */
00109   float getAccY();
00110 
00111   /**
00112    * Get Z axis acceleration
00113    *
00114    * @returns Z axis acceleration
00115    */
00116   float getAccZ();
00117 
00118   /**
00119    * Get XYZ axis acceleration
00120    *
00121    * @param res array where acceleration data will be stored
00122    */
00123   void getAccAllAxis(float * res);
00124 
00125 private:
00126   I2C m_i2c;
00127   int m_addr;
00128   void readRegs(int addr, uint8_t * data, int len);
00129   void writeRegs(uint8_t * data, int len);
00130   int16_t getAccAxis(uint8_t addr);
00131 
00132 };
00133 
00134 #endif