0

VL6180X problems with PSoC 5LP
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

VL6180X problems with PSoC 5LP

by kyookyoo on Wed Mar 13, 2019 3:07 am

Here is a link to my project code
Google Drive, Datasheet


I am trying to output the range to an LCD, but it keeps displaying the registers and not the distance in mm. How does this sensor calculate the distance? Reading through the datasheet, I can't seem to find how it grabs the 8 bit data and converts it to mm. I feel like i'm missing something conceptually. Any help would be greatly appreciated!

Also this is me trying to convert the sample Arduino code given

// This is my begin function
Code: Select all | TOGGLE FULL SIZE
bool VL680X_begin(void) {
    uint8_t dataBuf[1];
    I2C_ReadByte(i2caddr,VL6180X_REG_IDENTIFICATION_MODEL_ID, dataBuf);
    // 0xB4 - 1011 0100
    if (dataBuf[0] != 0xB4)
        return false;
 
  //if (read8(VL6180X_REG_SYSTEM_FRESH_OUT_OF_RESET) == 0x01) {
    //loadSettings();
  //}
  I2C_WriteByte(i2caddr, VL6180X_REG_SYSTEM_FRESH_OUT_OF_RESET, 0x00);

  return true;
}


// This is my reading range function
Code: Select all | TOGGLE FULL SIZE
uint8_t readRange(void) {
    uint8_t dataBuf[1];
  // wait for device to be ready for range measurement | 0x04D
    I2C_ReadByte(i2caddr, VL6180X_REG_RESULT_RANGE_STATUS, dataBuf);
    while (!(dataBuf[0] = 0x01)) {
        LCD_ClearDisplay();
        LCD_Position(0,0);
        LCD_PrintString("Range ERROR");
        CyDelay(10);
    }
  // Start a range measurement | 0x018
  I2C_WriteByte(i2caddr, VL6180X_REG_SYSRANGE_START, 0x01);
   

  // Poll until bit 2 is set | 0x04F
  I2C_ReadByte(i2caddr, VL6180X_REG_RESULT_INTERRUPT_STATUS_GPIO, dataBuf);
  while (!(dataBuf[0] = 0x04)) {
        LCD_ClearDisplay();
        LCD_Position(1,0);
        LCD_PrintString("Polling ERROR");
        CyDelay(10);
    }

  // read range in mm | 0x062
   
  I2C_ReadByte(i2caddr, VL6180X_REG_RESULT_RANGE_VAL, dataBuf);
  uint8_t range = dataBuf[0];
//    LCD_ClearDisplay();
//    LCD_Position(1,0);
//    sprintf(str,"Range: %03d", range);
//    LCD_PrintString(str);
//    CyDelay(10000);

  // clear interrupt | 0x015
  I2C_WriteByte(i2caddr, VL6180X_REG_SYSTEM_INTERRUPT_CLEAR, 0x07);

  return range;
}


// This is my Range Status function
Code: Select all | TOGGLE FULL SIZE
uint8_t readRangeStatus(void) {
  uint8_t dataBuf[1];
  I2C_ReadByte(i2caddr, VL6180X_REG_RESULT_RANGE_STATUS, dataBuf);
  return (dataBuf[0] >> 4); // 0x04D
}


// Here are my I2C read/write functions for 8bit & 16bit
Code: Select all | TOGGLE FULL SIZE
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;
   
}

kyookyoo
 
Posts: 1
Joined: Wed Mar 13, 2019 2:49 am

Re: VL6180X problems with PSoC 5LP

by adafruit_support_mike on Thu Mar 14, 2019 12:05 am

VL6180X_REG_RESULT_RANGE_VAL holds the range in millimeters.

What values do you expect, and what are you getting instead?

adafruit_support_mike
 
Posts: 57493
Joined: Thu Feb 11, 2010 2:51 pm

Please be positive and constructive with your questions and comments.