Datasheet
Chapter 8.
31
The I
2
C Library
In addition to the device routines, the library includes a complete set of low level I2C routines for devices
with seven bit I
2
C addresses. Almost all I
2
C devices may be supported with these routines.
Table 8.1. Low-level I
2
C functions
Operation Function
Initialize the I
2
C peripheral void InitI2C( void )
Start an I
2
C transaction unsigned int StartI2C( void )
Restart an I
2
C transaction unsigned int RestartI2C( void )
Terminate an I
2
C transaction unsigned int StopI2C( void )
Write a byte to an I
2
C device void WriteI2C( unsigned char )
Read a byte from an I
2
C device unsigned char getI2C( void )
Check that the I
2
C bus is idle void IdleI2C( void )
Send an acknowledgement to an I
2
C device void AckI2C( void )
Set a non-acknowledge (NAK) to an I
2
C device void NotAckI2C( void )
Test the acknowledge status of the I
2
C bus unsigned int ACKstatusI2C( void )
8.1. The I
2
C Transaction
Operations on the I
2
C bus are handled as transactions[PHI1]. The transaction must be started, then
operations may take place, and then the transaction must be stopped before another transaction may take
place.
In the case of a microcontroller communicating with a set of peripheral ICs, the microcontroller is the bus
master, and is responsible for starting and stopping transactions. The slave sends an acknowledge bit at
the end of each byte sent by the master. In the case of the dsPIC, handling the acknowledge bit is dealt
with by the hardware.
8.2. The I
2
C Control Byte
The first byte sent in any transaction is the control byte. The control byte contains the target device
address in the high seven bits. The low bit is cleared if the master intends to write data to the slave, and
set if the master expects the slave to send data. However, a set read/write bit may only be sent following
an I
2
C restart.
Thus, a function to communicate with a device will calculate the control byte by shifting the address one
bit to the left clearing the low bit:
ucControlByte = ucDeviceAddress<<1;