This article shows an implementation of I2C slave mode using PIC16F1825 written in C language and how to test or simulate the codes in PICkit Serial Analyzer I2C software (Note: Hardware part will not be shown here). There's a detailed discussion of I2C slave mode that can be found at Microchip Application Note 734. However, the sample code in this Application Note is written in assembly language.
Basically, it consist of three source files and their counterpart header file:
Main.c | main.h |
---|---|
|
|
i2c_slave.c | i2c_slave.h |
---|---|
|
|
delay.c | delay.h |
---|---|
|
Note that this is the crystal frequency, the CPU clock is divided by 4.
|
There is an attachment file available for download below that contains the source codes and MPLAB project setup of this application.
Here are the steps simulating the I2C application on PICkit Serial Analyzer software:
This test will demonstrate writing and reading functionality.
- First of all, compile the I2C application using MPLAB.
- Connect your hardware to PICkit Serial Analyzer cable. Open PICkit Serial Analyzer and click "Next" button.
- Select "I2C Master" and click "Next" button.
- Select "Yes" and click "Next" button.
- Make sure the "PICkit Serial will power my device" is checked and "5 Volt" is selected then click "Next" button.
- Click "OK" button.
- Click "View: Basic" at top left.
- Form menu bar, select "Communication" and click "Script Builder".
- We will create script for "Write" function. On "Script Builder" window under "Script Detail", select I2CM and click "Start" (this represents the start bit in I2C protocol).
- Next we'll setup the write byte. On "Script Builder" window under "Script Detail", select I2CM and click "Write Bytes".
- We will enter next the number of bytes and it is good to format it as decimal to avoid mistakes. On "Script Builder" window under "Script Detail" third row, click the "x" to switch to "d" (this means decimal and "x" means hexadecimal).
- Next enter the rest of data: slave address, mode, EEPROM address and EEPROM value on "Script Builder" window under "Script Detail" on rows available.
- Add the stop bit on available row of "Script Builder" window under "Script Detail".
- Click "Execute Script" button.
- The "Transaction" window will display the data sent to and received from the PIC16F1825 microcontroller. The text in blue font color represent the host or PC and text in red font color represent the microcontroller.
- I have already created script for the "Read" function named "ReadEEPROM". Just copy the data shown at "Script Builder" window under "Script Detail".
- After creating the "Read" function script, click "Execute Script" button.
- The "Transaction" window will display the data sent to and reply from the PIC16F1825 microcontroller.