I2C PIC Interfacing Tutorial

Program Download:

Main C File

The Software
           There are two main portions of code that we are concerned with:

In the examples below, I will only show tiny snippets of the I2C communication portion of the code. The formula shown for calculating the SSPADD value is very important as I couldn't find it anywhere in microchip's documentation and it's 100% necessary for getting these embedded functions to work properly.

I2C Setup Code

------------« Begin Code »------------
//Start I2C Module Setup
SSPADD = 0x63; //100kHz Baud clock(0x63) @40MHz
------------« End Code »------------

           100 KHz is the bitrate that we'll operate at for this design, so we have to use the formula seen above to find out what SSPADD value is needed to go along with the OpenI2C function. Master tells the PIC that it will be the Master for the I2C communication and Slew_Off says it will be a slower communication rate, namely 100 KHz.

I2C Read And Write

------------« Begin Code »------------
//Tell I2C Memory Device Address And Data
//Send Memory Device Data Address
i2c_data = EERandomRead(0xA0,i2c_address);
------------« End Code »------------

           These two functions do the 'heavy lifting' to write and read from the I2C EEPROM device. They offer us debug capability as well incase there is an addressing or bus error by giving specific return values when things go wrong.