简体   繁体   English

如何通过 I2C 在 Raspberry pi 和 PIC 设备(PIC16F18325)之间进行通信

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

I'm trying to communicate with Raspberry pi 3B+ from PIC device via I2C.我正在尝试通过 I2C 从 PIC 设备与 Raspberry pi 3B+ 通信。
My PIC device is PIC16F18325 .我的 PIC 设备是PIC16F18325

First, I generated i2c library by using MCC (mplab code configuration).首先,我使用MCC(mplab代码配置)生成了i2c库。 raspberry pi is set master, pic device is set slave.树莓派设为主设备,PIC 设备设为从设备。

Question
I want to pass data from slave to master, but I don't know how to use the generated library.我想将数据从slave传递给master,但我不知道如何使用生成的库。
How should I use the i2c1_slave.h library in main.c?我应该如何在 main.c 中使用 i2c1_slave.h 库?

My code is here.我的代码在这里。

I2C is a bi-directional bus with at least one master and one slave. I2C 是一种双向总线,至少有一个主机和一个从机。 Your program on the Raspberry Pi should provide I2C master functions, and the program on the PIC slave functions.你在树莓派上的程序应该提供 I2C 主功能,而 PIC 从功能上的程序。 The I2C master always initiates data exchanges. I2C 主设备始终启动数据交换。

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).只有当主机 (RPi) 在包含从机地址的 I2C 启动条件之后将一个字节放在总线上时,才会在从机 (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.从站执行的确切步骤必须与主站期望的一致,反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM