A basic library for the FXOS8700Q combination accelerometer / magnetometer
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
FXOS8700Q.cpp
- Committer:
- JimCarver
- Date:
- 2014-04-17
- Revision:
- 3:eb1271ef90bc
- Parent:
- 1:8b53edef272f
- Child:
- 4:be6abf9f2d59
File content as of revision 3:eb1271ef90bc:
/* Copyright (c) 2010-2011 mbed.org, MIT License * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software * and associated documentation files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "FXOS8700Q.h" #define UINT14_MAX 16383 FXOS8700Q::FXOS8700Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) { // activate the peripheral uint8_t data[2] = {FXOS8700Q_CTRL_REG1, 0x00}; writeRegs(data, 2); data[0] = FXOS8700Q_M_CTRL_REG1; data[1] = 0x1F; writeRegs(data, 2); data[0] = FXOS8700Q_M_CTRL_REG2; data[1] = 0x20; writeRegs(data, 2); data[0] = FXOS8700Q_XYZ_DATA_CFG; data[1] = 0x00; writeRegs(data, 2); data[0] = FXOS8700Q_CTRL_REG1; data[1] = 0x19;//0x1D; writeRegs(data, 2); } FXOS8700Q::~FXOS8700Q() { } uint8_t FXOS8700Q::getWhoAmI() { uint8_t who_am_i = 0; readRegs(FXOS8700Q_WHOAMI, &who_am_i, 1); return who_am_i; } float FXOS8700Q::getAccX() { return (float(getAccAxis(FXOS8700Q_OUT_X_MSB))/4096.0); } float FXOS8700Q::getAccY() { return (float(getAccAxis(FXOS8700Q_OUT_Y_MSB))/4096.0); } float FXOS8700Q::getAccZ() { return (float(getAccAxis(FXOS8700Q_OUT_Z_MSB))/4096.0); } void FXOS8700Q::getAccAllAxis(float * res) { res[0] = getAccX(); res[1] = getAccY(); res[2] = getAccZ(); } void FXOS8700Q::AccXYZraw(int16_t * d) { int16_t acc; uint8_t res[6]; readRegs(FXOS8700Q_OUT_X_MSB, res, 6); acc = (res[0] << 6) | (res[1] >> 2); if (acc > UINT14_MAX/2) acc -= UINT14_MAX; d[0] = acc; acc = (res[2] << 6) | (res[3] >> 2); if (acc > UINT14_MAX/2) acc -= UINT14_MAX; d[1] = acc; acc = (res[4] << 6) | (res[5] >> 2); if (acc > UINT14_MAX/2) acc -= UINT14_MAX; d[2] = acc; } void FXOS8700Q::MagXYZraw(int16_t * d) { int16_t acc; uint8_t res[6]; readRegs(FXOS8700Q_M_OUT_X_MSB, res, 6); d[0] = (res[0] << 8) | res[1]; d[1] = (res[2] << 8) | res[3]; d[2] = (res[4] << 8) | res[5]; } int16_t FXOS8700Q::getAccAxis(uint8_t addr) { int16_t acc; uint8_t res[2]; readRegs(addr, res, 2); acc = (res[0] << 6) | (res[1] >> 2); if (acc > UINT14_MAX/2) acc -= UINT14_MAX; return acc; } void FXOS8700Q::readRegs(int addr, uint8_t * data, int len) { char t[1] = {addr}; m_i2c.write(m_addr, t, 1, true); m_i2c.read(m_addr, (char *)data, len); } void FXOS8700Q::writeRegs(uint8_t * data, int len) { m_i2c.write(m_addr, (char *)data, len); }