This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/RM46L852: Interfacing ADXL345 accelerometer with RM46L852 controller.

Part Number: RM46L852
Other Parts Discussed in Thread: HALCOGEN

Tool/software: Code Composer Studio

Hello everyone,

I have been trying to interface accelerometer  ADXL345 ( https://www.analog.com/media/en/technical-documentation/data-sheets/ADXL345.pdf ) with RM46L852 controller ( http://www.ti.com/lit/ds/spns185c/spns185c.pdf ) using SPI communication (SPI4) for acquiring the accelerometer data from ADXL345. I am using HALCoGen to generate the SPI driver. Code Composer Studio (CCS) is used.

The results that are obtained are inconsistent and it picks up negative values when I place the accelerometer in positive Z axis. 

Please help me in this regard. Thank you.

The code in CCS is as follows:

/* USER CODE BEGIN (0) */
/* USER CODE END */
/* Include Files */
#include "sys_common.h"
/* USER CODE BEGIN (1) */
#include"spi.h"
#include"pinmux.h"
#include"stdio.h"
/* USER CODE END */
/* USER CODE BEGIN (2) */
uint16 X0,X1,Y0,Y1,Z0,Z1;
        uint16 i,j,k;
uint16 TX_Data_Master, RX_Data_Master;
        short int x,y,z;
        float Xg,Yg,Zg;
        float X[3200]={0x00};
        float Y[3200]={0x00};
        float Z[3200]={0x00};
/* USER CODE END */
int main(void)
{
/* USER CODE BEGIN (3) */
    uint16 TX_Data_Master[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    uint16 RX_Data_Master[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    spiDAT1_t dataconfig1_t;
            dataconfig1_t.CS_HOLD = FALSE; //DEFAULT CONFIGURATION FOR SPI
            dataconfig1_t.WDEL    = TRUE; //WAIT DELAY
            dataconfig1_t.DFSEL   = SPI_FMT_0;
            dataconfig1_t.CSNR    = 0xFE; // CHIP SELECT NUMBER
    /* {
                SPI_CS_NONE = 0xFFU,
                SPI_CS_0    = 0xFEU,
                SPI_CS_1    = 0xFDU,
                SPI_CS_2    = 0xFBU,
                SPI_CS_3    = 0xF7U,
                SPI_CS_4    = 0xEFU,
                SPI_CS_5    = 0xDFU,
                SPI_CS_6    = 0xBFU,
                SPI_CS_7    = 0x7FU
            };*/
    spiInit();// FOR INITIALIZATION of spi driver and module
    for(i=0;i<6;i++)
            {
             //0X2C IS FOR OUTPUT DATA RATE-BW_RATE REGISTER
                TX_Data_Master[i] = 0x2CFF;//1F ALSO DOES THE SAME WORK -FF OR 1F MEANS IT WORKS IN REDUCED  POWER MODEAND0F MEANS IT IS FOR NORMAL OPERATION
               spiTransmitAndReceiveData(spiREG4, &dataconfig1_t, 1, TX_Data_Master, RX_Data_Master);
               //0X2D IS FOR POWER CONTRL REGISTER
               TX_Data_Master[i] = 0x2D00;// INITIALLY WE MAKE IT AS ZERO
               spiTransmitAndReceiveData(spiREG4, &dataconfig1_t, 1, TX_Data_Master, RX_Data_Master);
               //0X31 IS FOR THE DATA FORMAT REGISTER
               TX_Data_Master[i] = 0x3101;
               spiTransmitAndReceiveData(spiREG4, &dataconfig1_t, 1, TX_Data_Master, RX_Data_Master);
               TX_Data_Master[i] = 0x2D16;//HERE WE ARE CONFIGURING AUTOSLEEP,SLEEP AND WAKEUP
               spiTransmitAndReceiveData(spiREG4, &dataconfig1_t, 1, TX_Data_Master, RX_Data_Master);
              TX_Data_Master[i] = 0x2D08;//HERE WE ARE SELECTING THE MEASURE BIT IN THE POWER CONTROL REGISTER
              spiTransmitAndReceiveData(spiREG4, &dataconfig1_t, 1, TX_Data_Master, RX_Data_Master);
            }
    for(j=0;j<3200;j++)
    //while(1)
    {
    TX_Data_Master[0] = 0xF200;
    spiTransmitAndReceiveData(spiREG4, &dataconfig1_t, 1, TX_Data_Master, RX_Data_Master);
    X0=RX_Data_Master[0];
    TX_Data_Master[0] = 0xF300;
    spiTransmitAndReceiveData(spiREG4, &dataconfig1_t, 1, TX_Data_Master, RX_Data_Master);
     X1=RX_Data_Master[0];
    TX_Data_Master[0] = 0xF400;
    spiTransmitAndReceiveData(spiREG4, &dataconfig1_t, 1, TX_Data_Master, RX_Data_Master);
    Y0=RX_Data_Master[0];
    TX_Data_Master[0] = 0xF500;
    spiTransmitAndReceiveData(spiREG4, &dataconfig1_t, 1, TX_Data_Master, RX_Data_Master);
    Y1=RX_Data_Master[0];
    TX_Data_Master[0] = 0xF600;
    spiTransmitAndReceiveData(spiREG4, &dataconfig1_t, 1, TX_Data_Master, RX_Data_Master);
     Z0=RX_Data_Master[0];
    TX_Data_Master[0] = 0xF700;
    spiTransmitAndReceiveData(spiREG4, &dataconfig1_t, 1, TX_Data_Master, RX_Data_Master);
    Z1=RX_Data_Master[0];
    x= (X1<<8) | X0;
    y= (Y1<<8) | Y0;
    z= (Z1<<8) | Z0;
    Xg=x*0.0078;
    Yg=y*0.0078;
    Zg=z*0.0078;
       Z[j]=Zg;
       X[j]=Xg;
       Y[j]=Yg;
  printf ("%1.2f %1.2f %1.2f \n",Zg,Yg,Xg);
            }
/* USER CODE END */
    return 0;
}
/* USER CODE BEGIN (4) */
/* USER CODE END */

 The HALCoGen settings for the SPI4 driver is as follows:

  • Hello,

    Do you follow the SPI communication requirements from ADXL345 datasheet? What happens if you decrease the SPI clock? What is the return value if your read the register 0 (DEVID)?

    "The maximum SPI clock speed is 5 MHz with 100 pF maximum loading, and the timing scheme follows clock polarity (CPOL) = 1 and clock phase (CPHA) = 1"

  • Hello,

    Do you follow the SPI communication requirements from ADXL345 datasheet? What happens if you decrease the SPI clock? What is the return value if your read the register 0 (DEVID)?

    "The maximum SPI clock speed is 5 MHz with 100 pF maximum loading, and the timing scheme follows clock polarity (CPOL) = 1 and clock phase (CPHA) = 1"

    Hello Wang,

    Yes, I have followed the SPI Communication requirements according to the datasheet.

    I'm able to read the DEVICE ID = E5 only when I set the clock polarity (CPOL) = 1 and clock phase (CPHA) = 0 and not when clock polarity (CPOL) = 1 and clock phase (CPHA) = 1.

    I am able to get the results even in the decreased SPI clock frequencies.

    And when I acquire the data from the accelerometer ADXL345 placing in the positive Z axis, it is picking negative values along with positive values. I have added the snapshot below. 

    Let me know if I am going wrong anywhere. The code used is put up in the first query.

     Thanks and Regards

    Rahul Achar

  • Hello Rahul,

    ADXL345 is to measure both dynamic acceleration resulting from motion or shock and static acceleration, such as gravity. Placing  ADXL345 in the positive Z axis doesn't mean that the acceleration is positive.

  • QJ Wang said:

    Hello Rahul,

    ADXL345 is to measure both dynamic acceleration resulting from motion or shock and static acceleration, such as gravity. Placing  ADXL345 in the positive Z axis doesn't mean that the acceleration is positive.

    Hello Wang, 

    The result which I have posted previously is the static acceleration obtained when ADXL345 was place on a table in the positive Z-axis. It wasn't subjected to any excitation or motion. 

    When I measured the static acceleration in the negative axis directions of X,Y,Z I'm getting negative values without any positive value. 

    Thanks and Regards

    Rahul Achar 

  • Hello,

    The SPI_MISO reads what the sensor outputs. The device ID is read correctly. I don't think the error (if it is a error) is caused by the SPI driver.