Skip to main content
Visitor II
May 22, 2019
Question

LIS2DH12TR accelerometer sensor

  • May 22, 2019
  • 5 replies
  • 1471 views

Hi ,

I'm trying to read the accelerometer data from the LIS2DH12TR which is connected to CC1310 TI's Controller.

In my design I'm using I2C hence i have connected the CS pin to Vdd(3.3V) and SA0 pin to VDD. As per the Data sheet with my case the address will be 0x19(SA0 is connected to Vdd).

I'm able to initialize the I2C and while reading the Data it always prints all values to zero except the Out_Z(H & L) and Out_H and Out_L are 255(Out_Z= accelerometer values in the z direction).

What could be the reason for this?

I assume it's factory calibrated.

Here's the attached code with CC1310(CCS):

 txBuffer[0] = 0x0C;

  i2cTransaction.slaveAddress = 0x19;

  i2cTransaction.writeBuf = txBuffer;

  i2cTransaction.writeCount =1;

  i2cTransaction.readBuf = rxBuffer;

  i2cTransaction.readCount = 2;

  status = I2C_transfer(i2c, &i2cTransaction);

  while (status == false) {

    // Unsuccessful I2C transfer

    Display_printf(display, 0, 0, "Unsucessful I2C Read");

    sleep(2);

  }

  txBuffer[0] = 0x28;

  i2cTransaction.slaveAddress = 0x19;

  i2cTransaction.writeBuf = txBuffer;

  i2cTransaction.writeCount =1;

  i2cTransaction.readBuf = rxBuffer;

  i2cTransaction.readCount = 6;

  status = I2C_transfer(i2c, &i2cTransaction);

  while (status == false) {

    // Unsuccessful I2C transfer

    Display_printf(display, 0, 0, "Unsucessful I2C Read");

    sleep(2);

  }

  /* Take 20 samples and print them out onto the console */

  for (sample = 0; sample < 1231150; sample++) {

    if (I2C_transfer(i2c, &i2cTransaction)) {

      /* Extract degrees C from the received data; see TMP116 datasheet */

      //temperature = (rxBuffer[0] << 8) | (rxBuffer[1]);

      //temperature *= 0.0078125;

      TMP =  (rxBuffer[0] <<8) | (rxBuffer[1]);

      AcX =  (rxBuffer[1] <<8) | (rxBuffer[2]);

      AcY =  (rxBuffer[3] <<8) | (rxBuffer[4]);

      AcZ =  (rxBuffer[5] <<8) | (rxBuffer[6]);

    This topic has been closed for replies.

    5 replies

    ST Employee
    May 22, 2019

    ​Hi, to check the I2C communication did you try a single read of the WHO_AM_I (0Fh) reg? You should get 33h value. Consider that the factory calibration are stored in the reserved registers 00h-06h and could be 00h. Another point is if you are using the I2C multi-read sequence according to ds specs? Its very strange you see only one axis well (maybe only MSByte)... Better if it is possible for you to share the oscilloscope screenshots. Regards.

    LNAuthor
    Visitor II
    May 22, 2019

    Hi, Thanks for your quick reply. I tried reading the OFh Register But as you can see it's returning 51(integer).

    Since I don't have an oscilloscope, I'll check and share you the screenshots shortly. 0690X000008wFDbQAM.png

    LNAuthor
    Visitor II
    May 22, 2019

    and with this code:

     txBuffer[0]=0x0F;

      i2cTransaction.slaveAddress = 0x19;

      i2cTransaction.writeBuf = NULL;

      i2cTransaction.writeCount = 0;

      i2cTransaction.readBuf = rxBuffer;

      i2cTransaction.readCount = 2;

      status = I2C_transfer(i2c, &i2cTransaction);

      if (!status) {

        // Unsuccessful I2C transfer

      }

      txBuffer[1] = 0x0C;

      //txBuffer[1] = 0;

      i2cTransaction.slaveAddress = 0x19;

      i2cTransaction.writeBuf = txBuffer;

      i2cTransaction.writeCount =1;

      i2cTransaction.readBuf = rxBuffer;

      i2cTransaction.readCount = 2;

      status = I2C_transfer(i2c, &i2cTransaction);

      while (status == false) {

        // Unsuccessful I2C transfer

        Display_printf(display, 0, 0, "Unsucessful I2C Read");

        sleep(2);

      }

      //txBuffer[0] = 0x3B;

      txBuffer[2] = 0x28;

      i2cTransaction.slaveAddress = 0x19;

      i2cTransaction.writeBuf = txBuffer;

      i2cTransaction.writeCount =1;

      i2cTransaction.readBuf = rxBuffer;

      i2cTransaction.readCount = 6;

      status = I2C_transfer(i2c, &i2cTransaction);

      while (status == false) {

        // Unsuccessful I2C transfer

        Display_printf(display, 0, 0, "Unsucessful I2C Read");

        sleep(2);

      }

      /* Take 20 samples and print them out onto the console */

      for (sample = 0; sample < 1231150; sample++) {

        if (I2C_transfer(i2c, &i2cTransaction)) {

          /* Extract degrees C from the received data; see TMP116 datasheet */

          //temperature = (rxBuffer[0] << 8) | (rxBuffer[1]);

          //temperature *= 0.0078125;

          whoami= (rxBuffer[0]) | (rxBuffer[1]);

          TMP =  (rxBuffer[2] <<8) | (rxBuffer[3]);

          AcX =  (rxBuffer[4] <<8) | (rxBuffer[5]);

          AcY =  (rxBuffer[6] <<8) | (rxBuffer[7]);

          AcZ =  (rxBuffer[8] <<8) | (rxBuffer[9]);

          Temperature =TMP/340.00+36.53

    ST Employee
    May 27, 2019

    Did you correctly initialize the reg CTRL_REG1 (20h) in terms of ODR value? Lçet me pls know if you succeed in correctly reading the data. Regards

    LNAuthor
    Visitor II
    May 28, 2019

    Yea I have set CTRL_REG1(20h) to 57h i.e. 0101 (100Hz ODR) but still Accel x,y,z, values never changes.

    Code:

    /*

     * ======== i2ctmp116.c ========

    #include <stdint.h>

    #include <stddef.h>

    #include <unistd.h>

    /* Driver Header files */

    #include <ti/drivers/GPIO.h>

    #include <ti/drivers/I2C.h>

    #include <ti/display/Display.h>

    #include <xdc/runtime/System.h>

    #include <ti/sysbios/knl/Clock.h>

    #include <ti/sysbios/knl/Task.h>

    #include <ti/drivers/Power.h>

    #include <ti/drivers/PIN.h>

    /* Example/Board Header files */

    #include "Board.h"

    #define TASKSTACKSIZE    640

    #define LIS_2DH   0x0000

    #ifdef Board_I2C_LIS_ADDR

    #define LIS_2DH_ADDR     Board_I2C_LIS_2DH_ADDR

    #else

    #define LIS_2DH_ADDR     0x19 //0x18 AD=1

    #endif

    static Display_Handle display;

    /*

     * ======== mainThread ========

     */

    char tmp_str[7]; // temporary variable used in convert function

    char* convert_int16_to_str(int16_t i) { // converts int16 to string. Moreover, resulting strings will have the same length in the debug monitor.

     printf(tmp_str, "%6d", i);

     return tmp_str;

    }

    void myCallbackFxn(I2C_Handle handle, I2C_Transaction *msg, bool status)

    {

      if (status == false) {

        //transfer failed

      }

    }

    void *mainThread(void *arg0)

    {

      void taskFunction(); {

       uint16_t    sample;

       uint8_t    AcX,AcY, AcZ,who_am_i,control1,TMP_CFG,CTRL_REG1;

       uint8_t     TMPH,TMPL;

    //  uint16_t  xAccel,yAccel,zAccel;

       int16_t TMP;

       float Temperature;

       uint8_t     txBuffer[3];

       uint8_t     rxBuffer[9];

       //uint16_t    Ax,Ay,Az;

       bool status;

       //I2C_Handle i2cHandle = I2C_open(Board_I2C_GY521_MPU6050, &i2cParams);

       //I2C_Handle   i2c;

       I2C_Params   i2cParams;

       I2C_Params_init(&i2cParams);

       //i2cParams.transferMode = I2C_MODE_CALLBACK;

       i2cParams.transferMode = I2C_MODE_BLOCKING;

       i2cParams.transferCallbackFxn = myCallbackFxn;

       i2cParams.bitRate = I2C_100kHz;

       //I2C_Transaction i2cTransaction;

       //I2C_Handle i2c = I2C_open(Board_I2C0, &i2cParams);

       I2C_Handle i2c = I2C_open( Board_I2C_LIS, &i2cParams);

       I2C_Transaction i2cTransaction = {0};

      /* Call driver init functions */

      Display_init();

      GPIO_init();

      I2C_init();

      /* Configure the LED and if applicable, the TMP116_EN pin

      GPIO_setConfig(Board_GPIO_LED0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);

    #ifdef Board_GPIO_TMP116_EN

      GPIO_setConfig(Board_GPIO_TMP116_EN, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_HIGH);

      /* 1.5 ms reset time for the TMP116

      sleep(1);

    #endif */

      /* Open the HOST display for output */

      display = Display_open(Display_Type_UART, NULL);

      if (display == NULL) {

        while (1);

      }

      /* Turn on user LED */

      //GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);

      //Display_printf(display, 0, 0, "Starting the i2ctmp116 example\n");

      Display_printf(display, 0, 0, "Starting the LIS2DH-Accelerometer example\n");

      /* Create I2C for usage */

      I2C_Params_init(&i2cParams);

      i2cParams.bitRate = I2C_400kHz;

      //i2c = I2C_open(Board_I2C_TMP, &i2cParams);

      i2c = I2C_open(Board_I2C_LIS, &i2cParams);

      //i2c = I2C_open(CC1310_LAUNCHXL_I2C0_SDA0, &i2cParams);

      //i2c = I2C_open(CC1310_LAUNCHXL_I2C0_SCL0 , &i2cParams);

      if (i2c == NULL) {

        Display_printf(display, 0, 0, "Error Initializing I2C\n");

        while (1);

      }

      else {

        Display_printf(display, 0, 0, "I2C Initialized!\n");

      }

      /* Who_am_i */

      txBuffer[0] = 0x0F;             //Who_am_i register = 0x0Fh 8 bit

      i2cTransaction.slaveAddress = 0x19;

      i2cTransaction.writeBuf = txBuffer;

      i2cTransaction.writeCount = 1;

      i2cTransaction.readBuf = rxBuffer;

      i2cTransaction.readCount = 1;

      /* CTRL_REG1 = 20h and Value is 57h */

      txBuffer[1] = 0x20;

      txBuffer[2] = 0x57;

      i2cTransaction.slaveAddress = 0x19;

      i2cTransaction.writeBuf = txBuffer;

      i2cTransaction.writeCount = 2;

      i2cTransaction.readBuf = rxBuffer;

      i2cTransaction.readCount = 1;

      /* Accelerometer values x,y & z */

      txBuffer[4] = 0x28;

      i2cTransaction.slaveAddress = 0x19;

      i2cTransaction.writeBuf = txBuffer;

      i2cTransaction.writeCount =1;

      i2cTransaction.readBuf = rxBuffer;

      i2cTransaction.readCount = 6;

      status = I2C_transfer(i2c, &i2cTransaction);

      while (status == false) {

        // Unsuccessful I2C transfer

        Display_printf(display, 0, 0, "Unsucessful I2C Read");

        sleep(2);

      }

      /* Take 20 samples and print them out onto the console */

      for (sample = 0; sample < 1231150; sample++) {

        if (I2C_transfer(i2c, &i2cTransaction)) {

          who_am_i = rxBuffer[0];

          CTRL_REG1 = rxBuffer[1];

          AcX    =(rxBuffer[2] <<8) | (rxBuffer[3]);

          AcX    =(rxBuffer[4] <<8) | (rxBuffer[5]);

          AcX    =(rxBuffer[6] <<8) | (rxBuffer[7]);

          Display_printf(display, 0, 0,"who_am_i is: %d \n", who_am_i);

          Display_printf(display, 0, 0,"CTRL_REG1 is: %d \n", CTRL_REG1);

          Display_printf(display, 0, 0, "AcX is : %d \t rxBuffer[3] : %d \t rxBuffer[2]  : %d \n", AcX,rxBuffer[2], rxBuffer[3]);

          Display_printf(display, 0, 0, "AcY is : %d \t rxBuffer[5] : %d \t rxBuffer[4]  : %d \n", AcY,rxBuffer[4], rxBuffer[5]);

          Display_printf(display, 0, 0, "AcZ is : %d \t rxBuffer[7] : %d \t rxBuffer[6]  : %d \n", AcZ,rxBuffer[6], rxBuffer[7]);

          }

    0690X000008wVNhQAM.png