树莓派Pico I2C问题求教!

树莓派 PICO 和 RP2040 相关应用讨论
回复
Strathclyde_yang
帖子: 1
注册时间: 2022年 3月 2日 19:50

树莓派Pico I2C问题求教!

#1

帖子 Strathclyde_yang »

用树莓派Pico与一个角度传感器通信,通过I2C,写入传感器的控制指令为2字节,16位,但不知道micropython如何写入16位,以下代码,写入后没有用,求大神帮忙!from machine import Pin, I2Cimport time, ustruct, binasciifrom time import sleep i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq = 400000)  #Define the I2C pin and frequency # Print out any addresses founddevices = i2c.scan()if devices:    for d in devices:        print(hex(d))        # Attitude sensor address   as_addr = 0x50 # Attitude sensor Regaddras_reg_addr = 0x1a                 # Set I2C addressas_reg_save = 0x00                # Save configurationas_reg_unlock = 0x69            # Unlock the module  # Main# Send 0x88 and 0xb5 to unlock the moduledata2 = 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 0x10data3 = b'\x10\x00'i2c.writeto_mem(as_addr, as_reg_addr, data3)sleep(0.5) #Send 0x00 and 0x00 to save the configdata4 = b'\x00\x00'i2c.writeto_mem(as_addr, as_reg_save, data4)sleep(0.5)

头像
shaoziyang
帖子: 3917
注册时间: 2019年 10月 21日 13:48

Re: 树莓派Pico I2C问题求教!

#2

帖子 shaoziyang »

关于i2c的16位方式用法,可以参考社区的驱动

https://github.com/micropython-Chinese- ... /I2C_16bit

taobaowang
帖子: 35
注册时间: 2022年 2月 20日 04:42

Re: 树莓派Pico I2C问题求教!

#3

帖子 taobaowang »

我来排版
from machine import Pin, I2C
import time, ustruct, binasciifrom time
import sleep 
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq = 400000) 
#Define the I2C pin and frequency 
# Print out any addresses
founddevices = i2c.scan()if devices:    for d in devices:        print(hex(d))        
# Attitude sensor address   as_addr = 0x50 
# Attitude sensor Regaddras_reg_addr = 0x1a                
# Set I2C addressas_reg_save = 0x00               
# Save configurationas_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)

回复

  • 随机主题
    回复总数
    阅读次数
    最新文章