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.cpp
- Revision:
- 17:6e4232c421c0
- Parent:
- 16:d6dde2318edc
- Child:
- 19:4d6cd7140a71
diff -r d6dde2318edc -r 6e4232c421c0 MMA8452.cpp --- a/MMA8452.cpp Wed Mar 05 16:47:13 2014 +0000 +++ b/MMA8452.cpp Wed Mar 05 17:01:00 2014 +0000 @@ -20,11 +20,16 @@ #include "MMA8452.h" #include "mbed.h" +#ifdef MMA8452_DEBUG extern Serial pc; +#define MMA8452_DBG(...) pc.printf(__VA_ARGS__); pc.printf("\r\n"); +#else +#define MMA8452_DBG(...) +#endif // Connect module at I2C address using I2C port pins sda and scl MMA8452::MMA8452(PinName sda, PinName scl, int frequency) : _i2c(sda, scl) , _frequency(frequency) { - DBG("Creating MMA8452"); + MMA8452_DBG("Creating MMA8452"); // set I2C frequency _i2c.frequency(_frequency); @@ -39,7 +44,7 @@ _dynamicRange = DYNAMIC_RANGE_UNKNOWN; setDynamicRange(DYNAMIC_RANGE_2G); - DBG("Done"); + MMA8452_DBG("Done"); } @@ -351,67 +356,69 @@ return readRegister(addr,dst,1); } +#ifdef MMA8452_DEBUG void MMA8452::debugRegister(char reg) { // get register value char v = 0; if(readRegister(reg,&v)) { - DBG("Error reading specified register"); + MMA8452_DBG("Error reading specified register"); return; } // print out details switch(reg) { case MMA8452_CTRL_REG_1: - DBG("CTRL_REG_1 has value: 0x%x",v); - DBG(" 7 ALSP_RATE_1: %d",(v&0x80)>>7); - DBG(" 6 ALSP_RATE_0: %d",(v&0x40)>>6); - DBG(" 5 DR2: %d", (v&0x20)>>5); - DBG(" 4 DR1: %d", (v&0x10)>>4); - DBG(" 3 DR0: %d", (v&0x08)>>3); - DBG(" 2 LNOISE: %d", (v&0x04)>>2); - DBG(" 1 FREAD: %d", (v&0x02)>>1); - DBG(" 0 ACTIVE: %d", (v&0x01)); + MMA8452_DBG("CTRL_REG_1 has value: 0x%x",v); + MMA8452_DBG(" 7 ALSP_RATE_1: %d",(v&0x80)>>7); + MMA8452_DBG(" 6 ALSP_RATE_0: %d",(v&0x40)>>6); + MMA8452_DBG(" 5 DR2: %d", (v&0x20)>>5); + MMA8452_DBG(" 4 DR1: %d", (v&0x10)>>4); + MMA8452_DBG(" 3 DR0: %d", (v&0x08)>>3); + MMA8452_DBG(" 2 LNOISE: %d", (v&0x04)>>2); + MMA8452_DBG(" 1 FREAD: %d", (v&0x02)>>1); + MMA8452_DBG(" 0 ACTIVE: %d", (v&0x01)); break; case MMA8452_XYZ_DATA_CFG: - DBG("XYZ_DATA_CFG has value: 0x%x",v); - DBG(" 7 Unused: %d", (v&0x80)>>7); - DBG(" 6 0: %d", (v&0x40)>>6); - DBG(" 5 0: %d", (v&0x20)>>5); - DBG(" 4 HPF_Out: %d",(v&0x10)>>4); - DBG(" 3 0: %d", (v&0x08)>>3); - DBG(" 2 0: %d", (v&0x04)>>2); - DBG(" 1 FS1: %d", (v&0x02)>>1); - DBG(" 0 FS0: %d", (v&0x01)); + MMA8452_DBG("XYZ_DATA_CFG has value: 0x%x",v); + MMA8452_DBG(" 7 Unused: %d", (v&0x80)>>7); + MMA8452_DBG(" 6 0: %d", (v&0x40)>>6); + MMA8452_DBG(" 5 0: %d", (v&0x20)>>5); + MMA8452_DBG(" 4 HPF_Out: %d",(v&0x10)>>4); + MMA8452_DBG(" 3 0: %d", (v&0x08)>>3); + MMA8452_DBG(" 2 0: %d", (v&0x04)>>2); + MMA8452_DBG(" 1 FS1: %d", (v&0x02)>>1); + MMA8452_DBG(" 0 FS0: %d", (v&0x01)); switch(v&0x03) { case 0: - DBG("Dynamic range: 2G"); + MMA8452_DBG("Dynamic range: 2G"); break; case 1: - DBG("Dynamic range: 4G"); + MMA8452_DBG("Dynamic range: 4G"); break; case 2: - DBG("Dynamic range: 8G"); + MMA8452_DBG("Dynamic range: 8G"); break; default: - DBG("Unknown dynamic range"); + MMA8452_DBG("Unknown dynamic range"); break; } break; case MMA8452_STATUS: - DBG("STATUS has value: 0x%x",v); - DBG(" 7 ZYXOW: %d",(v&0x80)>>7); - DBG(" 6 ZOW: %d", (v&0x40)>>6); - DBG(" 5 YOW: %d", (v&0x20)>>5); - DBG(" 4 XOW: %d", (v&0x10)>>4); - DBG(" 3 ZYXDR: %d",(v&0x08)>>3); - DBG(" 2 ZDR: %d", (v&0x04)>>2); - DBG(" 1 YDR: %d", (v&0x02)>>1); - DBG(" 0 XDR: %d", (v&0x01)); + MMA8452_DBG("STATUS has value: 0x%x",v); + MMA8452_DBG(" 7 ZYXOW: %d",(v&0x80)>>7); + MMA8452_DBG(" 6 ZOW: %d", (v&0x40)>>6); + MMA8452_DBG(" 5 YOW: %d", (v&0x20)>>5); + MMA8452_DBG(" 4 XOW: %d", (v&0x10)>>4); + MMA8452_DBG(" 3 ZYXDR: %d",(v&0x08)>>3); + MMA8452_DBG(" 2 ZDR: %d", (v&0x04)>>2); + MMA8452_DBG(" 1 YDR: %d", (v&0x02)>>1); + MMA8452_DBG(" 0 XDR: %d", (v&0x01)); break; default: - DBG("Unknown register address: 0x%x",reg); + MMA8452_DBG("Unknown register address: 0x%x",reg); break; } -} \ No newline at end of file +} +#endif \ No newline at end of file