树莓派Pico,I2C 写入问题,求教!

使用树莓派Pico,micropython 如何通过I2C总线向设备写入2字节数据

Strathclyde_sy

喜欢

1606
浏览
0
喜欢

> 更多图片

项目状态:搁置中
开放度:公开
所属分类:电子
发布时间:2022-03-02
最近更新:2022-03-02

描述

使用树莓派Pico,micropython 如何通过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)


详细说明

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'x88xB5'
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'x10x00'
i2c.writeto_mem(as_addr, as_reg_addr, data3)
sleep(0.5)

Send 0x00 and 0x00 to save the config

data4 = b'x00x00'
i2c.writeto_mem(as_addr, as_reg_save, data4)
sleep(0.5)

链接表