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:
Sun Apr 13 21:22:58 2014 +0000
Revision:
1:8b53edef272f
Parent:
0:2562215f5bc0
Child:
2:ab84f99086e5
Child:
3:eb1271ef90bc
This version fixes bugs in the previous version

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