How to communicate between Raspberry pi and PIC device (PIC16F18325) via I2C

524 Views Asked by At

I'm trying to communicate with Raspberry pi 3B+ from PIC device via I2C.
My PIC device is PIC16F18325.

First, I generated i2c library by using MCC (mplab code configuration). raspberry pi is set master, pic device is set slave.

Question
I want to pass data from slave to master, but I don't know how to use the generated library.
How should I use the i2c1_slave.h library in main.c?

My code is here.

1

There are 1 best solutions below

2
On

I2C is a bi-directional bus with at least one master and one slave. Your program on the Raspberry Pi should provide I2C master functions, and the program on the PIC slave functions. The I2C master always initiates data exchanges.

To exchange data with the slave the master sends a byte containing the slave’s address and whether it is going to send more bytes or wait to receive. When the slave receives that byte it should respond accordingly by sending or receiving.

Only when the master (RPi) puts a byte on the bus following an I2C start condition that contains the slave’s address, will an interrupt be triggered on the slave (PIC).

The interrupt service routine must process the received byte and prepare for the next. The read/write bit determines the response and next action, along with housekeeping to prepare for the next interrupt. The exact steps performed by the slave must match what the master expects and vice-versa.