在上一篇文章 詳解 I2C的末尾,有這樣一張波形圖:
實例2
這個對應的就是常見的從設備寄存器讀數據的波形,只是在寫數據時多了一個字節。
通用的從 I2C 設備讀寄存器數據的模式應該是這樣的:
從從設備寄存器讀數據示例
在mpu6050 數據表 中的9.3 I2C Communications Protocol
章節說明了 mpu6050 的 i2c 接口協議,符合通用模式。
mpu6050 單字節寫和加速寫序列如下:
mpu6050單字節寫和加速寫
mpu6050 單字節讀和加速讀序列如下:
mpu6050單字節讀和加速讀
上面的加速寫和加速讀都是以 2 個數據字節為例的。
從實例 2 的波形圖可以看出,啟動傳輸后,寫了0x12``0x13``0x14
3 個字節,這 3 個字節后都是NACK
,然后是重復起始信號,跟著剛才地址字節0x12
+1
即0x13
,I2C 從寫轉換到讀,然后開始讀數據字節。
對應代碼為:
result = ch347_driver.i2c_set(device_index, 1)
if result:
print("Success to set I2C speed.")
else:
print("Failed to set I2C speed.")
result = ch347_driver.i2c_set_delay_ms(device_index, 1)
if result:
print("Success to set I2C delay.")
else:
print("Failed to set I2C delay.")
result = ch347_driver.stream_i2c(device_index, b'x12x13x14', 8)
if result:
print("Success!")
else:
print("Failed!")
將上面這段代碼稍加修改:
result = ch347_driver.i2c_set(device_index, 1)
if result:
print("Success to set I2C speed.")
else:
print("Failed to set I2C speed.")
result = ch347_driver.i2c_set_delay_ms(device_index, 1)
if result:
print("Success to set I2C delay.")
else:
print("Failed to set I2C delay.")
result = ch347_driver.stream_i2c(device_index, b'xd0x75', 1)
if result:
print("Success! result:", result.hex())
else:
print("Failed!")
mpu6050 模塊的地址是0b1101000
(AD0 = 0),左移 1 位得到設備寫地址0xd0
,讀數據時的讀地址 ch347 會自動產生,波形如下:
讀寄存器0x75
還沒有連接 mpu6050 模塊,所以沒有數據返回。現在把 mpu6050 模塊 i2c 接口連接到 ch347 模塊上,運行程序,結果為:
? python test.py
Successfully opened device index: 0
Device Information:
iIndex: 0
DevicePath: ?USB#VID_1A86&PID_55DB&MI_02#7&2887B016&0&0002#{5446F048-98B4-4EF0-96E8-27994BAC0D00}
UsbClass: 0
FuncType: 1
DeviceID: USBVID_1A86&PID_55DB&MI_02#7&2887B016&0&0002#
ChipMode: 1
DevHandle: 912
BulkOutEndpMaxSize: 512
BulkInEndpMaxSize: 512
UsbSpeedType: 1
CH347IfNum: 0
DataUpEndp: 6
DataDnEndp: 6
ProductString:
ManufacturerString:
WriteTimeout: 500
ReadTimeout: 500
FuncDescStr: USB2.0 To SPI&IIC
FirewareVer: 64
ch347.mDeviceInforS object at 0x000002465A1E63C0 >
Version Information:
Driver Version: 35
DLL Version: 34
Device Version: 64
Chip Type: 1
Success to set I2C speed.
Success to set I2C delay.
Success! result: 68
Successfully closed device index: 0
可以看到返回了數據0x68
,0x75
是'WHO_AM_I'寄存器,默認值為0x68
。
'WHO_AM_I'寄存器
完整代碼:
import ch347
dll_path = "ch347dlla64.dll" # Replace with the actual path to the DLL
device_index = 0 # Set the device index according to your requirements
ch347_driver = ch347.CH347Driver(dll_path)
result = ch347_driver.open_device(device_index)
if result:
print(f"Successfully opened device index: {device_index}")
else:
print(f"Failed to close device index: {device_index}")
result, device_info = ch347_driver.get_device_info(device_index)
if result:
print("Device Information:")
print(f"iIndex: {device_info.iIndex}")
print(f"DevicePath: {device_info.DevicePath.decode()}")
print(f"UsbClass: {device_info.UsbClass}")
print(f"FuncType: {device_info.FuncType}")
print(f"DeviceID: {device_info.DeviceID.decode()}")
print(f"ChipMode: {device_info.ChipMode}")
print(f"DevHandle: {device_info.DevHandle}")
print(f"BulkOutEndpMaxSize: {device_info.BulkOutEndpMaxSize}")
print(f"BulkInEndpMaxSize: {device_info.BulkInEndpMaxSize}")
print(f"UsbSpeedType: {device_info.UsbSpeedType}")
print(f"CH347IfNum: {device_info.CH347IfNum}")
print(f"DataUpEndp: {device_info.DataUpEndp}")
print(f"DataDnEndp: {device_info.DataDnEndp}")
print(f"ProductString: {device_info.ProductString.decode()}")
print(f"ManufacturerString: {device_info.ManufacturerString.decode()}")
print(f"WriteTimeout: {device_info.WriteTimeout}")
print(f"ReadTimeout: {device_info.ReadTimeout}")
print(f"FuncDescStr: {device_info.FuncDescStr.decode()}")
print(f"FirewareVer: {device_info.FirewareVer}")
print(repr(device_info))
else:
print("Failed to get device information.")
result, driver_ver, dll_ver, device_ver, chip_type = ch347_driver.get_version(device_index)
if result:
print("Version Information:")
print(f"Driver Version: {driver_ver}")
print(f"DLL Version: {dll_ver}")
print(f"Device Version: {device_ver}")
print(f"Chip Type: {chip_type}")
else:
print("Failed to get version information.")
result = ch347_driver.i2c_set(device_index, 1)
if result:
print("Success to set I2C speed.")
else:
print("Failed to set I2C speed.")
result = ch347_driver.i2c_set_delay_ms(device_index, 1)
if result:
print("Success to set I2C delay.")
else:
print("Failed to set I2C delay.")
result = ch347_driver.stream_i2c(device_index, b'xd0x75', 1)
if result:
print("Success! result:", result.hex())
else:
print("Failed!")
# Example usage of CH347CloseDevice
result = ch347_driver.close_device(device_index)
if result:
print(f"Successfully closed device index: {device_index}")
else:
print(f"Failed to close device index: {device_index}")
-
模塊
+關注
關注
7文章
2707瀏覽量
47473 -
寄存器
+關注
關注
31文章
5343瀏覽量
120363 -
I2C
+關注
關注
28文章
1487瀏覽量
123754 -
MPU6050
+關注
關注
39文章
307瀏覽量
71408
發布評論請先 登錄
相關推薦
評論