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:
- 11:dfd1e0afcb7b
- Parent:
- 10:ca9ba7ad4e94
- Child:
- 12:172540ff6b8b
diff -r ca9ba7ad4e94 -r dfd1e0afcb7b MMA8452.h --- a/MMA8452.h Tue Mar 04 11:14:34 2014 +0000 +++ b/MMA8452.h Tue Mar 04 16:23:40 2014 +0000 @@ -20,6 +20,10 @@ // the SparkFun breakout board defaults to 1, set to 0 if SA0 jumper on the bottom of the board is set // see the Table 10. I2C Device Address Sequence in Freescale MMA8452Q pdf + +#define DBG(...) pc.printf(__VA_ARGS__); pc.printf("\r\n"); +#define DBGX(...) pc.print(__VA_ARGS__); + //#define SA0 1 //#if SA0 //#define MMA8452_ADDRESS 0x3A // SA0 is high, 0x1C if low - it should be 0x1D, but we shift now to save shifting in the code @@ -64,9 +68,9 @@ // More info on MCU Master address can be found on section 5.10.1 of http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=MMA8452Q #define SA0 1 #if SA0 - #define MMA8452_ADDRESS 0x1D // SA0 is high, 0x1C if low - + #define MMA8452_ADDRESS 0x3A // 0x1D<<1 // SA0 is high, 0x1C if low - #else - #define MMA8452_ADDRESS 0x1C + #define MMA8452_ADDRESS 0x38 // 0x1C<<1 #endif // Register descriptions found in section 6 of pdf @@ -116,21 +120,53 @@ #define PDET_STATUS 0x09 // Tap/Pulse Detection Register (Read/Write) #define PD_STATUS 0xA // Tap/Pulse Debounce Count Register (Read/Write) +#define MMA8452_ACTIVE_MASK 0x01 +#define MMA8452_STANDBY_MASK 0xFE + +#define MMA8452_XYZ_DATA_CFG 0x0E +#define MMA8452_CTRL_REG_1 0x2A + +#define MMA8452_DYNAMIC_RANGE_MASK 0xFC + + +#define MMA8452_DATA_RATE_MASK 0xC7 +#define MMA8452_DATA_RATE_MASK_SHIFT 0x03 + +#define MMA8452_WRITE_MASK 0xFE +#define MMA8452_READ_MASK 0x01 + +#define MMA8452_BIT_DEPTH_MASK 0xFD +#define MMA8452_BIT_DEPTH_MASK_SHIFT 0x01 + class MMA8452 { public: - enum OperationalMode { - MODE_STANDBY, - MODE_ACTIVE_2G, - MODE_ACTIVE_4G, - MODE_ACTIVE_8G + enum DynamicRange { + DYNAMIC_RANGE_2G=0x00, + DYNAMIC_RANGE_4G, + DYNAMIC_RANGE_8G, + DYNAMIC_RANGE_UNKNOWN }; enum BitDepth { - BIT_DEPTH_8, - BIT_DEPTH_12 + BIT_DEPTH_12=0x00, + BIT_DEPTH_8 // 1 sets fast read mode, hence the inversion }; + + enum DataRateHz { + RATE_800=0x00, + RATE_400, + RATE_200, + RATE_100, + RATE_50, + RATE_12_5, + RATE_6_25, + RATE_1_563, + RATE_UNKNOWN + }; + + //void setDynamicRange( /** Create an accelerometer object connected to the specified I2C object * @@ -145,26 +181,8 @@ * */ ~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 getSystemMode(int *dst); - /** 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 getStatus(int *dst); + //int setOperationMode(OperationMode m); /** Activate the MMA8452 (required) @@ -195,7 +213,7 @@ * This will return the state of the control register 1. This holds and sets values for auto wake, sleep mode * output data rate, fast read mode and active/standby. More info on 6.7 of pdf for MMA8452 Freescale doc. */ - int get_CTRL_Reg1(int* dst); + int get_CTRL_Reg1(char* dst); /** Initialization of device MMA8452 (required) @@ -208,7 +226,7 @@ * return 0 for success or * return 1 for failure. */ - int getDeviceID(int *dst); + int getDeviceID(char* dst); int read_y(); @@ -246,24 +264,36 @@ * @param addr The internal registeraddress of the MMA8452 * @returns The value of the register */ - int readRegister(char addr, int *dst); + int readRegister(char addr, char *dst); + + int readRegister(char addr, char *dst, int nbytes); /** Write to specified MMA8452 register * * @param addr The internal registeraddress of the MMA8452 * @param data New value of the register */ - void writeRegister(char addr, char data); + int writeRegister(char addr, char data); + int writeRegister(char addr, char *data, int nbytes); + + int logicalANDRegister(char addr, char mask); + int logicalORRegister(char addr, char mask); + int logicalXORRegister(char addr, char mask); - int setOperationalMode(OperationalMode m); - int setBitDepth(BitDepth b); + int setDynamicRange(DynamicRange range, int toggleActivation=1); + int setBitDepth(BitDepth depth, int toggleActivation=1); + int setDataRate(DataRateHz dataRate, int toggleActivation=1); + DynamicRange getDynamicRange(); + DataRateHz getDataRate(); + + void debugRegister(char reg); private: int readRaw(char src, char *dst, int len); - + int maskAndApplyRegister(char reg, char mask, char value, int toggleActivation); - I2C m_i2c; - int m_frequency; + I2C _i2c; + int _frequency; int _readAddress; int _writeAddress;