User avatar
Milliways
Posts: 644
Joined: Fri Apr 25, 2014 12:18 am
Location: Sydney, Australia

WiringPi I2C library to get 16bit value

Mon Jan 26, 2015 12:07 am

I have used the WiringPi I2C library to perform simple byte transfers between a Raspberry Pi and an Arduino (using function wiringPiI2CRead).

I want to get the Arduino to return 16bit word values, but am at somewhat of a loss.

Arduino has `Wire.write(data, length)` which I could use; WiringPi has `int wiringPiI2CReadReg16 (int fd, int reg)` but I am somewhat confused by the `reg`. There seems to be no definition of device registers.

I have looked at the WiringPi code and read http://www.mjmwired.net/kernel/Document ... -interface

Can anyone clarify and/or show some sample code.

User avatar
BAStumm
Posts: 134
Joined: Fri Aug 23, 2013 3:37 pm
Location: Loon Lake, WA USA
Contact: Website

Re: WiringPi I2C library to get 16bit value

Tue Jan 27, 2015 8:52 pm

I had a similar problem where I needed to read 4 bytes over i2c.

read(fd, temp, 4);

jamesm
Posts: 3
Joined: Sun Feb 22, 2015 9:38 am

Re: WiringPi I2C library to get 16bit value

Sun Feb 22, 2015 9:51 am

WiringPi has `int wiringPiI2CReadReg16 (int fd, int reg)` but I am somewhat confused by the `reg`.
reg is the data address to read from. This corresponds to the parameter after the chip-address when you call i2cget,

Code: Select all

i2cget -y 1 0x48 0x0 w
As an example, I'm trying to read temperature from the LM75 which has a device address of 0x48, register address of 0x0, so the call would be

Code: Select all

int fd = wiringPiI2CSetup (0x48);
int data = wiringPiI2CReadReg16 (fd, 0x0);

Return to “Interfacing (DSI, CSI, I2C, etc.)”