什么是I2C?

什么是I2C?A question I asked myself a couple months ago that I am glad to answer now! I2C (also called IIC and typically pronounced I-squared-C) stands for Inter-IC or Inter-Integrated-Circuit, was developed by Philips Semiconductor in 1982, and is one form of electronics protocol: how electronic devices communicate data with each other. I will discuss the basics to understanding I2C protocol, then show an example of debugging I2C with theAnalog Discovery 2以及逻辑分析仪和协议分析仪WaveForms

Get stoked. Photo from这里

I2C的工作方式使其成为电子协议的非常流行的选择。与其他协议相比,对I2C的最直接好处是,多个主机和多个从设备都可以在同一总线上与警告中的同一总线传达数据,而主设备无法直接与彼此进行通信,并且在与从属设备进行通信时必须“转弯”。UART仅限于两个设备之间的通信,而SPI可以具有多个从设备,但以更多的从选择线为代价,并且仍然仅限于单个主设备。I2C通常是2线系统:SDA(串行数据线)和SCK(串行时钟,有时以SCL的形式显示),尽管也可能存在地面和电源线。

尽管这很酷,但在这一点上,您可能会想知道..多个主人和多个从设备如何通过共享的2线总线发送数据?

I2C数据传输一般示例。照片来自这里

The ICs used in I2C operate with open-drain (FET) or open-collector (BJT) output stages. What that means is that the output of the IC is connected internally to the gate/base of a transistor, the source/emitter is connected internally to ground, and the drain/collector is externally connected to a pull-up resistor (“open” to connection outside of the IC). Since the pull-up resistor does not need to be connected to the IC power supply (which would be 5V or lower), the output can be connected to devices operating at voltages beyond the limitations of the chip. More than one of these chip outputs can be connected to the same wire while sharing the same pull-up resistor since only one output will be active at any particular time.

A general schematic to illustrate an open collector circuit. Photo from这里

输出晶体管充当芯片的开关:当将IC输出应用于晶体管的门/底座并超过阈值电压时,晶体管打开并将排水/收集器驱动到近0V。当不将IC的输出应用于晶体管的门/底座时,晶体管是“关闭”的,并在排水/收集器的开路中导致晶体电路,该电路将其恢复到逻辑水平高。因此,当任何芯片输出变得活跃时,它会将输出驱动到逻辑级别低,并且只能使其降低。由于没有任何设备可以驱动数据线高,因此这可以防止一些不良情况:竞争驾驶信号(一个低点和高)可能会导致电源到地面的缺点,对芯片/驱动器的损坏以及过度的电力消耗。此外,这就是允许数据线在主设备和从设备之间进行双向的原因。

主人和奴隶,他们分享了这条路。照片来自这里

主设备是驱动SCK信号并启动数据传输到从设备的数据。连接到I2C总线的每个设备都是通过唯一地址解决的软件。主设备如何与从设备通信的过程具有三个主要部分:开始条件,数据传输和停止条件。回忆起SDA和SCK在逻辑级别高的闲置状态时,开始条件定义为主设备将SDA线拉到逻辑级别低时,同时将SCK保持在逻辑级别高。一个时钟周期之后,主机和从设备之间开始通信。当数据传输结束后,停止条件将由主设备启动,并定义为使SDA线拉下并释放SCK线,从而允许上拉电阻器将SCK线驱动到逻辑级别高。在此状态下一个时钟周期之后,SDA线释放,并且上拉电阻将其返回到逻辑水平高。

Below is an example schematic of an I2C system. Photo from这里

Lastly, for our introduction to I2C, the data transmission itself has a few intrinsic characteristics. Data is sent byte-wise (8 bits) with an additional Acknowledge Bit (ACK). ACK is the 9th bit in the sequence and is left low when SCK is high. This is to “acknowledge” to the transmitter that the receiver successfully read the byte of data and that another byte can be sent. If the SDA line is high on the 9th bit, then it is called a Not Acknowledge (NACK) and is basically an error message. This is the Stop Condition mentioned earlier. SDA being a single wire, the transmitter and receiver must take turns pulling it down for ACK/NACK to occur. If you wish to dive further into the nitty gritty of the different data transmission characteristics, more detailed information from the Digilent documentation resources is available这里

Below is a graphical example to distinguish between ACK and NACK. Photo from这里

Before you run off to get started on your own I2C project, I want to show you a visual example of I2C protocol in action using theAnalog Discovery 2and the Logic Analyzer (to show the individual signals) and the Protocol Analyzer (to show the overall communication) inWaveForms。我建立了发现的项目这里that uses anArduino Uno,PMOD AD2, 和PMOD CLS将一个模拟我nput to a digital display of the input value.

Below is a view from the Logic Analyzer. Right-click on the picture and open in a new tab to see a larger version. We can see the Start Condition happens right before time = 0 seconds. Then, 8 clock cycles gather 8 bits of data (as we are expecting) before the ACK/NACK 9th bit. We can also see that this particular system is gathering data in 3-byte chunks before initiating the Stop Condition. The data result is displayed as a hexadecimal value in the I2C logic line including the ACK/NACK result and Stop Condition.

以下是协议分析仪的视图。右键单击图片,然后在新标签中打开以查看较大版本。We can see the data being written to, and read from, memory, the hexadecimal result of the input analog voltage conversion (which is nearly constant in this case because I wasn’t changing the input analog voltage), the ACK/NACK bit (only NACK in this case), and the Stop Condition. This is a great way to streamline debugging without analyzing logic signals.

数据来自不同的时间点,因此不要太努力地连接两个屏幕截图。协议分析仪和逻辑分析仪是关键工具,如果您的项目似乎无法正常运行或通信,则可以帮助调试I2C协议。每个I2C系统的细节还要多于此处讨论,因此请确保您正在查看数据表。

谢谢阅读!

0

关于伊恩·埃瑟里奇

View all posts by Ian Etheridge →

2条评论“什么是I2C?”

    1. 嗨,纳兹!

      From my understanding, yes, I2C can be considered a half-duplex protocol. Half-duplex protocols are bidirectional with the channel being used interchangeably by the connected devices, which is what I2C is doing. Thanks!

发表评论

您的电子邮件地址不会被公开。Required fields are marked*