Xtrinsic MMA8451Q 3-Axis, 14-bit/8-bit Digital Accelerometer
Dependents: test_MMA8451Q afero_node_suntory_171018 testSensor Vytah ... more
Revision 0:5a09d01c6a2c, committed 2015-12-25
- Comitter:
- Rhyme
- Date:
- Fri Dec 25 07:21:49 2015 +0000
- Commit message:
- First commit, only the minimum functions have been implemented.
Changed in this revision
MMA8451Q.cpp | Show annotated file Show diff for this revision Revisions of this file |
MMA8451Q.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 5a09d01c6a2c MMA8451Q.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA8451Q.cpp Fri Dec 25 07:21:49 2015 +0000 @@ -0,0 +1,128 @@ +/** + * MMA8451Q 3-Axis, 14-bit/8-bit Digital Accelerometer + */ + +#include "mbed.h" +#include "MMA8451Q.h" + +#define REG_STATUS 0x00 // when F_MODE = 00 +#define REG_FIFO_STATUS 0x00 // when F_MODE > 0 +#define REG_XYZ_FIFO 0x01 // Root pointer to XYZ FIFO data +#define REG_OUT_X_MSB 0x01 // 8 MSBs of 14-bit sample +#define REG_OUT_X_LSB 0x02 // 6 LSBs of 14-bit sample +#define REG_OUT_Y_MSB 0x03 +#define REG_OUT_Y_LSB 0x04 +#define REG_OUT_Z_MSB 0x05 +#define REG_OUT_Z_LSB 0x06 +#define REG_F_SETUP 0x09 // FIFO setup +#define REG_TRIG_CFG 0x0A // Map of FIFO daa capture events +#define REG_SYSMOD 0x0B // Current System Mode +#define REG_INT_SOURCE 0x0C // Interrupt status +#define REG_WHO_AM_I 0x0D // Device ID (0x1A) +#define REG_XYZ_DATA_CFG 0x0E // Dynamic Range Settings +#define REG_HP_FILTER_CUTOFF 0x0F // Cutoff freq is set to 16Hz@800Hz +#define REG_PL_STATUS 0x10 // Landscape/Portrait orientation status +#define REG_PL_CFG 0x11 // Landscape/Portrait configuration +#define REG_PL_COUNT 0x12 // Landscape/Portrait debounce counter +#define REG_PL_BF_ZCOMP 0x13 // Back/Front, Z-Lock Trip threshold +#define REG_P_L_THS_REG 0x14 // Portrait to Landscape Trip Angle is 29 degree +#define REG_FF_MT_CFG 0x15 // Freefall/Motion function block configuration +#define REG_FF_MT_SRC 0x16 // Freefall/Motion event source register +#define REG_FF_MT_THS 0x17 // Freefall/Motion threshold register +#define REG_FF_MT_COUNT 0x18 // Freefall/Motion debounce counter +// TRANSIENT +#define REG_TRANSIENT_CFG 0x1D // Transient functional block configuration +#define REG_TRANSIENT_SRC 0x1E // Transient event status register +#define REG_TRANSIENT_THS 0x1F // Transient event threshold +#define REG_TRANSIENT_COUNT 0x20 // Transient debounce counter +// PULSE +#define REG_PULSE_CFG 0x21 // ELE, Double_XYZ or Single_XYZ +#define REG_PULSE_SRC 0x22 // EA, Double_XYZ or Single_XYZ +#define REG_PULSE_THSX 0x23 // X pulse threshold +#define REG_PULSE_THSY 0x24 // Y pulse threshold +#define REG_PULSE_THSZ 0x25 // Z pulse threshold +#define REG_PULSE_TMLT 0x26 // Time limit for pulse +#define REG_PULSE_LTCY 0x27 // Latency time for 2nd pulse +#define REG_PULSE_WIND 0x28 // Window time for 2nd pulse +#define REG_ASLP_COUNT 0x29 // Counter setting for Auto-SLEEP +// Control Registers +#define REG_CTRL_REG1 0x2A // ODR = 800Hz, STANDBY Mode +#define REG_CTRL_REG2 0x2B // Sleep Enable, OS Modes, RST, ST +#define REG_CTRL_REG3 0x2C // Wake from Sleep, IPOL, PP_OD +#define REG_CTRL_REG4 0x2D // Interrupt enable register +#define REG_CTRL_REG5 0x2E // Interrupt pin (INT1/INT2) map +// User Offset +#define REG_OFF_X 0x2F // X-axis offset adjust +#define REG_OFF_Y 0x30 // Y-axis offset adjust +#define REG_OFF_Z 0x31 // Z-axis offset adjust + +// Value definitions +#define BIT_TRIG_TRANS 0x20 // Transient interrupt trigger bit +#define BIT_TRIG_LNDPRT 0x10 // Landscape/Portrati Orientation +#define BIT_TRIG_PULSE 0x08 // Pulse interrupt trigger bit +#define BIT_TRIG_FF_MT 0x04 // Freefall/Motion trigger bit + +MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr<<1) { + // activate the peripheral + uint8_t data[2] = {REG_CTRL_REG1, 0x01}; + writeRegs(data, 2); +} + +MMA8451Q::~MMA8451Q() { } + +void MMA8451Q::readRegs(int addr, uint8_t * data, int len) +{ + char t[1] = {addr}; + m_i2c.write(m_addr, t, 1, true); + m_i2c.read(m_addr, (char *)data, len); +} + +void MMA8451Q::writeRegs(uint8_t * data, int len) +{ + m_i2c.write(m_addr, (char *)data, len); +} + +int16_t MMA8451Q::getRawData(uint8_t addr) +{ + int16_t value ; + uint8_t data[2] ; + readRegs(addr, data, 2) ; + value = ((int16_t)((data[0] << 8) | data[1])) >> 2 ; + return( value ) ; +} + +int16_t MMA8451Q::getRawX(void) +{ + int16_t value ; + value = getRawData(REG_OUT_X_MSB) ; + return( value ) ; +} + +int16_t MMA8451Q::getRawY(void) +{ + int16_t value ; + value = getRawData(REG_OUT_Y_MSB) ; + return( value ) ; +} + +int16_t MMA8451Q::getRawZ(void) +{ + int16_t value ; + value = getRawData(REG_OUT_Z_MSB) ; + return( value ) ; +} + +float MMA8451Q::getAccX(void) +{ + return(((float)getRawX())/4096.0) ; +} + +float MMA8451Q::getAccY(void) +{ + return(((float)getRawY())/4096.0) ; +} + +float MMA8451Q::getAccZ(void) +{ + return(((float)getRawZ())/4096.0) ; +} \ No newline at end of file
diff -r 000000000000 -r 5a09d01c6a2c MMA8451Q.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA8451Q.h Fri Dec 25 07:21:49 2015 +0000 @@ -0,0 +1,41 @@ +/** + * MMA8451Q 3-Axis, 14-bit/8-bit Digital Accelerometer + */ +#ifndef _MMA8451Q_H_ +#define _MMA8451Q_H_ + +#include "mbed.h" + +class MMA8451Q { +public: + /** + * MMA8451Q constructor + * + * @param sda SDA pin + * @param sdl SCL pin + * @param addr 7bit addr of the I2C peripheral + */ + MMA8451Q(PinName sda, PinName scl, int addr); + + /** + * MMA8451Q destructor + */ + ~MMA8451Q(); + + int16_t getRawData(uint8_t addr) ; + int16_t getRawX(void) ; + int16_t getRawY(void) ; + int16_t getRawZ(void) ; + + float getAccX(void) ; + float getAccY(void) ; + float getAccZ(void) ; + +private: + I2C m_i2c; + int m_addr; + void readRegs(int addr, uint8_t * data, int len); + void writeRegs(uint8_t * data, int len); +} ; + +#endif /* _MMA8451Q_H_ */ \ No newline at end of file