1 Peripheral Overview
The I2C bus is a multi-host serial data communication bus. Microcontrollers communicate in a host/client environment where the host devices initiate the communication and the devices are selected through addressing.
- Host Transmit mode – host is transmitting data to a client
- Host Receive mode – host is receiving data from a client
- Client Transmit mode – client is transmitting data to a host
- Client Receive mode – client is receiving data from a host
To begin communication, the host device sends out a Start bit followed by the address byte of the client it intends to communicate with. This is followed by a bit which determines if the host intends to write to or read from the client.
If there is a client on the bus with the indicated address, it will respond with an Acknowledge bit. After the host receives the Acknowledge bit, it can continue in either Writing or Reading mode.
- If the host intends to write to the client, then it will send a byte and wait for an Acknowledge bit for each sent byte.
- If the host intends to read from the client, then it will receive a byte and respond with an Acknowledge bit for each received byte.
I2C protocol:
- The Start bit is indicated by a high-to-low transition of the SDA line while the SCL line is held high.
- The Acknowledge bit is an active-low signal, which holds the SDA line low to indicate to the transmitter that the client device has received the transmitted data and is ready to receive more.
- A transition of a data bit is always performed while the SCL line is held low. Transitions that occur while the SCL line is held high are used to indicate Start and Stop bits.
The MSSP registers used to configure the device in I2C Host mode:
- MSSP Control register 1 (SSPxCON1) used to enable the MSSP peripheral and set the device in I2C Host mode.
- MSSP Control register 2 (SSPxCON2) used to send the Start and Stop conditions, set the Receive mode and handle the Acknowledge bits.
- MSSP Data Buffer (SSPxBUF) register used to send the bytes to and receive the bytes from the client.
- In addition, this is the address the I2C client responds to.
I2C Clock = F_OSC / (4 * (SSP1ADD + 1)