NXP / FXLS8471

Dependents:   FRDMSTBC-A8471_SensorShield_HelloWorld

Fork of MMA8652FC by NXP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FXLS8471.cpp Source File

FXLS8471.cpp

00001 /*
00002  * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
00003  * Copyright 2016-2017 NXP
00004  *
00005  * Redistribution and use in source and binary forms, with or without modification,
00006  * are permitted provided that the following conditions are met:
00007  *
00008  * o Redistributions of source code must retain the above copyright notice, this list
00009  *   of conditions and the following disclaimer.
00010  *
00011  * o Redistributions in binary form must reproduce the above copyright notice, this
00012  *   list of conditions and the following disclaimer in the documentation and/or
00013  *   other materials provided with the distribution.
00014  *
00015  * o Neither the name of the copyright holder nor the names of its
00016  *   contributors may be used to endorse or promote products derived from this
00017  *   software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00020  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00023  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00026  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  */
00030 
00031 #include "FXLS8471.h"
00032  #include "mbed.h"
00033 
00034 FXLS8471::FXLS8471(PinName MOSI, PinName MISO, PinName CLK, PinName CS) : FXLS8471_spi(MOSI,MISO,CLK), FXLS8471_cs(CS)
00035  {
00036       FXLS8471_cs=1;
00037  }
00038     
00039  void FXLS8471::FXLS8471_config(void)
00040  {
00041    char d[2]; 
00042    d[0] = FXLS8471_CTRL_REG1;                     //Puts device in Standby mode
00043    FXLS8471_cs=0;
00044    FXLS8471_spi.write(d[0]+128); 
00045    FXLS8471_spi.write(0x00);  
00046    FXLS8471_spi.write(0x00);  
00047    FXLS8471_cs=1;
00048           
00049    
00050    d[0] = FXLS8471_CTRL_REG1;                     //Puts device in Active mode
00051    FXLS8471_cs=0;
00052    FXLS8471_spi.write(d[0]+128); 
00053    FXLS8471_spi.write(0x01);  
00054    FXLS8471_spi.write(0x01);  
00055    FXLS8471_cs=1;  
00056       
00057  }
00058  
00059   
00060  
00061  void FXLS8471::acquire_FXLS8471_data_g(float * a_data)
00062  {
00063   
00064    char data_bytes[7];
00065    char d[1];
00066    int count=1;
00067    d[0]=0x01;
00068    
00069    FXLS8471_cs=0;
00070    FXLS8471_spi.write(d[0]); 
00071    FXLS8471_spi.write(0x00);  
00072    while(count<7)
00073    {  data_bytes[count]= FXLS8471_spi.write(0xff);  count=count+1;
00074    }
00075    FXLS8471_cs=1;
00076    
00077    // a_data[0]= ((float)((int16_t)data_bytes[1]));
00078    a_data[0] =  ((float)((int16_t)(((data_bytes[1]*256) + (data_bytes[2])))>> 4) * 0.0009765);
00079    a_data[1] =  ((float)((int16_t)(((data_bytes[3]*256) + (data_bytes[4])))>> 4) * 0.0009765);
00080    a_data[2] =  ((float)((int16_t)(((data_bytes[5]*256) + (data_bytes[6])))>> 4) * 0.0009765);
00081    
00082  }
00083 
00084