我正在尝试使用树莓派的ACS764霍尔效应电流传感器。该传感器将检测电流,并通过芯片内置的I2C接口返回其值。我已经按照说明书接通了电路。在我的Raspberry Pi Python代码中,我可以向传感器写入数据,也可以从传感器读取数据,但是我读取的数据总是相同的值。
下面是我读取传感器的简单代码:
import datetime
import smbus
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(37, GPIO.OUT) #Connected to the ACS764 Freeze pin
bus=smbus.SMBus(1)
#Freeze the data
GPIO.output(37, True)
#Read the values
bus.write_byte(0x60, 0x00) #Simulate the combined data transmission format
data=bus.read_i2c_block_data(0x60, 0x00)
print data
#Unfreeze the data
GPIO.output(37, False)
GPIO.cleanup()然而,当我运行脚本时,即使我改变了当前要检测的值,值也总是显示相同的值。
pi@Raspberry:~ $ python i2cAcs764.py
[0, 0, 3, 0, 0, 3, 0, 0, 3, 0, 0, 3, 0, 0, 3, 0, 0, 3, 0, 0, 3, 0, 0, 3, 0, 0, 3, 0, 0, 3, 0, 0]根据ACS764规范,要读取传感器数值,我需要使用“组合数据传输”格式。但是,我在Python SMBus库中没有找到任何允许我使用组合数据传输的函数,所以现在我使用"bus.write_byte“函数来模拟”组合数据传输“。下面是该规范的屏幕截图。
ACS764 Datasheet Snapshoot
我现在的问题是,如何使用Python SMBus I2C库来执行ACS764芯片的“组合数据传输”读取?
请指教,谢谢。
发布于 2016-03-25 22:59:45
在谷歌搜索了几天后,我终于找到了我上面问题的解决方案。答案是,树莓I2C接口确实支持“组合数据传输”(又称重复启动),但默认情况下不启用。您需要通过以下命令启用该设置。
sudo su -
echo -n 1 > /sys/module/i2c_bcm2708/parameters/combined
exit有关更多信息,请参阅i2c repeated start transactions 。
根据smbus规范,支持重复启动的函数是i2c_smbus_read_i2c_block_data(),在Python库中称为read_i2c_block_data()。
有关更多详细信息,请参阅SMBus Protocol Summary。
下面是我从需要重复启动的ACS764霍尔效应传感器芯片中读取数据的示例代码。
import datetime
import smbus
import time
bus=smbus.SMBus(1)
# Write setting parameter to the chip
data = [0x02, 0x02, 0x02]
bus.write_i2c_block_data(0x60, 0x04, data)
# Read the data out of the chip that require Repeated Start
data=bus.read_i2c_block_data(0x60, 0x00)
print data我很高兴找到了解决方案,并希望那些面临同样问题的人能从这篇文章中得到帮助。谢谢大家!
https://stackoverflow.com/questions/36202300
复制相似问题