PIC32MX: I2C

From Mech
Jump to navigationJump to search

Inter-Integrated Circuit, or I2C (pronounced I squared C), is used as a serial communication between other devices including other PICs.

I2C can be used in the following cases:

  • As a slave device
  • As a master in a single master system
  • As a master in a multi-master system

I2C for PIC32 is discussed in more detail in the Microchip I2C documention.

Available Pins

There are two sets of pins for I2C. Each consists of SDAx and SCLx where x is either 1 or 2. SDA is the data pin and SCL is the clock pin.

Methodology: Single Master System

This section discusses the general approach for I2C between a single master and a slave.

To begin communication, the master first transmits a start command.

Each slave has a unique address. For the PIC32, the slaves can have either 7 bit or 10 bit addresses. For communicating with 10 bit addresses, see the Microchip I2C documention. The master transmits an 8 bit number. Bits 7-1 make up the slave address and bit 0 indicates whether or not the slave is expected to read or write. If bit 0 is a 1, the data transfer is output from the slave. If bit 0 is a 0, the data transfer is input to the slave.

When the master transmits the 8 bit number, the slaves compare their address to the sent address. If there is a match, the slave automatically generates an acknowledge that is sent to the master.

The slave then must read the address and R/W bit. This is done to clear the buffer.

The master then either waits for a response (output from slave) or sends up to 8 bits of data (input to slave). If output is from the slave, after each transmission the master sends an acknowledge. The slave continues to send data until the master does not acknowledge.

The master then sends a stop condition.

C Code

I2C Master Code

I2C Slave Code

This section details the master code for I2C between two PICs. The slave has a 7 bit address in this example.

Associated Circuitry

The SDAx and SCLx pins must be connected to Vdd (power) with a resistor typically 2.4k Ohm. They must also be connected from the master to the slave.