,,
Fork of Application by
main.cpp@13:3dfb24be0612, 2018-02-15 (annotated)
- Committer:
- Zaitsev
- Date:
- Thu Feb 15 11:30:54 2018 +0000
- Revision:
- 13:3dfb24be0612
- Parent:
- 12:3a30cdffa27c
- Child:
- 14:3eee9e20da88
Initial commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 7:5e693654d5b4 | 1 | #include "mbed.h" |
danix | 12:3a30cdffa27c | 2 | #include "USBMouse.h" |
samux | 7:5e693654d5b4 | 3 | #include "USBSerial.h" |
danix | 12:3a30cdffa27c | 4 | #include "MMA8451Q.h" |
Zaitsev | 10:41552d038a69 | 5 | |
Zaitsev | 13:3dfb24be0612 | 6 | |
Zaitsev | 13:3dfb24be0612 | 7 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
Zaitsev | 13:3dfb24be0612 | 8 | #define REG_WHO_AM_I 0x0D |
Zaitsev | 13:3dfb24be0612 | 9 | #define REG_CTRL_REG_1 0x2A |
Zaitsev | 13:3dfb24be0612 | 10 | #define REG_OUT_X_MSB 0x01 |
Zaitsev | 13:3dfb24be0612 | 11 | #define REG_OUT_Y_MSB 0x03 |
Zaitsev | 13:3dfb24be0612 | 12 | #define REG_OUT_Z_MSB 0x05 |
Zaitsev | 13:3dfb24be0612 | 13 | #define UINT14_MAX 16383 |
Zaitsev | 13:3dfb24be0612 | 14 | |
Zaitsev | 10:41552d038a69 | 15 | DigitalOut myled(LED1); |
danix | 12:3a30cdffa27c | 16 | USBMouse mouse; |
Zaitsev | 13:3dfb24be0612 | 17 | I2C m_i2c(PTE25,PTE24); |
Zaitsev | 13:3dfb24be0612 | 18 | |
Zaitsev | 13:3dfb24be0612 | 19 | |
Zaitsev | 13:3dfb24be0612 | 20 | uint8_t getWhoAmI(); |
Zaitsev | 13:3dfb24be0612 | 21 | float getAccX(); |
Zaitsev | 13:3dfb24be0612 | 22 | float getAccY(); |
Zaitsev | 13:3dfb24be0612 | 23 | float getAccZ(); |
Zaitsev | 13:3dfb24be0612 | 24 | void getAccAllAxis(float * res); |
Zaitsev | 13:3dfb24be0612 | 25 | void readRegs(int addr, uint8_t * data, int len); |
Zaitsev | 13:3dfb24be0612 | 26 | void writeRegs(uint8_t * data, int len); |
Zaitsev | 13:3dfb24be0612 | 27 | int16_t getAccAxis(uint8_t addr); |
danix | 12:3a30cdffa27c | 28 | |
Zaitsev | 10:41552d038a69 | 29 | int main() { |
danix | 12:3a30cdffa27c | 30 | float x, y, z; |
danix | 12:3a30cdffa27c | 31 | |
danix | 12:3a30cdffa27c | 32 | while (1) { |
Zaitsev | 13:3dfb24be0612 | 33 | x = (getAccX()*10); |
Zaitsev | 13:3dfb24be0612 | 34 | y = (getAccY()*10); |
Zaitsev | 13:3dfb24be0612 | 35 | z = (getAccZ()*10); |
danix | 12:3a30cdffa27c | 36 | printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n", x, y, z); |
danix | 12:3a30cdffa27c | 37 | mouse.move(x, y); |
danix | 12:3a30cdffa27c | 38 | wait(0.01); |
samux | 7:5e693654d5b4 | 39 | } |
Zaitsev | 13:3dfb24be0612 | 40 | } |
Zaitsev | 13:3dfb24be0612 | 41 | |
Zaitsev | 13:3dfb24be0612 | 42 | |
Zaitsev | 13:3dfb24be0612 | 43 | void acc_Init() |
Zaitsev | 13:3dfb24be0612 | 44 | { |
Zaitsev | 13:3dfb24be0612 | 45 | uint8_t data[2] = {REG_CTRL_REG_1, 0x01}; |
Zaitsev | 13:3dfb24be0612 | 46 | writeRegs(data, 2); |
Zaitsev | 13:3dfb24be0612 | 47 | } |
Zaitsev | 13:3dfb24be0612 | 48 | |
Zaitsev | 13:3dfb24be0612 | 49 | uint8_t getWhoAmI() |
Zaitsev | 13:3dfb24be0612 | 50 | { |
Zaitsev | 13:3dfb24be0612 | 51 | uint8_t who_am_i = 0; |
Zaitsev | 13:3dfb24be0612 | 52 | readRegs(REG_WHO_AM_I, &who_am_i, 1); |
Zaitsev | 13:3dfb24be0612 | 53 | return who_am_i; |
Zaitsev | 13:3dfb24be0612 | 54 | } |
Zaitsev | 13:3dfb24be0612 | 55 | float getAccX() |
Zaitsev | 13:3dfb24be0612 | 56 | { |
Zaitsev | 13:3dfb24be0612 | 57 | return (float(getAccAxis(REG_OUT_X_MSB))/4096.0); |
Zaitsev | 13:3dfb24be0612 | 58 | } |
Zaitsev | 13:3dfb24be0612 | 59 | float getAccY() |
Zaitsev | 13:3dfb24be0612 | 60 | { |
Zaitsev | 13:3dfb24be0612 | 61 | return (float(getAccAxis(REG_OUT_Y_MSB))/4096.0); |
Zaitsev | 13:3dfb24be0612 | 62 | } |
Zaitsev | 13:3dfb24be0612 | 63 | float getAccZ() { |
Zaitsev | 13:3dfb24be0612 | 64 | return (float(getAccAxis(REG_OUT_Z_MSB))/4096.0); |
Zaitsev | 13:3dfb24be0612 | 65 | } |
Zaitsev | 13:3dfb24be0612 | 66 | void getAccAllAxis(float * res) { |
Zaitsev | 13:3dfb24be0612 | 67 | res[0] = getAccX(); |
Zaitsev | 13:3dfb24be0612 | 68 | res[1] = getAccY(); |
Zaitsev | 13:3dfb24be0612 | 69 | res[2] = getAccZ(); |
Zaitsev | 13:3dfb24be0612 | 70 | } |
Zaitsev | 13:3dfb24be0612 | 71 | int16_t getAccAxis(uint8_t addr) { |
Zaitsev | 13:3dfb24be0612 | 72 | int16_t acc; |
Zaitsev | 13:3dfb24be0612 | 73 | uint8_t res[2]; |
Zaitsev | 13:3dfb24be0612 | 74 | readRegs(addr, res, 2); |
Zaitsev | 13:3dfb24be0612 | 75 | |
Zaitsev | 13:3dfb24be0612 | 76 | acc = (res[0] << 6) | (res[1] >> 2); |
Zaitsev | 13:3dfb24be0612 | 77 | if (acc > UINT14_MAX/2) |
Zaitsev | 13:3dfb24be0612 | 78 | acc -= UINT14_MAX; |
Zaitsev | 13:3dfb24be0612 | 79 | |
Zaitsev | 13:3dfb24be0612 | 80 | return acc; |
Zaitsev | 13:3dfb24be0612 | 81 | } |
Zaitsev | 13:3dfb24be0612 | 82 | |
Zaitsev | 13:3dfb24be0612 | 83 | void readRegs(int addr, uint8_t * data, int len) { |
Zaitsev | 13:3dfb24be0612 | 84 | char t[1] = {addr}; |
Zaitsev | 13:3dfb24be0612 | 85 | m_i2c.write(MMA8451_I2C_ADDRESS, t, 1, true); |
Zaitsev | 13:3dfb24be0612 | 86 | m_i2c.read(MMA8451_I2C_ADDRESS, (char *)data, len); |
Zaitsev | 13:3dfb24be0612 | 87 | } |
Zaitsev | 13:3dfb24be0612 | 88 | |
Zaitsev | 13:3dfb24be0612 | 89 | void writeRegs(uint8_t * data, int len) { |
Zaitsev | 13:3dfb24be0612 | 90 | m_i2c.write(MMA8451_I2C_ADDRESS, (char *)data, len); |
Zaitsev | 13:3dfb24be0612 | 91 | } |