Glimit

Fork of MMA8451Q8 by Stanley Cohen

Committer:
emilmont
Date:
Thu Oct 11 10:54:16 2012 +0000
Revision:
0:6149091f755d
Child:
1:d2630136d51e
Package samux acceleromiter library;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 0:6149091f755d 1 #include "MMA8451Q.h"
emilmont 0:6149091f755d 2
emilmont 0:6149091f755d 3 #define REG_WHO_AM_I 0x0D
emilmont 0:6149091f755d 4 #define REG_CTRL_REG_1 0x2A
emilmont 0:6149091f755d 5 #define REG_OUT_X_MSB 0x01
emilmont 0:6149091f755d 6 #define REG_OUT_Y_MSB 0x03
emilmont 0:6149091f755d 7 #define REG_OUT_Z_MSB 0x05
emilmont 0:6149091f755d 8
emilmont 0:6149091f755d 9 #define UINT14_MAX 16383
emilmont 0:6149091f755d 10
emilmont 0:6149091f755d 11 MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
emilmont 0:6149091f755d 12 // activate the peripheral
emilmont 0:6149091f755d 13 uint8_t data[2] = {REG_CTRL_REG_1, 0x01};
emilmont 0:6149091f755d 14 write_regs(data, 2);
emilmont 0:6149091f755d 15 }
emilmont 0:6149091f755d 16
emilmont 0:6149091f755d 17 MMA8451Q::~MMA8451Q() { }
emilmont 0:6149091f755d 18
emilmont 0:6149091f755d 19 uint8_t MMA8451Q::getWhoAmI() {
emilmont 0:6149091f755d 20 uint8_t who_am_i = 0;
emilmont 0:6149091f755d 21 read_regs(REG_WHO_AM_I, &who_am_i, 1);
emilmont 0:6149091f755d 22 return who_am_i;
emilmont 0:6149091f755d 23 }
emilmont 0:6149091f755d 24
emilmont 0:6149091f755d 25 int16_t MMA8451Q::getAccX() {
emilmont 0:6149091f755d 26 return getAccAxis(REG_OUT_X_MSB);
emilmont 0:6149091f755d 27 }
emilmont 0:6149091f755d 28
emilmont 0:6149091f755d 29 int16_t MMA8451Q::getAccY() {
emilmont 0:6149091f755d 30 return getAccAxis(REG_OUT_Y_MSB);
emilmont 0:6149091f755d 31 }
emilmont 0:6149091f755d 32
emilmont 0:6149091f755d 33 int16_t MMA8451Q::getAccZ() {
emilmont 0:6149091f755d 34 return getAccAxis(REG_OUT_Z_MSB);
emilmont 0:6149091f755d 35 }
emilmont 0:6149091f755d 36
emilmont 0:6149091f755d 37 void MMA8451Q::getAccAllAxis(int16_t * res) {
emilmont 0:6149091f755d 38 res[0] = getAccX();
emilmont 0:6149091f755d 39 res[1] = getAccY();
emilmont 0:6149091f755d 40 res[2] = getAccZ();
emilmont 0:6149091f755d 41 }
emilmont 0:6149091f755d 42
emilmont 0:6149091f755d 43 int16_t MMA8451Q::getAccAxis(uint8_t addr) {
emilmont 0:6149091f755d 44 int16_t acc;
emilmont 0:6149091f755d 45 uint8_t res[2];
emilmont 0:6149091f755d 46 read_regs(addr, res, 2);
emilmont 0:6149091f755d 47
emilmont 0:6149091f755d 48 acc = (res[0] << 6) | (res[1] >> 2);
emilmont 0:6149091f755d 49 if (acc > UINT14_MAX/2)
emilmont 0:6149091f755d 50 acc -= UINT14_MAX;
emilmont 0:6149091f755d 51
emilmont 0:6149091f755d 52 return acc;
emilmont 0:6149091f755d 53 }
emilmont 0:6149091f755d 54
emilmont 0:6149091f755d 55 void MMA8451Q::read_regs(int addr, uint8_t * data, int len) {
emilmont 0:6149091f755d 56 char t[1] = {addr};
emilmont 0:6149091f755d 57 m_i2c.write(m_addr, t, 1, true);
emilmont 0:6149091f755d 58 m_i2c.read(m_addr, (char *)data, len);
emilmont 0:6149091f755d 59 }
emilmont 0:6149091f755d 60
emilmont 0:6149091f755d 61 void MMA8451Q::write_regs(uint8_t * data, int len) {
emilmont 0:6149091f755d 62 m_i2c.write(m_addr, (char *)data, len);
emilmont 0:6149091f755d 63 }