KLUJA-SSD447-Hw8.1
Fork of MMA8451Q8 by
Revision 8:be66db51aa76, committed 2017-03-08
- Comitter:
- kennylujan42
- Date:
- Wed Mar 08 17:46:27 2017 +0000
- Parent:
- 7:7812354ef684
- Commit message:
- KLUJ-SSD447-HW8.1
Changed in this revision
MMA8451Q8.cpp | Show annotated file Show diff for this revision Revisions of this file |
MMA8451Q8.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 7812354ef684 -r be66db51aa76 MMA8451Q8.cpp --- a/MMA8451Q8.cpp Wed Feb 22 15:35:47 2017 +0000 +++ b/MMA8451Q8.cpp Wed Mar 08 17:46:27 2017 +0000 @@ -33,7 +33,14 @@ #define MAX_4G 0x01 #define MAX_8G 0x02 -#define GSCALING 1024.0 +//#define GSCALING 1024.0 + +#define NUM_DATA 2 +#define GSCALING 1024.0 +#define ADDRESS_INDEX 0 +#define DATA_INDEX 1 + +float gScaling[3] = {4095.0,2048.0,1024.0};/////////////// MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) { // activate the peripheral @@ -43,11 +50,30 @@ MMA8451Q::~MMA8451Q() { } +void MMA8451Q::setStandbyMode(){ +#define ACTIVEMASK 0x01 + uint8_t registerData[1]; + uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00}; + + readRegs(REG_CTRL_REG_1, registerData, 1); + data[1] = registerData[0] & ~ACTIVEMASK; + writeRegs(data, NUM_DATA); +} +void MMA8451Q::setActiveMode(){ + #define ACTIVEMASK 0x01 + uint8_t registerData[1]; + uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00}; + + readRegs(REG_CTRL_REG_1, registerData, 1); + data[1] = registerData[0] | ACTIVEMASK; + writeRegs(data, NUM_DATA); + } uint8_t MMA8451Q::getWhoAmI() { uint8_t who_am_i = 0; readRegs(REG_WHO_AM_I, &who_am_i, 1); return who_am_i; } +/* void MMA8451Q::setGLimit() { uint8_t data[2] = {REG_CTRL_REG_1, 0x00}; writeRegs(data, 2); // put in standby @@ -57,8 +83,30 @@ data[0] = REG_CTRL_REG_1; data[1] = 0x01; writeRegs(data, 2); // make active +}*/ +void MMA8451Q::setGLimit(int gSelect){ + + uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00}; + gChosen = gSelect; + setStandbyMode(); + data[ADDRESS_INDEX] = XYZ_DATA_CFG; + data[DATA_INDEX] = gChosen; + writeRegs(data, 2); + setActiveMode(); + } +int16_t MMA8451Q::getAccAxis(uint8_t addr){ + int16_t acc; + uint8_t res[2]; + readRegs(addr, res, 2); + + acc = (res[0] << 6) | (res[1] >> 2); + if (acc > UINT14_MAX/2) + acc -= UINT14_MAX; + + return acc; +} float MMA8451Q::getAccX() { return (float(getAccAxis(REG_OUT_X_MSB))/GSCALING); } @@ -76,7 +124,7 @@ res[1] = getAccY(); res[2] = getAccZ(); } - +/* int16_t MMA8451Q::getAccAxis(uint8_t addr) { int16_t acc; uint8_t res[2]; @@ -88,7 +136,7 @@ return acc; } - +*/ void MMA8451Q::readRegs(int addr, uint8_t * data, int len) { char t[1] = {addr}; m_i2c.write(m_addr, t, 1, true);
diff -r 7812354ef684 -r be66db51aa76 MMA8451Q8.h --- a/MMA8451Q8.h Wed Feb 22 15:35:47 2017 +0000 +++ b/MMA8451Q8.h Wed Mar 08 17:46:27 2017 +0000 @@ -98,15 +98,18 @@ */ void getAccAllAxis(float * res); + int16_t getAccAxis(uint8_t addr); I2C m_i2c; int m_addr; + int gChosen; + void setGLimit(int gSelect); + void setStandbyMode(); + void setActiveMode(); void readRegs(int addr, uint8_t * data, int len); void writeRegs(uint8_t * data, int len); - void setGLimit(); private: - int16_t getAccAxis(uint8_t addr); };