Code based on the original version from http://developer.mbed.org/users/eencae

Fork of MMA8452 by Craig Evans

MMA8452.h

Committer:
ml13emds
Date:
2015-05-11
Revision:
1:584301c98fb8
Parent:
0:df3b9e41edf3

File content as of revision 1:584301c98fb8:

/**
@file MMA8452.h

@brief Header file containing member functions and variables
@see https://developer.mbed.org/users/eencae/code/MMA8452/
*/

#ifndef MMA8452_H
#define MMA8452_H

// MMA8452 address is 0x1D by default (see EAGLE schematic of breakout - SA = 1)
#define MMA8452_W_ADDRESS   0x3A
#define MMA8452_R_ADDRESS   0x3B
// Register Descriptions - p18 datasheet
#define OUT_X_MSB           0x01
#define INT_SOURCE          0x0C
#define WHO_AM_I            0x0D
#define XYZ_DATA_CFG        0x0E
#define HP_FILTER_CUTOFF    0x0F
#define TRANSIENT_CFG       0x1D
#define TRANSIENT_SRC       0x1E
#define TRANSIENT_THS       0x1F
#define TRANSIENT_COUNT     0x20
#define PULSE_CFG           0x21
#define PULSE_SRC           0x22
#define PULSE_THSY          0x24
#define PULSE_TMLT          0x26
#define PULSE_LTCY          0x27
#define CTRL_REG1           0x2A
#define CTRL_REG3           0x2C
#define CTRL_REG4           0x2D
#define CTRL_REG5           0x2E


typedef struct Acceleration Acceleration;
struct Acceleration {
    float x;
    float y;
    float z;
};

#include "mbed.h"

/**
@brief Library for interfacing with MMA8452 I2C Accelerometer

@see http://www.freescale.com/files/sensors/doc/data_sheet/MMA8452Q.pdf
@see https://www.sparkfun.com/products/12756

@brief Revision 1.0

@author Craig A. Evans
@date   March 2015
 *
 * Example:
 * @code

#include "mbed.h"
#include "MMA8452.h"

MMA8452 mma8452(p28,p27);  // SDA, SCL
Serial serial(USBTX,USBRX);

int main() {

    mma8452.init();  // 100 Hz update rate, ±4g scale

    Acceleration acceleration;  // Accleration structure declared in MMA8452 class

    while(1) {

        acceleration = mma8452.readValues();   // read current values and print over serial port
        serial.printf("x = %.2f g y = %.2f g z = %.2f g\n",acceleration.x,acceleration.y,acceleration.z);
        wait(0.1);   // short delay until next reading

    }

}


 * @endcode
 */
class MMA8452
{

public:
    /** Create a MMA8452 object connected to the specified pins
    *
    * @param sdaPin - mbed SDA pin
    * @param sclPin - mbed SCL pin
    *
    */
    MMA8452(PinName sdaPin, PinName sclPin);

    /** Initialise accelerometer
    *
    *   Powers up the accelerometer, sets 100 Hz update rate and ±4g scale
    */
    void init();
    void transient_counting();

    Acceleration average();

    /** Get values of acceleration
    *
    *   Reads the x,y,z values in g's
    *   @returns an Acceleration structure with x,y,z members (float)
    */
    Acceleration readValues();
    char readByteFromRegister(char reg); //change for public by Edson


private:
    void sendByteToRegister(char byte,char reg);
    void readBytesFromRegister(char reg,int numberOfBytes,char bytes[]);
    void error();

public:

private:  // private variables
    I2C*    i2c;
    BusOut*  leds;

};



#endif