public class I2C extends Object
WiringPi includes a library which can make it easier to use the Raspberry Pi’s on-board I2C interface.
Before you can use the I2C interface, you may need to use the gpio utility to load the I2C drivers into the kernel: > gpio load i2c If you need a baud rate other than the default 100Kbps, then you can supply this on the command-line: > gpio load i2c 1000 will set the baud rate to 1000Kbps – ie. 1,000,000 bps. (K here is times 1000) *
This library depends on the wiringPi native system library. (developed by Gordon Henderson @ http://wiringpi.com/)
Modifier and Type | Field and Description |
---|---|
static int |
CHANNEL_0 |
static int |
CHANNEL_1 |
Modifier and Type | Method and Description |
---|---|
static int |
wiringPiI2CRead(int fd)
wiringPiI2CRead:
|
static int |
wiringPiI2CReadReg16(int fd,
int reg)
wiringPiI2CReadReg16:
|
static int |
wiringPiI2CReadReg8(int fd,
int reg)
wiringPiI2CReadReg8:
|
static int |
wiringPiI2CSetup(int devid)
wiringPiI2CSetup:
|
static int |
wiringPiI2CWrite(int fd,
int data)
wiringPiI2CWrite:
|
static int |
wiringPiI2CWriteReg16(int fd,
int reg,
int data)
wiringPiI2CWriteReg16:
|
static int |
wiringPiI2CWriteReg8(int fd,
int reg,
int data)
wiringPiI2CWriteReg8:
|
public static int wiringPiI2CSetup(int devid)
wiringPiI2CSetup:
This initialises the I2C system with your given device identifier. The ID is the I2C number of the device and you can use the i2cdetect program to find this out. wiringPiI2CSetup() will work out which revision Raspberry Pi you have and open the appropriate device in /dev.
The return value is the standard Linux filehandle, or -1 if any error – in which case, you can consult errno as usual.
E.g. the popular MCP23017 GPIO expander is usually device Id 0×20, so this is the number you would pass into wiringPiI2CSetup().
devid
- I2C device idpublic static int wiringPiI2CRead(int fd)
wiringPiI2CRead:
Simple I2C device read. Some devices present data when you read them without having to do any register transactions.
fd
- Linux file handle obtained from call to wiringPiI2CSetuppublic static int wiringPiI2CWrite(int fd, int data)
wiringPiI2CWrite:
Simple I2C device write. Some devices accept data this way without needing to access any internal registers.
fd
- Linux file handle obtained from call to wiringPiI2CSetupdata
- data to writepublic static int wiringPiI2CWriteReg8(int fd, int reg, int data)
wiringPiI2CWriteReg8:
I2C device write. Write an 8-bit data value into the device register indicated.
fd
- Linux file handle obtained from call to wiringPiI2CSetupreg
- I2C device register addressdata
- data byte to write (8 bits/1 byte)public static int wiringPiI2CWriteReg16(int fd, int reg, int data)
wiringPiI2CWriteReg16:
I2C device write. Write a 16-bit data value into the device register indicated.
fd
- Linux file handle obtained from call to wiringPiI2CSetupreg
- I2C device register addressdata
- data bytes to write (16 bits/2 bytes)public static int wiringPiI2CReadReg8(int fd, int reg)
wiringPiI2CReadReg8:
I2C device read. Read an 8-bit (1 byte) data value from the device register indicated.
fd
- Linux file handle obtained from call to wiringPiI2CSetupreg
- I2C device register addresspublic static int wiringPiI2CReadReg16(int fd, int reg)
wiringPiI2CReadReg16:
I2C device read. Read a 16-bit (2 bytes) data value from the device register indicated.
fd
- Linux file handle obtained from call to wiringPiI2CSetupreg
- I2C device register addressCopyright © 2012–2019 Pi4J. All rights reserved.