Darley Gonzalez
/
I2C_Slave
Programa
MMA8451Q.cpp@0:782b008e3b3a, 2021-03-02 (annotated)
- Committer:
- darley_gonzalez
- Date:
- Tue Mar 02 22:37:23 2021 +0000
- Revision:
- 0:782b008e3b3a
I2c Slave
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
darley_gonzalez | 0:782b008e3b3a | 1 | #include "MMA8451Q.h" |
darley_gonzalez | 0:782b008e3b3a | 2 | |
darley_gonzalez | 0:782b008e3b3a | 3 | #define REG_WHO_AM_I 0x0D |
darley_gonzalez | 0:782b008e3b3a | 4 | #define REG_CTRL_REG_1 0x2A |
darley_gonzalez | 0:782b008e3b3a | 5 | #define REG_OUT_X_MSB 0x01 |
darley_gonzalez | 0:782b008e3b3a | 6 | #define REG_OUT_Y_MSB 0x03 |
darley_gonzalez | 0:782b008e3b3a | 7 | #define REG_OUT_Z_MSB 0x05 |
darley_gonzalez | 0:782b008e3b3a | 8 | |
darley_gonzalez | 0:782b008e3b3a | 9 | #define UINT14_MAX 16383 |
darley_gonzalez | 0:782b008e3b3a | 10 | |
darley_gonzalez | 0:782b008e3b3a | 11 | MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) { |
darley_gonzalez | 0:782b008e3b3a | 12 | // activate the peripheral |
darley_gonzalez | 0:782b008e3b3a | 13 | uint8_t data[2] = {REG_CTRL_REG_1, 0x01}; |
darley_gonzalez | 0:782b008e3b3a | 14 | writeRegs(data, 2); |
darley_gonzalez | 0:782b008e3b3a | 15 | } |
darley_gonzalez | 0:782b008e3b3a | 16 | |
darley_gonzalez | 0:782b008e3b3a | 17 | MMA8451Q::~MMA8451Q() { } |
darley_gonzalez | 0:782b008e3b3a | 18 | |
darley_gonzalez | 0:782b008e3b3a | 19 | uint8_t MMA8451Q::getWhoAmI() { |
darley_gonzalez | 0:782b008e3b3a | 20 | uint8_t who_am_i = 0; |
darley_gonzalez | 0:782b008e3b3a | 21 | readRegs(REG_WHO_AM_I, &who_am_i, 1); |
darley_gonzalez | 0:782b008e3b3a | 22 | return who_am_i; |
darley_gonzalez | 0:782b008e3b3a | 23 | } |
darley_gonzalez | 0:782b008e3b3a | 24 | |
darley_gonzalez | 0:782b008e3b3a | 25 | float MMA8451Q::getAccX() { |
darley_gonzalez | 0:782b008e3b3a | 26 | return (float(getAccAxis(REG_OUT_X_MSB))/4096.0); |
darley_gonzalez | 0:782b008e3b3a | 27 | } |
darley_gonzalez | 0:782b008e3b3a | 28 | |
darley_gonzalez | 0:782b008e3b3a | 29 | float MMA8451Q::getAccY() { |
darley_gonzalez | 0:782b008e3b3a | 30 | return (float(getAccAxis(REG_OUT_Y_MSB))/4096.0); |
darley_gonzalez | 0:782b008e3b3a | 31 | } |
darley_gonzalez | 0:782b008e3b3a | 32 | |
darley_gonzalez | 0:782b008e3b3a | 33 | float MMA8451Q::getAccZ() { |
darley_gonzalez | 0:782b008e3b3a | 34 | return (float(getAccAxis(REG_OUT_Z_MSB))/4096.0); |
darley_gonzalez | 0:782b008e3b3a | 35 | } |
darley_gonzalez | 0:782b008e3b3a | 36 | |
darley_gonzalez | 0:782b008e3b3a | 37 | void MMA8451Q::getAccAllAxis(float * res) { |
darley_gonzalez | 0:782b008e3b3a | 38 | res[0] = getAccX(); |
darley_gonzalez | 0:782b008e3b3a | 39 | res[1] = getAccY(); |
darley_gonzalez | 0:782b008e3b3a | 40 | res[2] = getAccZ(); |
darley_gonzalez | 0:782b008e3b3a | 41 | } |
darley_gonzalez | 0:782b008e3b3a | 42 | |
darley_gonzalez | 0:782b008e3b3a | 43 | int16_t MMA8451Q::getAccAxis(uint8_t addr) { |
darley_gonzalez | 0:782b008e3b3a | 44 | int16_t acc; |
darley_gonzalez | 0:782b008e3b3a | 45 | uint8_t res[2]; |
darley_gonzalez | 0:782b008e3b3a | 46 | readRegs(addr, res, 2); |
darley_gonzalez | 0:782b008e3b3a | 47 | |
darley_gonzalez | 0:782b008e3b3a | 48 | acc = (res[0] << 6) | (res[1] >> 2); |
darley_gonzalez | 0:782b008e3b3a | 49 | if (acc > UINT14_MAX/2) |
darley_gonzalez | 0:782b008e3b3a | 50 | acc -= UINT14_MAX; |
darley_gonzalez | 0:782b008e3b3a | 51 | |
darley_gonzalez | 0:782b008e3b3a | 52 | return acc; |
darley_gonzalez | 0:782b008e3b3a | 53 | } |
darley_gonzalez | 0:782b008e3b3a | 54 | |
darley_gonzalez | 0:782b008e3b3a | 55 | void MMA8451Q::readRegs(int addr, uint8_t * data, int len) { |
darley_gonzalez | 0:782b008e3b3a | 56 | char t[1] = {0}; |
darley_gonzalez | 0:782b008e3b3a | 57 | t[1] =addr; |
darley_gonzalez | 0:782b008e3b3a | 58 | m_i2c.write(m_addr, t, 1, true); |
darley_gonzalez | 0:782b008e3b3a | 59 | m_i2c.read(m_addr, (char *)data, len); |
darley_gonzalez | 0:782b008e3b3a | 60 | } |
darley_gonzalez | 0:782b008e3b3a | 61 | |
darley_gonzalez | 0:782b008e3b3a | 62 | void MMA8451Q::writeRegs(uint8_t * data, int len) { |
darley_gonzalez | 0:782b008e3b3a | 63 | m_i2c.write(m_addr, (char *)data, len); |
darley_gonzalez | 0:782b008e3b3a | 64 | } |