Skip to main content
Associate
April 30, 2024
Question

VL6180X set_register and get_register functions for PIC18F

  • April 30, 2024
  • 4 replies
  • 2760 views

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.

This topic has been closed for replies.

4 replies

Andrew Neil
Super User
April 30, 2024

Please use this button to properly post source code:

AndrewNeil_0-1714473607151.png

 

 


@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 

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
bmete21Author
Associate
April 30, 2024

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 );

 

bmete21Author
Associate
April 30, 2024

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.

Andrew Neil
Super User
April 30, 2024

@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 ...

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
bmete21Author
Associate
April 30, 2024

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. Picture 1Picture 1picture 2picture 2picture 3picture 3picture 4picture 4

This is really an interesting case for me. 

 

John E KVAM
ST Employee
April 30, 2024

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

bmete21Author
Associate
April 30, 2024

Hello John, thanks for the advice. I'll look at it.