,,

Fork of Application by Daniel Sygut

Committer:
Zaitsev
Date:
Thu Feb 15 12:02:18 2018 +0000
Revision:
14:3eee9e20da88
Parent:
13:3dfb24be0612
Child:
15:2a20c3d2616e
l

Who changed what in which revision?

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