2024-04-30 03:23 AM - last edited on 2024-04-30 03:34 AM by Peter BENSCH
Hello Everyone,
I'm working on a project where I need to use the VL6180X sensor with the PIC18F. However, I could not adapt the set register and get register functions in Arduino to the PIC using CCS C. All I want from the experts here is to adapt the two functions I shared below to work in the CCS C program in accordance with PIC.
uint8_t VL6180x::VL6180x_getRegister(uint16_t registerAddr)
{
uint8_t data;
Wire.beginTransmission(_i2caddress); // Address set on class instantiation
Wire.write((registerAddr >> 8) & 0xFF); // MSB of register address
Wire.write(registerAddr & 0xFF); // LSB of register address
Wire.endTransmission(false); // Send address and register address bytes
Wire.requestFrom(_i2caddress, 1);
data = Wire.read(); // Read Data from selected register
return data;
}
void VL6180x::VL6180x_setRegister(uint16_t registerAddr, uint8_t data)
{
Wire.beginTransmission(_i2caddress); // Address set on class instantiation
Wire.write((registerAddr >> 8) & 0xFF); // MSB of register address
Wire.write(registerAddr & 0xFF); // LSB of register address
Wire.write(data); // Data/setting to be sent to device.
Wire.endTransmission(); // Send address and register address bytes
}
Thanks to everyone who supported.
2024-04-30 03:40 AM - edited 2024-04-30 03:43 AM
Please use this button to properly post source code:
@bmete21 wrote:I could not adapt the set register and get register functions in Arduino to the PIC using CCS C. .
Why not - what problem(s) did you face?
Do you know how to do I2C on the PIC?
Knowing that, you should be able to implement functions equivalent to the Arduino's beginTransmission(), write(), endTransmission, etc ...
To do that, you're more likely to find PIC experts on Microchip's forums:
https://forum.microchip.com/s/forums?&forumId=a553l000000J2pxAAC&forumName=8-Bit%20Microcontrollers
EDIT:
Some suggestions here on Arduino cores for PIC:
https://forum.arduino.cc/t/arduino-and-pic/868085/2
2024-04-30 03:47 AM
I think there was an error somewhere while copy-pasting the code.
The correct version of the code in lines 6 and 18 is:
Wire.write((registerAddr >> 8 ) & 0xFF );
2024-04-30 04:04 AM
Hello Andrew, first of all, thank you for your interest. I'm not familiar with your forum so I'm sorry if I opened a topic in the wrong format.
I know how to use I2C functions on PIC, I have used it in many projects before. But for some reason I couldn't succeed here. I also asked in the CCS C forums and I'm waiting for an answer from there, so I wanted to ask if I could get some help here, too. In fact, when I examined my data write packages with Scope, I saw that it was correct. I think the problem is in the data reading part. The sensor sends correct messages to Arduino, but when I use it with PIC, it returns me either 0x00 or 0xFF messages.
i2c_start();
i2c_write(VL6180X_ADDRESS);
i2c_write((registerAddr >> 8) & 0xFF);
i2c_write(registerAddr & 0xFF); /
i2c_stop();
i2c_start();
i2c_write(VL6180X_ADDRESS | 0x01);
data = i2c_read();
i2c_stop();
Although the function I used for PIC seems theoretically correct, I could not get a correct answer.
2024-04-30 04:24 AM - edited 2024-04-30 04:25 AM
@bmete21 wrote:The sensor sends correct messages to Arduino, but when I use it with PIC, it returns me either 0x00 or 0xFF messages.
So look carefully at the differences on the wires between the Arduino (working) case, and the PIC (non-working) case.
The sensor neither knows nor cares what microcontroller you use - all it sees is what happens on the wires.
Does your scope do I2C decode?
If not, there are very cheap USB logic analysers which can ...
2024-04-30 06:56 AM
I've no experience with your processor, so I'm no help there. But I've have had good luck searching GitHub for stuff like this. Put in your processor and VL53. All the ToF sensors have the same I2C connections so any of them will do.
There might be an example of a working project.
Worth a try anyway.
- john
2024-04-30 07:52 AM
Yes my scope has I2C decode and when I look at it, I see what problem is. There is a definetely read problem.
For example:
i2c_start();
i2c_write(VL6180_address); // 0x52
i2c_write((0x0018 >> 8 ) & 0xFF);
i2c_write(0x0018 >> & 0xFF);
i2c_write(0x01);
i2c_stop();
delay_ms(10);
i2c_start();
i2c_write(VL6180_address);
i2c_write((0x0015 >> 8 ) & 0xFF);
i2c_write(0x0015 >> & 0xFF);
i2c_write(0x07);
i2c_stop();
i2c_start();
i2c_write(VL6180_address);
i2c_write((0x0062 >> 8 ) & 0xFF);
i2c_write(0x0062 >> & 0xFF);
i2c_stop();
i2c_start();
i2c_write(VL6180_address + 1); //0x53
sensor_data = i2c_read();
i2c_stop();
delay_ms(500);
Now I will show you 4 scope photos.
If I do not use the "i2c_read()" command you see on line 24 in the code above, the messages appear as in the first 3 images. Everything seems in order.
If I use the "i2c_read()" command, things get crazy like in the 4th picture.
This is really an interesting case for me.
2024-04-30 07:54 AM
Hello John, thanks for the advice. I'll look at it.
2024-04-30 08:43 AM - edited 2024-05-02 02:23 AM
Can't you get proper screenshots via that USB socket:
That would give far better results that photos of the screen - with all that glare, etc ...
#Screenshots
2024-05-01 11:41 PM
Hello Andrew,
The oscilloscope only gives output in XML format, I could not take it as a screenshot. When I looked at the XML output I didn't understand much. I can share these outputs if you want.