簡體   English   中英

除了文檔之外,是否有 python-periphery 庫 i2c 代碼示例或詳細說明?

[英]Is there a python-periphery library i2c code example or detailed explanation other than the documentation?

這是我在 Stackoverflow 中的第一個問題,如果我在寫這個問題時犯了錯誤,請給我反饋以糾正自己。

我想為我的樹莓派使用 i2c 通信。 我想使用 python-periphery(我知道那里有 smbus)。 在文檔https://python-periphery.readthedocs.io/en/latest/i2c.html 中,關於如何使用該庫的信息並不多。 這是文檔中的代碼:

from periphery import I2C

# Open i2c-0 controller
i2c = I2C("/dev/i2c-0")

# Read byte at address 0x100 of EEPROM at 0x50
msgs = [I2C.Message([0x01, 0x00]), I2C.Message([0x00], read=True)]
i2c.transfer(0x50, msgs)
print("0x100: 0x{:02x}".format(msgs[1].data[0]))

i2c.close()

我試圖用傳感器寫/讀,它成功了。 但是在我的測試代碼中我使用了這個:

#Write to 0x09 register,this byte: 0x13
W9 = [I2C.Message([0x09,0x13])]
i2c.transfer(DFLT_ADDRESS,W9)

#Read from 0x09 register
R = [I2C.Message([0x09]), I2C.Message([0x00], read=True)]
i2c.transfer(DFLT_ADDRESS,R)
print(R[1].data[0])

(我已經使用 smbus 檢查數據是否寫入寄存器並正確讀取。因此,測試代碼正在運行。)

我想知道這一行中的 0x100(我認為 [0x01, 0x00] 這是 0x100)在msgs = [I2C.Message([0x01, 0x00]), I2C.Message([0x00], read=True)]msgs = [I2C.Message([0x01, 0x00]), I2C.Message([0x00], read=True)] ,當我嘗試這樣做時,它會將 0x00 寫入我的傳感器中的寄存器。 是不是因為我的傳感器有 8 位,但我嘗試寫入 16 位? 那么,誰能解釋一下文檔示例中發生了什么?

也許你可以試試我在 raspberry pi zero 上使用的這個代碼:

# coding=utf-8
# date : 25/05/2021
# dossier : piPeri
# fichier : pP_readByte01.py
# check the result with these commands : 
# i2cset -y 1 0x51 0x10 0x01
# i2cget -y 1 0x51
# 3x times 

from periphery import I2C

print(" Read 3 bytes starting at register address 0x1001 of EEPROM at I2C address 0x51 ")


# Open i2c-0 controller
i2c = I2C("/dev/i2c-1")

# create array to hold received data ( 3 bytes in my case )
dataRX = [0,0,0]

# create message to place 16 bits in memory register of eeprom
msgWritePointer = I2C.Message([0x10,0x01],read=False)

# create message to read data according to length of array 'dataRX'
msgRead = I2C.Message(dataRX,read=True)

# messages are assembled, order is important 
msgs = [msgWritePointer, msgRead]

# call transfer function 
i2c.transfer(0x51, msgs)

# print received bytes
for i in range(0,len(msgs[1].data)):
    print(hex(msgs[1].data[i]))

i2c.close()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM