2 Replies Latest reply on Mar 7, 2019 2:04 PM by keyoc_3667136

    Need help implementing I2C functionality for Adafruit VL6180X IR sensor in PSoC 5LP platform from Arduino.

    keyoc_3667136

      I am an undergrad student and am new to embedded programming. I am very lost on how to use this IR distance sensor and output the data properly. On the Adafruit website (https://www.adafruit.com/product/3316) they provide a sample Arduino code that I got to work and output to a display to make sure the sensors are working properly. 

       

      I really want to learn how to code any I2C sensor so any helpful links to tutorials or any feedback would be greatly appreciated!!

       

      Datasheet for VL6180X (https://cdn-learn.adafruit.com/assets/assets/000/037/608/original/VL6180X_datasheet.pdf )

       

      This is sample code from a DS3231 RTC sensor I have but I have no clue how and why it was programmed in this manner to initialize the read and write functions.

      Here is the datasheet for this sensor (https://datasheets.maximintegrated.com/en/ds/DS3231.pdf )

      bool I2C_ReadByte(uint8_t slaveAddr, uint8_t registerNum, uint8_t* retVal)

      {

      bool status;

       

      status = I2C_ReadBytes(slaveAddr, registerNum, 1, retVal);

      return status;

      }

      //-------------------------------------------------------------------------------

      bool I2C_ReadBytes(uint8_t slaveAddr, uint8_t registerNum, int dataLength, uint8_t* userArray)

      {

      uint8_t status;

      bool noError = false;

      int i;

       

      status = I2C_MasterSendStart (slaveAddr, I2C_WRITE_XFER_MODE);

      if (status == I2C_MSTR_NO_ERROR) {   // Check if transfer completed w/o errors

      // Send registerNum

      status = I2C_MasterWriteByte(registerNum);

      if (status != I2C_MSTR_NO_ERROR)

      goto FINISHED;

      status = I2C_MasterSendRestart(slaveAddr, I2C_READ_XFER_MODE);

      if (status != I2C_MSTR_NO_ERROR)

      goto FINISHED;

      for(i = 0; i < dataLength; ++i){

      if (i != dataLength -1)

      *userArray = I2C_MasterReadByte(I2C_ACK_DATA);

      else

      *userArray = I2C_MasterReadByte(I2C_NAK_DATA);

      userArray++;

      }

      noError = true;

      }

      FINISHED:

      I2C_MasterSendStop(); // Send Stop

      return noError;

      }

      //-------------------------------------------------------------------------------

      bool I2C_WriteByte(uint8_t slaveAddr, uint8_t registerNum, uint8_t userArray)

      {

      uint8_t status;

      status = I2C_WriteBytes(slaveAddr, registerNum, 1, &userArray);

      return status;

      }

      //-------------------------------------------------------------------------------

      bool I2C_WriteBytes(uint8_t slaveAddr, uint8_t registerNum, int dataLength, uint8_t* userArray)

      {

      uint8_t status;

      bool noError = false;

      int i;

       

      status = I2C_MasterSendStart(slaveAddr, I2C_WRITE_XFER_MODE);

      if (status == I2C_MSTR_NO_ERROR) {   // Check if transfer completed w/o errors

      // Send registerNum

      status = I2C_MasterWriteByte(registerNum);

      if (status != I2C_MSTR_NO_ERROR)

      goto FINISHED;

      for(i = 0; i < dataLength;++i){

      status = I2C_MasterWriteByte(*userArray);

      if (status != I2C_MSTR_NO_ERROR)

      goto FINISHED;

      userArray++;

      }

      noError = true;

      }

      FINISHED:

      I2C_MasterSendStop(); // Send Stop

      return noError;

       

      }