Rev 0.1 Simple operation of just X, Y, Z values in floating point G's

Dependents:   Hello_FXLS8471Q Multi-Sensor sensor AerCloud_MutliTech_Socket_Modem_Example ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FXLS8471Q.cpp Source File

FXLS8471Q.cpp

00001 /* Copyright (c) 2010-2011 mbed.org, MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 */
00018 
00019 
00020 #include "FXLS8471Q.h"
00021 #define UINT14_MAX        16383
00022 
00023 FXLS8471Q::FXLS8471Q(PinName mosi, PinName miso, PinName sck, PinName cs) : _spi(mosi, miso, sck), _spi_cs(cs)
00024 {
00025     _spi_cs = 1;
00026     wait(0.1);
00027     begin();
00028 }
00029 
00030 void FXLS8471Q::RegWrite( int reg, int *d, int len)
00031 {
00032     int ob[2];
00033     int c = 0;
00034     ob[1] = 0;
00035     ob[0] = reg + 128;
00036     _spi_cs = 0;
00037     _spi.write(ob[0]);
00038     _spi.write(ob[1]);
00039     while(c < len) _spi.write(d[c++]);
00040     _spi_cs = 1;
00041 }
00042 
00043 void FXLS8471Q::RegRead( int reg, int *d, int len)
00044 {
00045     int ob[2];
00046     int c = 0;
00047     ob[0] = reg;
00048     ob[1] = 0;
00049     _spi_cs = 0;
00050     _spi.write(ob[0]);
00051     _spi.write(ob[1]);
00052     while(c < len) d[c++] = _spi.write(0xff);
00053     _spi_cs = 1;
00054 }
00055 
00056 char FXLS8471Q::getWhoAmI(void)
00057 {
00058     int d;
00059     RegRead( FXLS8471Q_WHOAMI, &d, 1);
00060     return((char) d);
00061 }
00062 
00063 
00064 void FXLS8471Q::begin(void)
00065 {
00066     int databyte;
00067 
00068     // write 0000 0000 = 0x00 to accelerometer control register 1 to place FXLS8471Q into
00069     // standby
00070     // [7-1] = 0000 000
00071     // [0]: active=0
00072     databyte = 0x00;
00073     RegWrite(FXLS8471Q_CTRL_REG1, &databyte, 1);
00074 
00075     // write 0000 0001= 0x01 to XYZ_DATA_CFG register
00076     // [7]: reserved
00077     // [6]: reserved
00078     // [5]: reserved
00079     // [4]: hpf_out=0
00080     // [3]: reserved
00081     // [2]: reserved
00082     // [1-0]: fs=00 for accelerometer range of +/-2g with 0.244mg/LSB
00083     databyte = 0x00;
00084     RegWrite(FXLS8471Q_XYZ_DATA_CFG, &databyte, 1);
00085 
00086     // write 0001 0101b = 0x15 to accelerometer control register 1
00087     // [7-6]: aslp_rate=00
00088     // [5-3]: dr=010 for 200Hz data rate
00089     // [2]: lnoise=1 for low noise mode
00090     // [1]: f_read=0 for normal 16 bit reads
00091     // [0]: active=1 to take the part out of standby and enable sampling
00092     databyte = 0x15;
00093     RegWrite(FXLS8471Q_CTRL_REG1, &databyte, 1);
00094 }
00095 
00096 void FXLS8471Q::ReadXYZ(float * fdata)
00097 {
00098     int raw[7];
00099     int16_t ix, iy, iz;
00100     RegRead(FXLS8471Q_STATUS, raw, 7);
00101 
00102     ix = ((raw[1] * 256) + raw[2]);// / 4;
00103     iy = ((raw[3] * 256) + raw[4]);// / 4;
00104     iz = ((raw[5] * 256) + raw[6]);// / 4;
00105     fdata[0] = ((float) ix) / 16384.0;
00106     fdata[1] = ((float) iy) / 16384.0;
00107     fdata[2] = ((float) iz) / 16384.0;
00108 }
00109 
00110 void FXLS8471Q::ReadXYZraw(int16_t * d)
00111 {
00112     int res[6];
00113     int16_t acc;
00114     RegRead(FXLS8471Q_OUT_X_MSB, res, 6);
00115 
00116     acc = (res[0] << 6) | (res[1] >> 2);
00117     if (acc > UINT14_MAX/2)
00118         acc -= UINT14_MAX;
00119     d[0] = acc;
00120     acc = (res[2] << 6) | (res[3] >> 2);
00121     if (acc > UINT14_MAX/2)
00122         acc -= UINT14_MAX;
00123     d[1] = acc;
00124     acc = (res[4] << 6) | (res[5] >> 2);
00125     if (acc > UINT14_MAX/2)
00126         acc -= UINT14_MAX;
00127     d[2] = acc;
00128 }
00129