In the以前的教程,我们讨论了I2C协议的基础知识。在大多数嵌入式设备中,要么UART否则I2C用于控制台消息。在本教程中,我们将使用I2C协议讨论Raspberry Pi中的串行通信。万博app怎么更新
I2C在覆盆子万博app怎么更新Pi中
对于I2C协议上的串行通信,Raspberry Pi的Broadcom处理器具有Broadcom Serial Controller(BSC)。万博app怎么更新该标准模式主BSC控制器是NXP半导体的I2C符合I2C,并支持400 kbps的数据传输速率。BSC控制器支持7位和10位寻址。该I2C接口可在PINGPIO2(板引脚3号)和GPIO3(板引脚第5号)上访问。GPIO2是串行数据(SDA)线,GPIO3是I2C1的串行时钟(SCL)线。这些I2C引脚通过1.8 KOHMS电阻在内部拉出3.3V。这就是为什么这些销不能用于不需要上拉的通用I/O的原因。
There is one more I2C peripheral BSC2 in Raspberry Pi identified as I2C0. The BSC2 master is dedicated to the HDMI interface and cannot be accessed by users. This I2C interface is present at board pins 27 (ID_SD) and 28 (ID_SC). I2C0 remains reserved for reading EEPROM of Raspberry Pi’s add-on boards called Hardware on The Top (HAT) boards. I2C0 can only talk to HAT EEPROM at address 0x50 during boot time.
仅当摄像机界面和HDMI端口都未使用时,才可以访问I2C0。要使用I2C0,请将以下行添加到boot/config.txt。
dtparam=i2c_vc=on
I2C0引脚没有内部拉动,因此,如果通过修改Raspberry Pi配置文件使用它们,则必须在SDA和SCL线上使用外部上拉(至3.3V)。万博app怎么更新在使用I2C0时,引脚避免使用帽子EEPROM地址。
默认情况下,即使在Raspberry Pi上也禁用I2C1万博app怎么更新。需要从Raspberry Pi配置启用它。万博app怎么更新Rasp万博app怎么更新berry Pi的BSC控制器支持多主管Multi-Slave I2C。因此,I2C1足以连接几个I2C奴隶(最多112个从奴隶)和任何数量的主设备。
从Raspberry Pi Gui启用万博app怎么更新I2C1
On Raspbian, navigate to Pi Start Menu -> Preferences -> Raspberry Pi Configuration.
In the pop-up window, click on the ‘Interfaces’ tab and select the ‘Enable’ radio button for I2C. You can also enable or disable other interfaces as required.
To take changes effect, restart Raspberry Pi. After rebooting, GPIO3 and GPIO5 can be used to connect Raspberry Pi as I2C master with an I2C bus or to any I2C slave.
Enabling I2C1 from Terminal
还可以通过端子(Raspberry pi万博app怎么更新上的Bash Shell)启用I2C对Raspberry Pi的臂芯和Linux内核的支持。打开终端并运行以下命令:
sudo raspi-config
在Raspbe万博app怎么更新rry Pi软件配置工具中,导航到“接口选项”。
In older Raspberry Pi models, navigate to ‘Advanced Options’ and then ‘I2C’.
在弹出窗口中,启用ARM I2C接口,然后选择“是”加载I2C内核模块。
现在,通过输入以下命令万博app怎么更新来重新启动Raspberry Pi:
Sudo重新启动
重新启动后,GPIO3和GPIO5可用于将Raspberry Pi与I2C总线或I2C总线或任何I2C奴隶连接起万博app怎么更新来。
测试I2C端口
启用I2C用户端口并重新启动Raspberry Pi后,我们可以测试该端口是否可作为L万博app怎么更新inux设备可用。在终端窗口中,运行以下命令:
ls /dev /
要么
ls /dev /*i2c*
I2C1必须显示为Linux设备之一,如下图所示。
Note that in the older versions of Raspberry Pi, the I2C user port is identified as I2C0 instead of I2C1. In all 256M Raspberry Pi models, the I2C user port is 0, and in rest, all it is 1.
在Raspberry Pi上扫描I2C奴隶万博app怎么更新
启用I2C用户端口后,可以使用I2C-Tools检测到连接的I2C从属。首先,通过在Raspberry Pi终端中运行以下命令来安装I2C-Tools:万博app怎么更新
sudo apt-get安装-y i2c-tools
现在运行以下命令以扫描连接的I2C奴隶:
sudo i2cdetect -y 1
正如已经提到的那样,在Raspberry Pi的较旧版本中,I2C用户端口为0,在较旧版本中,万博app怎么更新将端口号更改为0,如下所示:
sudo i2cdetect -y 0
I2C-Detect是一种扫描I2C用户端口并返回连接的从设备的I2C地址的工具。该工具返回连接的I2C从设备的地址表,如下图所示:
使用SMBUS库访问I2C设备
在Ra万博app怎么更新spberry Pi上,可以使用SMBUS库中的Python脚本访问I2C总线。SMBUS是I2C接口的子集。用于SMBU的Python库可用于与基于I2C的设备进行通信。可以通过运行以下命令在Raspberry Pi上安装SMBUS库:万博app怎么更新
sudo apt-get安装python-smbus
在Python脚本中,可以使用以下语句导入SMBUS库:
导入Smbus
导入SMBUS库后,必须使用SMBUS()方法创建SMBUS类的对象。SMBUS()方法将I2C端口号作为参数,必须在分配语句中使用以创建SMBUS对象。它具有以下语法:
The following is a valid example of creating an SMBus object:
i2c-bus = smbus.smbus(1)
Note that in older Raspberry Pi versions, I2C user port is 0, and in all Raspberry Pi versions above 256M RPi versions, it is 1. To use the latest SMBus2 library, it can be installed using pip by running the following command:
PIP安装SMBUS2
In a Python script, the SMBus2 library can be imported using the following statement:
from smbus2 import SMBus, i2c_msg
可以使用smbus2.smbus()方法创建SMBUS类的对象:如下:
i2c-bus = smbus2.smbus(1)
The smBus2 library has two classes – SMBus and i2c_msg. The SMBus class supports the following methods:
smbus.smbus()/smbus2.smbus()- 在Python脚本中创建一个Smbus对象。
开放(公共汽车)– To open a given i2c bus.
关()- 关闭I2C连接。
来自I2C从属的序列数据可以在字节,单词或字节块中读取。在某些I2C从设备中,主需要从特定寄存器访问串行数据。以下方法在SMBUS2库中可用,用于从从设备读取串行I2C数据:
read_byte(i2c_addr,force = none)– To read a single byte from a device.
read_byte_data(i2c_addr,register,force=None)– To read a single byte from a designated register.
READ_BLOCK_DATA(I2C_ADDR,注册,force = none)- 从给定的寄存器中读取最多32字的块。
READ_I2C_BLOCK_DATA(I2C_ADDR,寄存器,长度,force = none)- 从给定寄存器读取一块字节数据。
read_word_data(i2c_addr,注册,force = none)- 从给定寄存器读取单个单词(2个字节)。
同样,可以将数据写入字节,单词或字节块中的I2C奴隶。在某些I2C从设备中,必须将数据写入特定寄存器。以下方法在SMBUS2库中可用,用于从从设备编写串行I2C数据:
write_byte(i2c_addr,value,force = none)- 将单个字节写入设备。
write_byte_data(i2c_addr,注册,值,武力=无)- 将字节写入给定寄存器。
write_block_data(i2c_addr,register,data,force=None)- 将一块字节数据写入给定寄存器。
write_i2c_block_data(i2c_addr,注册,数据,force = none)- 将一块字节数据写入给定寄存器。
write_word_data(i2c_addr,寄存器,值,force = none)- 将字节写入给定寄存器。
write_quick(i2c_addr,force = none)- 执行快速交易。扔ioError,如果不成功。
以下方法可用于管理SMBUS流程并结合I2C总线读/写操作:
process_call(i2c_addr,register,value,force=None)- 执行SMBUS流程调用,发送16位值并接收16位响应
block_process_call(i2c_addr,注册,数据,武力=无)– To send a variable-size data block and receiving another variable-size response.
i2c_rdwr(*i2c_msgs)- 将一系列I2C读写操作组合在单个交易中。
In the next tutorial, we will discuss interfacing the ADXL345 accelerometer sensor with Raspberry Pi via I2C port.
提交以下:精选贡献,,,,2ManBetX登陆
与本文有关的问题?
询问并讨论edaboard.comand电气技术在线.com论坛。
Tell Us What You Think!!
你一定是登录to post a comment.