from pyb import I2C i2c = I2C(1) # create on bus 1 i2c = I2C(1, I2C.MASTER) # create and init as a master i2c.init(I2C.MASTER, baudrate=20000) # init as a master i2c.init(I2C.SLAVE, addr=0x42) # init as a slave with given address i2c.deinit() # turn off the peripheral i2c.init(I2C.MASTER) i2c.send('123', 0x42) # send 3 bytes to slave with address 0x42 i2c.send(b'456', addr=0x42) # keyword for address i2c.is_ready(0x42) # check if slave 0x42 is ready i2c.scan() # scan for slaves on the bus, returning # a list of valid addresses i2c.mem_read(3, 0x42, 2) # read 3 bytes from memory of slave 0x42, # starting at address 2 in the slave i2c.mem_write('abc', 0x42, 2, timeout=1000) # write 'abc' (3 bytes) to memory of slave 0x42 # 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.SLAVEaddr,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总线上设备。