树莓派PICO I2C 通信问题!求教大神!

Strathclyde_sy 15cm2022-03-021452 次点击
必须向传感器的寄存器发送2给字节数据,以下代码没用。求教大神如何使用I2C一次性发送2字节数据?
万分感谢!
```
from machine import Pin, I2C
import time, ustruct, binascii
from time import sleep

i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq = 400000) #Define the I2C pin and frequency

# Print out any addresses found
devices = i2c.scan()
if devices:
for d in devices:
print(hex(d))

# Attitude sensor address
as_addr = 0x50

# Attitude sensor Regaddr
as_reg_addr = 0x1a # Set I2C address
as_reg_save = 0x00 # Save configuration
as_reg_unlock = 0x69 # Unlock the module


# Main
# Send 0x88 and 0xb5 to unlock the module
data2 = b'\x88\xB5'
i2c.writeto_mem(as_addr, as_reg_unlock, data2)
sleep(0.5)

# Send 0x10 and 0x00 to change the address of the module to 0x10
data3 = b'\x10\x00'
i2c.writeto_mem(as_addr, as_reg_addr, data3)
sleep(0.5)

#Send 0x00 and 0x00 to save the config
data4 = b'\x00\x00'
i2c.writeto_mem(as_addr, as_reg_save, data4)
sleep(0.5)
```
收藏 ♥ 感谢
Spoony 小组长 2022-03-03 
分两次发,每次发一个字节
接收端也是一个字节收完再收一个字节
Strathclyde_sy 15cm 2022-03-03 
好像不行,要求两个字节一起发过去。分开发, 执行两次,相当于两次开始和结束信号。
Mich_Chu 25cm 2022-03-25  ♥ 1
我是用i2c.writeto(as_addr, data)

data = bytearray(3)
data[0] = as_reg_save
data[1] = 0x00
data[2] = 0x00
i2c.writeto(as_addr, data)

登录注册 后可回复。




› 相关内容关注微信公众号