Library for driving the MMA8452 accelerometer over I2C
Dependents: MMA8452_Test MMA8452_Demo Dualing_Tanks IMU-Controlled_MP3_Player ... more
Here is a simple example:
#include "mbed.h" #include "MMA8452.h" int main() { Serial pc(USBTX,USBRX); pc.baud(115200); double x = 0, y = 0, z = 0; MMA8452 acc(p28, p27, 40000); acc.setBitDepth(MMA8452::BIT_DEPTH_12); acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G); acc.setDataRate(MMA8452::RATE_100); while(1) { if(!acc.isXYZReady()) { wait(0.01); continue; } acc.readXYZGravity(&x,&y,&z); pc.printf("Gravities: %lf %lf %lf\r\n",x,y,z); } }
An easy way to test that this actually works is to run the loop above and hold the MMA8452 parallel to the ground along the respective axis (and upsidedown in each axis). You will see 1G on the respective axis and 0G on the others.
Diff: MMA8452.h
- Revision:
- 1:ef026bf28798
- Parent:
- 0:bcf2aa85d7f9
- Child:
- 2:66db0f91b215
--- a/MMA8452.h Fri Oct 04 14:48:02 2013 +0000 +++ b/MMA8452.h Tue Oct 08 15:25:32 2013 +0000 @@ -89,7 +89,7 @@ #define FF_MT_CFG 0X15 // Type 'read/write' : Freefaul motion functional block configuration #define FF_MT_SRC 0X16 // Type 'read' : Freefaul motion event source register -#define FF_COUNT 0X17 // Type 'read' : Freefaul motion threshold register +#define FF_MT_THS 0X17 // Type 'read' : Freefaul motion threshold register #define FF_COUNT 0X18 // Type 'read' : Freefaul motion debouce counter #define ASLP_COUNT 0x29 // Type 'read/write' : Counter setting for auto sleep @@ -126,7 +126,8 @@ * @param scl I2C8452 clock port * */ - Accelerometer_MMA8452(PinName sda, PinName scl); + Accelerometer_MMA8452(PinName sda, PinName scl, int frequency); + //Accelerometer_MMA8452(PinName sda, PinName scl); /** Destroys an MMA8452 object * @@ -134,16 +135,23 @@ ~Accelerometer_MMA8452(); /** Activate the MMA8452 (required) - * - * - */ + * returns 0 for success in activating the chip + * returns 1 for failure in activating the chip + * -currrently no retries or waiting is done, this method tries 1 time the exits. + */ int activate(); /** Initialization of device MMA8452 (required) - * */ void init(); + /** Read the device ID from the accelerometer + * + * @param *deviceID Value of device - is should be 0x2A + * return 0 for success or + * return 1 for failure. + */ + int Get_DeviceID(int *deviceID); /** Read the Tilt Angle using Three Axis * * @param *x Value of x tilt @@ -169,6 +177,15 @@ * @returns The value of z acceleration */ int read_z(); + + /** Read the x,y and z registers of the MMA8452 + * + * takes a 2 byte char buffer to store the value + * for each axis. + * returns 0 for success, and 1 for failure + * + */ + int read_xyz(char *x, char *y, char *z); /** Read from specified MMA8452 register * @@ -187,7 +204,7 @@ private: I2C m_i2c; - int m_frequencey; + int m_frequency; };