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:
- 3:ffb0b1650ca2
- Parent:
- 2:66db0f91b215
- Child:
- 5:b3d0abd97e55
--- a/MMA8452.h Tue Oct 08 16:13:14 2013 +0000 +++ b/MMA8452.h Wed Oct 16 14:11:04 2013 +0000 @@ -92,13 +92,19 @@ #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 +#define ASLP_COUNT 0x29 // Type 'read/write' : Counter settings for auto sleep #define CTRL_REG_1 0x2A // Type 'read/write' : #define CTRL_REG_2 0x2B // Type 'read/write' : #define CTRL_REG_3 0x2C // Type 'read/write' : #define CTRL_REG_4 0x2D // Type 'read/write' : #define CTRL_REG_5 0x2E // Type 'read/write' : +// Defined in table 13 of the Freescale PDF +#define STANDBY 0x00 // State value returned after a SYSMOD request, it can be in state STANDBY, WAKE or SLEEP +#define WAKE 0x01 // State value returned after a SYSMOD request, it can be in state STANDBY, WAKE or SLEEP +#define SLEEP 0x02 // State value returned after a SYSMOD request, it can be in state STANDBY, WAKE or SLEEP +#define ACTIVE 0x01 // Stage value returned and set in Control Register 1, it can be STANDBY=00, or ACTIVE=01 + #define TILT_STATUS 0x03 // Tilt Status (Read only) @@ -134,12 +140,53 @@ */ ~Accelerometer_MMA8452(); + + + /** Get system mode of the MMA8452 (not required) + * returns 0 for success in reading the system mode of the chip + * returns 1 for failure in reading the system mode of the chip + * -currently no retries or waiting is done, this method tries 1 time then exits. + * + * This method is used to find out the system mode of the chip ACTIVE = 0x00 or STANDBY = 0x01 + */ + int get_SystemMode(int& deviceSystemMode); + + + + /** Get status of the MMA8452 (not required) + * returns 0 for success in reading the status of the chip + * returns 1 for failure in reading the status of the chip + * -currrently no retries or waiting is done, this method tries 1 time then exits. + * + * This method is used to find out the real time status of the device it shows if + * x,y and z values have been overwritten before they have been read since a change happened. + * + */ + int get_Status(int& deviceStatus); + + + /** 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. + * + * This will set the device 'active' even if it's already active. It's just a way to force that state. */ int activate(); + + + + /** Standby the MMA8452 (not 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. + * + * This will set the device 'standby' even if it's already in standby. It's just a way to force that state. + */ + int standby(); + + /** Initialization of device MMA8452 (required) */ @@ -151,32 +198,29 @@ * 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 - * @param *y Value of y tilt - * @param *z Value of z tilt - */ - void read_Tilt(float *x, float *y, float *z); + int get_DeviceID(int& deviceID); + int read_y(); + + int read_z(); + /** Read the x register of the MMA8452 * * @returns The value of x acceleration */ - int read_x(); - + int read_x_raw(char *xaxis); + //int read_x(int& xaxisLSB); /** Read the y register of the MMA8452 * * @returns The value of y acceleration */ - int read_y(); + int read_y_raw(char *yaxis); /** Read the z register of the MMA8452 * * @returns The value of z acceleration */ - int read_z(); + int read_z_raw(char * zaxis); /** Read the x,y and z registers of the MMA8452 *