cancel
Showing results for 
Search instead for 
Did you mean: 

VL6180X set_register and get_register functions for PIC18F

bmete21
Associate II

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 >> 😎 & 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 >> 😎 & 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.

10 REPLIES 10
Andrew Neil
Evangelist III

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 

 

bmete21
Associate II

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

 

bmete21
Associate II

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 >> 😎 & 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.


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

John E KVAM
ST Employee

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


If this or any post solves your issue, please mark them as 'Accept as Solution' It really helps. And if you notice anything wrong do not hesitate to 'Report Inappropriate Content'. Someone will review it.

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. 

 

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

Can't you get proper screenshots via that USB socket:

AndrewNeil_0-1714491765974.png

That would give far better results that photos of the screen - with all that glare, etc ...

 

#Screenshots

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.