My version of the library for this accelerometer. It works on 800Hz data rate.

Dependents:   FRDM_DEMO_CODE

Fork of MMA8451Q by Antonio Quevedo

Files at this revision

API Documentation at this revision

Comitter:
B50132
Date:
Tue Aug 19 15:05:55 2014 +0000
Parent:
0:7c9ab58f6af3
Commit message:
modified contents with another library doing the same thing.

Changed in this revision

MMA8451Q.cpp Show annotated file Show diff for this revision Revisions of this file
MMA8451Q.h Show annotated file Show diff for this revision Revisions of this file
--- a/MMA8451Q.cpp	Wed Jun 04 19:16:47 2014 +0000
+++ b/MMA8451Q.cpp	Tue Aug 19 15:05:55 2014 +0000
@@ -1,5 +1,4 @@
 /* Copyright (c) 2010-2011 mbed.org, MIT License
-* Copyright (c) 2014 Antonio Quevedo, UNICAMP
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files (the "Software"), to deal in the Software without
@@ -19,57 +18,64 @@
 
 #include "MMA8451Q.h"
 
-#define ACCEL_I2C_ADDRESS 0x1D<<1
-#define REG_STATUS        0x00
-#define REG_XYZ_DATA_CFG  0x0E
 #define REG_WHO_AM_I      0x0D
-#define REG_CTRL_REG1     0x2A
-#define REG_CTRL_REG2     0x2B
-#define REG_CTRL_REG3     0x2C
-#define REG_CTRL_REG4     0x2D
-#define REG_CTRL_REG5     0x2E
+#define REG_CTRL_REG_1    0x2A
 #define REG_OUT_X_MSB     0x01
 #define REG_OUT_Y_MSB     0x03
 #define REG_OUT_Z_MSB     0x05
 
-MMA8451Q::MMA8451Q(PinName sda, PinName scl) : m_i2c(sda, scl) {
-    uint8_t data[2] = {REG_CTRL_REG1, 0x00}; // Puts acc in standby for configuring
-    writeRegs(data, 2);
-    data[0] = REG_XYZ_DATA_CFG; // Writing 00 turns off high-pass filter and sets full scale range to 2g
-    // data[1] = 0x01; for 4g
-    // data[1] = 0x02; for 8g
-    writeRegs(data, 2);
-    data[0] = REG_CTRL_REG2;
-    data[1] = 0x00; // Disable self-test, software reset and auto-sleep; operates in normal mode
-    writeRegs(data, 2);
-    data[0] = REG_CTRL_REG3; // Interrupt polarity low, push-pull output
-    writeRegs(data, 2);
-    data[0] = REG_CTRL_REG4;
-    data[1] = 0x01; // Enables interrupt for data Ready
-    writeRegs(data, 2);
-    data[0] = REG_CTRL_REG5;
-    writeRegs(data, 2); // Routes Data Ready interrupt to INT1
-    data[0] = REG_CTRL_REG1; 
-    data[1] = 0x09; // Data rate is 800Hz
+#define UINT14_MAX        16383
+
+MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
+    // activate the peripheral
+    uint8_t data[2] = {REG_CTRL_REG_1, 0x01};
     writeRegs(data, 2);
 }
 
 MMA8451Q::~MMA8451Q() { }
 
-void MMA8451Q::getAccAllAxis(int16_t * res) {
-    uint8_t temp[6];
-    readRegs(REG_OUT_X_MSB, temp, 6);
-    res[0] = (temp[0] * 256) + temp[1];
-    res[1] = (temp[2] * 256) + temp[3];
-    res[2] = (temp[4] * 256) + temp[5];
+uint8_t MMA8451Q::getWhoAmI() {
+    uint8_t who_am_i = 0;
+    readRegs(REG_WHO_AM_I, &who_am_i, 1);
+    return who_am_i;
+}
+
+float MMA8451Q::getAccX() {
+    return (float(getAccAxis(REG_OUT_X_MSB))/4096.0);
+}
+
+float MMA8451Q::getAccY() {
+    return (float(getAccAxis(REG_OUT_Y_MSB))/4096.0);
+}
+
+float MMA8451Q::getAccZ() {
+    return (float(getAccAxis(REG_OUT_Z_MSB))/4096.0);
+}
+
+void MMA8451Q::getAccAllAxis(float * res) {
+    res[0] = getAccX();
+    res[1] = getAccY();
+    res[2] = getAccZ();
+}
+
+int16_t MMA8451Q::getAccAxis(uint8_t addr) {
+    int16_t acc;
+    uint8_t res[2];
+    readRegs(addr, res, 2);
+
+    acc = (res[0] << 6) | (res[1] >> 2);
+    if (acc > UINT14_MAX/2)
+        acc -= UINT14_MAX;
+
+    return acc;
 }
 
 void MMA8451Q::readRegs(int addr, uint8_t * data, int len) {
     char t[1] = {addr};
-    m_i2c.write(ACCEL_I2C_ADDRESS, t, 1, true);
-    m_i2c.read(ACCEL_I2C_ADDRESS, (char *)data, len);
+    m_i2c.write(m_addr, t, 1, true);
+    m_i2c.read(m_addr, (char *)data, len);
 }
 
 void MMA8451Q::writeRegs(uint8_t * data, int len) {
-    m_i2c.write(ACCEL_I2C_ADDRESS, (char *)data, len);
-}
\ No newline at end of file
+    m_i2c.write(m_addr, (char *)data, len);
+}
--- a/MMA8451Q.h	Wed Jun 04 19:16:47 2014 +0000
+++ b/MMA8451Q.h	Tue Aug 19 15:05:55 2014 +0000
@@ -1,5 +1,4 @@
 /* Copyright (c) 2010-2011 mbed.org, MIT License
-* Copyright (c) 2014 Antonio Quevedo, UNICAMP
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files (the "Software"), to deal in the Software without
@@ -23,26 +22,26 @@
 #include "mbed.h"
 
 /**
-* MMA8451Q accelerometer example, 8-bit samples
+* MMA8451Q accelerometer example
 *
 * @code
 * #include "mbed.h"
 * #include "MMA8451Q.h"
 * 
+* #define MMA8451_I2C_ADDRESS (0x1d<<1)
+* 
 * int main(void) {
 * 
-* MMA8451Q acc(PTE25, PTE24);
+* MMA8451Q acc(P_E25, P_E24, MMA8451_I2C_ADDRESS);
 * PwmOut rled(LED_RED);
 * PwmOut gled(LED_GREEN);
 * PwmOut bled(LED_BLUE);
-* uint8_t data[3];
-*
-*     while (true) {
-          acc.getAccAllAxis(data);       
-*         rled = 1.0 - abs(data[0]/128);
-*         gled = 1.0 - abs(data[0]/128);
-*         bled = 1.0 - abs(data[0]/128);
-*         wait(0.4);
+* 
+*     while (true) {       
+*         rled = 1.0 - abs(acc.getAccX());
+*         gled = 1.0 - abs(acc.getAccY());
+*         bled = 1.0 - abs(acc.getAccZ());
+*         wait(0.1);
 *     }
 * }
 * @endcode
@@ -55,8 +54,9 @@
   *
   * @param sda SDA pin
   * @param sdl SCL pin
+  * @param addr addr of the I2C peripheral
   */
-  MMA8451Q(PinName sda, PinName scl);
+  MMA8451Q(PinName sda, PinName scl, int addr);
 
   /**
   * MMA8451Q destructor
@@ -64,17 +64,47 @@
   ~MMA8451Q();
 
   /**
-   * Get XYZ axis acceleration, 8-bits
+   * Get the value of the WHO_AM_I register
+   *
+   * @returns WHO_AM_I value
+   */
+  uint8_t getWhoAmI();
+
+  /**
+   * Get X axis acceleration
+   *
+   * @returns X axis acceleration
+   */
+  float getAccX();
+
+  /**
+   * Get Y axis acceleration
+   *
+   * @returns Y axis acceleration
+   */
+  float getAccY();
+
+  /**
+   * Get Z axis acceleration
+   *
+   * @returns Z axis acceleration
+   */
+  float getAccZ();
+
+  /**
+   * Get XYZ axis acceleration
    *
    * @param res array where acceleration data will be stored
    */
-  void getAccAllAxis(int16_t * res);
+  void getAccAllAxis(float * res);
 
 private:
   I2C m_i2c;
+  int m_addr;
   void readRegs(int addr, uint8_t * data, int len);
   void writeRegs(uint8_t * data, int len);
+  int16_t getAccAxis(uint8_t addr);
 
 };
 
-#endif
\ No newline at end of file
+#endif