The java I2C class provides the software developer an easy to use tool with lots of flexibility without having to worry about the technical handshaking details between the java program and the I2C device. The I2C class is contained in com.dalsemi.system.*.
To implement the I2C port in java a new I2Cport needs to be allocated.
I2CPort i2cport = new I2CPort();
The slave address needs to be set. This address depends on the device that is being used. Every manufacturer has different specifications that can be found in the device datasheet. The slave address is always a 7-bit-long address. In the MAX127's case the first four bits (MSB) are factory programmed to 0101. The last 3 bits (LSB) are determined by the hardware address settings and for this project are 111. That is binary 0b0101111 or hex 0x2f. Clock delay is set according to the device handshake speed.
i2cport.slaveAddress = (byte)0x2f; i2cport.setClockDelay((byte)2);
The I2C port read and write functions throw an IllegalAddressException that needs to be caught with a try catch block. After the slave address has been assigned, the i2cport can be red and written to and java will take care of the handshaking and I2C protocol. When writing to an I2C device, it will expect an 8-bit-long control byte. This control byte is also different for different manufacturers so consult the data sheets. For this project the control byte is made up as follows: The first bit (MSB) is the start bit, which is always a 1. The next three bits select the channel from which data is to be collected. Bits 3 and 2 select the voltage input range and the last two bits (LSB) select the power down method. To collect data from channel 0 with a �5V input range the control byte would be in binary 0b10000110 and in hex 0x86. The write function returns a -1 value if the device could not interpret the control byte. The parameters 'off' and 'num' are for the starting offset to send and the number of bytes to send.
i2cport.write(controlbyte,off,num)
The read function returns a -1 value if an error occurred. The parameters 'off' and 'num' are for the starting offset to read and the number of bytes to read. For the MAX127, after the control byte was written the device sends back two bytes that contain the data.
i2cport.read(FinalServlet.datab,off,num)