Craig Evans / MMA8452

Dependents:   acce_ChenZhengyang mbed_ProjectC_Accelerometer AccelerometerProject mbed_Accelerometer ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MMA8452.h Source File

MMA8452.h

Go to the documentation of this file.
00001 /**
00002 @file MMA8452.h
00003 
00004 @brief Header file containing member functions and variables
00005 
00006 */
00007 
00008 #ifndef MMA8452_H
00009 #define MMA8452_H
00010 
00011 // MMA8452 address is 0x1D by default (see EAGLE schematic of breakout - SA = 1)
00012 #define MMA8452_W_ADDRESS   0x3A
00013 #define MMA8452_R_ADDRESS   0x3B
00014 // Register Descriptions - p18 datasheet
00015 #define OUT_X_MSB           0x01
00016 #define WHO_AM_I            0x0D
00017 #define CTRL_REG1           0x2A
00018 #define XYZ_DATA_CFG        0x0E
00019 
00020 typedef struct Acceleration Acceleration;
00021 struct Acceleration {
00022     float x;
00023     float y;
00024     float z;
00025 };
00026 
00027 #include "mbed.h"
00028 
00029 /**
00030 @brief Library for interfacing with MMA8452 I2C Accelerometer
00031 
00032 @see http://www.freescale.com/files/sensors/doc/data_sheet/MMA8452Q.pdf
00033 @see https://www.sparkfun.com/products/12756
00034 
00035 @brief Revision 1.0
00036 
00037 @author Craig A. Evans
00038 @date   March 2015
00039  *
00040  * Example:
00041  * @code
00042 
00043 #include "mbed.h"
00044 #include "MMA8452.h"
00045 
00046 MMA8452 mma8452(p28,p27);  // SDA, SCL
00047 Serial serial(USBTX,USBRX);
00048 
00049 int main() {
00050 
00051     mma8452.init();  // 100 Hz update rate, ±4g scale
00052 
00053     Acceleration acceleration;  // Accleration structure declared in MMA8452 class
00054 
00055     while(1) {
00056 
00057         acceleration = mma8452.readValues();   // read current values and print over serial port
00058         serial.printf("x = %.2f g y = %.2f g z = %.2f g\n",acceleration.x,acceleration.y,acceleration.z);
00059         wait(0.1);   // short delay until next reading
00060 
00061     }
00062 
00063 }
00064 
00065 
00066  * @endcode
00067  */
00068 class MMA8452
00069 {
00070 
00071 public:
00072     /** Create a MMA8452 object connected to the specified pins
00073     *
00074     * @param sdaPin - mbed SDA pin
00075     * @param sclPin - mbed SCL pin
00076     *
00077     */
00078     MMA8452(PinName sdaPin, PinName sclPin);
00079 
00080     /** Initialise accelerometer
00081     *
00082     *   Powers up the accelerometer, sets 100 Hz update rate and ±4g scale
00083     */
00084     void init();
00085 
00086     /** Get values of acceleration
00087     *
00088     *   Reads the x,y,z values in g's
00089     *   @returns an Acceleration structure with x,y,z members (float)
00090     */
00091     Acceleration readValues();
00092 
00093 
00094 private:
00095     void sendByteToRegister(char byte,char reg);
00096     char readByteFromRegister(char reg);
00097     void readBytesFromRegister(char reg,int numberOfBytes,char bytes[]);
00098     void error();
00099 
00100 public:
00101 
00102 private:  // private variables
00103     I2C*    i2c;
00104     BusOut*  leds;
00105 
00106 };
00107 
00108 
00109 
00110 #endif