树莓派 I2C通信,控制多个I2C从设备

1.需要安装 i2c-tools工具

在黑框输入:sudo apt-get install i2c-tools

2.后续编程使用python库

在黑框输入:sudo apt-get install python-smbus

若这边报错可试一下:sudo apt-get install python3-smbus

以上2步这边已经安装完成不在演示。

3.安装完成后可以手动查看一下树莓派挂载几个i2c

通过指令:ls /dev/*i2c* 可以看到我的树莓派挂载着i2c-1 和 i2c-20 和 i2c-21

由于我这边使用的是i2c-1 后续的指令跟这个有关系

4.查看i2c-1下挂载的多少个设备:sudo i2cdetect -y -a 1 (注意:这边的1就是前面我们查询到的i2c-1)

可以看到当前i2c总线上挂载着有地址 0x0C/0x50/0x51/0x54/0x55/0x70六个子设备

5.读取子设备的全部数据,我这边以0x50为例:sudo i2cdump -y 1 0x50

5.1读取子设备某个地址到某个地址的数据:sudo i2cdump -y -r 0x00-0x0f 1 0x50

 5.2读取子设备某个地址的数据我这边以0x70为例:sudo i2cget -y 1 0x70 0x00

6.写入子设备某个地址数据,这边在0x70子设备的0x03地址下写0x3F为例:sudo i2cset -y 1 0x70 0x03 0x3F 

写入完成

 回读一下写入的值:sudo i2cget -y 1 0x70 0x03 ,回读回来的数据跟写入的数据一致都为0x3F

7.因子设备中挂载着LT3966的子设备,地址为0x51,LT3966设备写格式有包含校验位,具体校验位计算查看手册,这边只说明i2c的使用

根据LT3966的子设备协议发送:sudo i2cset -y 1 0x51 0x50 0xa0 0xfb 可以看到下图,提示错误:Invalid mode '0xfb'!  无效模式,下方还提示了指令说明。

 

 根据提示的指令说明,我们将指令修改成:sudo i2cset -y 1 0x51 0x50 0xa0 0xfb i   

 并回读:sudo i2cget -y 1 0x51 0x50 说明写入OK

好了,接下来就是小伙伴们根据需要写入和读取数据了

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------分隔符-------------------------以下为python程序举例---------------------------

首先我们先看一下 smbus库的i2c可调用的函数

原文链接:https://blog.csdn.net/yuanzywhu/article/details/106450739

Python SMBus库函数介绍
在树莓派中,我们可用Python SMBus库函数访问串行I2C设备,导入SMBus模块及常用SMBus库函数介绍如下。
(1) Import SMBus
●To access I2C bus on Raspberry Pi using SMBus Python module, import SMBus module as follows.
import smbus
●Create object of SMBus class to access I2C based Python function.
= smbus.SMBus(I2C port no.)
I2C port no : I2C port no. i.e. 0 or 1
●Example - Bus = smbus.SMBus(1)
Now, we can access SMBus class with Bus object.
(2) Bus.write_byte_data(Device Address, Register Address, Value)
●This function is used to write data to the required register.
Device Address : 7-bit or 10-bit device address
Register Address : Register address to which we need to write
Value : pass value which needs to write into the register
●Example - Bus.write_byte_data(0x68, 0x01, 0x07)
(3) Bus.write_i2c_block_data(Device Address, Register Address, [value1, value2,….])
●This function is used to write a block of 32 bytes.
Device Address : 7-bit or 10-bit device address
Register Address : Register address to which we need to write data
Value1 Value2…. : write a block of bytes to the required address
●Example - Bus.write_i2c_block_data(0x68, 0x00, [0, 1, 2, 3, 4, 5]) # write 6 bytes of data from 0 address.
(4) Bus.read_byte_data(Device Address, Register Address)
●This function is used to read data byte from required register.
Device Address : 7-bit or 10-bit device address
Register Address : Register address from which we need to read data
●Example - Bus.read_byte_data (0x68, 0x01)
(5) Bus.read_i2c_block_data(Device Address, Register Address, block of bytes)
●This function is used to read a block of 32 bytes.
Device Address – 7-bit or 10-bit device address
Register Address – Register address from which we need to read data
Block of Bytes – read no of bytes from the required address
●Example - Bus.read_i2c_block_data(0x68, 0x00, 8) # return value is a list of 6 bytes
———————————————————————————————————————————
根据上方给的函数我们开始编写脚本

1.需要先调用我们的函数库:import smbus

import smbus


bus = smbus.SMBus(1) ###这边的1是前面我们查询的i2c-1
bus.write_byte_data(0x70,0x03,0x3F)
ReadData = bus.read_byte_data(0x70,0x03)
print(ReadData)

bus.write_i2c_block_data(0x51,0x50,[0xA0,0xFB])
Volt = bus.read_byte_data(0x51,0x51)
print(Volt)

ReadData=[] #创建一个list
   for i in range(0,255):
      ReadData.append(chr(bus.read_byte_data(addr,i)))#将读取到的数据转换为字符格式传入到ReadData的list

   print("".join(ReadData))#将list组合成字符串并打印

 附件:附在自己写的通过UDP方式写入和读取I2C

 

  • 2
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值