三木社区

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 237|回复: 0
打印 上一主题 下一主题

【MicroPython】I2C的用法

[复制链接]

1562

主题

1564

帖子

4904

积分

博士

Rank: 8Rank: 8

积分
4904
跳转到指定楼层
楼主
发表于 2017-9-6 12:52:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. from pyb import I2C

  2. i2c = I2C(1)                         # create on bus 1
  3. i2c = I2C(1, I2C.MASTER)             # create and init as a master
  4. i2c.init(I2C.MASTER, baudrate=20000) # init as a master
  5. i2c.init(I2C.SLAVE, addr=0x42)       # init as a slave with given address
  6. i2c.deinit()                         # turn off the peripheral

  7. i2c.init(I2C.MASTER)
  8. i2c.send('123', 0x42)        # send 3 bytes to slave with address 0x42
  9. i2c.send(b'456', addr=0x42)  # keyword for address

  10. i2c.is_ready(0x42)           # check if slave 0x42 is ready
  11. i2c.scan()                   # scan for slaves on the bus, returning
  12.                              #   a list of valid addresses
  13. i2c.mem_read(3, 0x42, 2)     # read 3 bytes from memory of slave 0x42,
  14.                              #   starting at address 2 in the slave
  15. i2c.mem_write('abc', 0x42, 2, timeout=1000) # write 'abc' (3 bytes) to memory of slave 0x42
  16.                                             # starting at address 2 in the slave, timeout after 1 second
复制代码
I2C的用法:
  • class pyb.I2C(bus, ...)
    bus,I2C总线的序号
  • i2c.deinit(),解除I2C定义
  • i2c.init(mode, *, addr=0x12, baudrate=400000, gencall=False),初始化
    • mode,只能是 I2C.MASTER 或 I2C.SLAVE
    • addr,7位I2C地址
    • baudrate,时钟频率
    • gencall,通用调用模式
  • i2c.is_ready(addr),检测I2C设备是否响应,只对主模式有效
  • i2c.mem_read(data, addr, memaddr, *, timeout=5000, addr_size=8),读取数据
    • data,整数或者缓存
    • addr,设备地址
    • memaddr,内存地址
    • timeout,读取等待超时时间
    • addr_size,memaddr的大小。8位或16位
  • i2c.mem_write(data, addr, memaddr, *, timeout=5000, addr_size=8),写入数据,参数含义同上
  • i2c.recv(recv, addr=0x00, *, timeout=5000),从总线读取数据
    • recv,需要读取数据数量,或者缓冲区
    • addr,I2C地址
    • timeout,超时时间
  • i2c.send(send, addr=0x00, *, timeout=5000)
    • send,整数或者缓冲区
    • addr,I2C地址
    • timeout,超时时间
  • i2c.scan(),搜索I2C总线上设备。

回复

使用道具 举报

Archiver|手机版|小黑屋|三木电子社区 ( 辽ICP备11000133号-4 )

辽公网安备 21021702000620号

GMT+8, 2024-4-20 01:39 , Processed in 0.027477 second(s), 22 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表