A basic library for the FXOS8700Q combination accelerometer / magnetometer

Dependencies:   MotionSensor

Dependents:   K64F_eCompass_LCD Hello_FXOS8700Q rtos_compass K64F_eCompass ... more

This library supports the 6 axis combination Accelerometer / Magnetometer. Functions are provided to retrieve data in raw 16 bit signed integers or unit converted G's and micro-teslas

Committer:
JimCarver
Date:
Mon Apr 14 17:13:15 2014 +0000
Revision:
2:ab84f99086e5
Parent:
1:8b53edef272f
Minor feature change

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JimCarver 0:2562215f5bc0 1 #include "FXOS8700Q.h"
JimCarver 1:8b53edef272f 2 #define UINT14_MAX 16383
JimCarver 1:8b53edef272f 3
JimCarver 1:8b53edef272f 4
JimCarver 1:8b53edef272f 5 FXOS8700Q::FXOS8700Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
JimCarver 2:ab84f99086e5 6
JimCarver 2:ab84f99086e5 7 }
JimCarver 2:ab84f99086e5 8
JimCarver 2:ab84f99086e5 9
JimCarver 2:ab84f99086e5 10 FXOS8700Q::~FXOS8700Q() { }
JimCarver 2:ab84f99086e5 11
JimCarver 2:ab84f99086e5 12 void FXOS8700Q::enable(void) {
JimCarver 1:8b53edef272f 13 // activate the peripheral
JimCarver 1:8b53edef272f 14 uint8_t data[2] = {FXOS8700Q_CTRL_REG1, 0x00};
JimCarver 2:ab84f99086e5 15 m_i2c.frequency(400000);
JimCarver 1:8b53edef272f 16 writeRegs(data, 2);
JimCarver 1:8b53edef272f 17 data[0] = FXOS8700Q_M_CTRL_REG1;
JimCarver 1:8b53edef272f 18 data[1] = 0x1F;
JimCarver 1:8b53edef272f 19 writeRegs(data, 2);
JimCarver 1:8b53edef272f 20 data[0] = FXOS8700Q_M_CTRL_REG2;
JimCarver 1:8b53edef272f 21 data[1] = 0x20;
JimCarver 1:8b53edef272f 22 writeRegs(data, 2);
JimCarver 1:8b53edef272f 23 data[0] = FXOS8700Q_XYZ_DATA_CFG;
JimCarver 1:8b53edef272f 24 data[1] = 0x00;
JimCarver 1:8b53edef272f 25 writeRegs(data, 2);
JimCarver 1:8b53edef272f 26 data[0] = FXOS8700Q_CTRL_REG1;
JimCarver 1:8b53edef272f 27 data[1] = 0x19;//0x1D;
JimCarver 1:8b53edef272f 28 writeRegs(data, 2);
JimCarver 2:ab84f99086e5 29 }
JimCarver 2:ab84f99086e5 30
JimCarver 1:8b53edef272f 31 uint8_t FXOS8700Q::getWhoAmI() {
JimCarver 1:8b53edef272f 32 uint8_t who_am_i = 0;
JimCarver 1:8b53edef272f 33 readRegs(FXOS8700Q_WHOAMI, &who_am_i, 1);
JimCarver 1:8b53edef272f 34 return who_am_i;
JimCarver 1:8b53edef272f 35 }
JimCarver 1:8b53edef272f 36
JimCarver 1:8b53edef272f 37 float FXOS8700Q::getAccX() {
JimCarver 2:ab84f99086e5 38 return (float(getAccAxis(FXOS8700Q_OUT_X_MSB))/4096.0f);
JimCarver 0:2562215f5bc0 39 }
JimCarver 1:8b53edef272f 40
JimCarver 1:8b53edef272f 41 float FXOS8700Q::getAccY() {
JimCarver 2:ab84f99086e5 42 return (float(getAccAxis(FXOS8700Q_OUT_Y_MSB))/4096.0f);
JimCarver 1:8b53edef272f 43 }
JimCarver 1:8b53edef272f 44
JimCarver 1:8b53edef272f 45 float FXOS8700Q::getAccZ() {
JimCarver 2:ab84f99086e5 46 return (float(getAccAxis(FXOS8700Q_OUT_Z_MSB))/4096.0f);
JimCarver 1:8b53edef272f 47 }
JimCarver 1:8b53edef272f 48
JimCarver 1:8b53edef272f 49
JimCarver 1:8b53edef272f 50 void FXOS8700Q::getAccAllAxis(float * res) {
JimCarver 1:8b53edef272f 51 res[0] = getAccX();
JimCarver 1:8b53edef272f 52 res[1] = getAccY();
JimCarver 1:8b53edef272f 53 res[2] = getAccZ();
JimCarver 0:2562215f5bc0 54 }
JimCarver 0:2562215f5bc0 55
JimCarver 1:8b53edef272f 56 void FXOS8700Q::AccXYZraw(int16_t * d) {
JimCarver 1:8b53edef272f 57 int16_t acc;
JimCarver 1:8b53edef272f 58 uint8_t res[6];
JimCarver 1:8b53edef272f 59 readRegs(FXOS8700Q_OUT_X_MSB, res, 6);
JimCarver 0:2562215f5bc0 60
JimCarver 1:8b53edef272f 61 acc = (res[0] << 6) | (res[1] >> 2);
JimCarver 1:8b53edef272f 62 if (acc > UINT14_MAX/2)
JimCarver 1:8b53edef272f 63 acc -= UINT14_MAX;
JimCarver 1:8b53edef272f 64 d[0] = acc;
JimCarver 1:8b53edef272f 65 acc = (res[2] << 6) | (res[3] >> 2);
JimCarver 1:8b53edef272f 66 if (acc > UINT14_MAX/2)
JimCarver 1:8b53edef272f 67 acc -= UINT14_MAX;
JimCarver 1:8b53edef272f 68 d[1] = acc;
JimCarver 1:8b53edef272f 69 acc = (res[4] << 6) | (res[5] >> 2);
JimCarver 1:8b53edef272f 70 if (acc > UINT14_MAX/2)
JimCarver 1:8b53edef272f 71 acc -= UINT14_MAX;
JimCarver 1:8b53edef272f 72 d[2] = acc;
JimCarver 0:2562215f5bc0 73 }
JimCarver 0:2562215f5bc0 74
JimCarver 1:8b53edef272f 75 void FXOS8700Q::MagXYZraw(int16_t * d) {
JimCarver 1:8b53edef272f 76 int16_t acc;
JimCarver 1:8b53edef272f 77 uint8_t res[6];
JimCarver 1:8b53edef272f 78 readRegs(FXOS8700Q_M_OUT_X_MSB, res, 6);
JimCarver 1:8b53edef272f 79
JimCarver 1:8b53edef272f 80 d[0] = (res[0] << 8) | res[1];
JimCarver 1:8b53edef272f 81 d[1] = (res[2] << 8) | res[3];
JimCarver 1:8b53edef272f 82 d[2] = (res[4] << 8) | res[5];
JimCarver 1:8b53edef272f 83 }
JimCarver 1:8b53edef272f 84
JimCarver 1:8b53edef272f 85 int16_t FXOS8700Q::getAccAxis(uint8_t addr) {
JimCarver 1:8b53edef272f 86 int16_t acc;
JimCarver 1:8b53edef272f 87 uint8_t res[2];
JimCarver 1:8b53edef272f 88 readRegs(addr, res, 2);
JimCarver 0:2562215f5bc0 89
JimCarver 1:8b53edef272f 90 acc = (res[0] << 6) | (res[1] >> 2);
JimCarver 1:8b53edef272f 91 if (acc > UINT14_MAX/2)
JimCarver 1:8b53edef272f 92 acc -= UINT14_MAX;
JimCarver 1:8b53edef272f 93
JimCarver 1:8b53edef272f 94 return acc;
JimCarver 1:8b53edef272f 95 }
JimCarver 0:2562215f5bc0 96
JimCarver 1:8b53edef272f 97 void FXOS8700Q::readRegs(int addr, uint8_t * data, int len) {
JimCarver 1:8b53edef272f 98 char t[1] = {addr};
JimCarver 1:8b53edef272f 99 m_i2c.write(m_addr, t, 1, true);
JimCarver 1:8b53edef272f 100 m_i2c.read(m_addr, (char *)data, len);
JimCarver 0:2562215f5bc0 101 }
JimCarver 1:8b53edef272f 102
JimCarver 1:8b53edef272f 103 void FXOS8700Q::writeRegs(uint8_t * data, int len) {
JimCarver 2:ab84f99086e5 104 if(m_i2c.write(m_addr, (char *)data, len)) printf("WRite Error\r\n");
JimCarver 1:8b53edef272f 105 }
JimCarver 1:8b53edef272f 106
JimCarver 0:2562215f5bc0 107