Reading measurement on VL6180X
STMicroelectronics support,
I am testing a VL6180X laser sensor device for use in autonomous robotics.
I currently get good distance measurements using an Arduino sketch but can't use that platform in my final product.
Instead I'm using a pic18f46k22 microcontroller but the measured value of the distance at register 0X62 always reads zero.
The device is configured exactly as shown in the Arduino_VL6180X.cpp file and I can successfully read the VL6180X slave address at register 0x212. All voltages for the sensor are 3.3.
I have used a pic and I2C with other devices and I'm getting all the acknowledgements with the VL6180X.
My code for reading the measurement is:
int readDistance(void)
{
int distance = 0;
I2C_Master_Start();
I2C_Master_Write(0x29 << 1); // TOF050C I²C address write
I2C_Master_Write(0x18); // VL6180X_REG_SYSRANGE_START
I2C_Master_Write(0x01); // start a distance measurement
__delay_us(100); // wait for measurement to complete
I2C_Master_RepeatedStart();
I2C_Master_Write(0x29 << 1); // TOF050C I²C address write
I2C_Master_Write(0x62); // register to read distance
I2C_Master_RepeatedStart();
I2C_Master_Write((0x29 << 1) | 1); // TOF050C I²C address read
distance = I2C_Read_Byte(1); // Read the measured distance
I2C_Master_Stop();
return (distance);
} // end unsigned int readDistance(void)I must be missing something.
Jerry Donaldson
Edited to apply source code formatting - please see How to insert source code for future reference.
