0
我試圖將我的加速計ADXL3458連接到運行在ubuntu mate中的Raspberry Pi 3。我已經安裝了I2C通信所需的所有必要軟件包。當我執行這個命令i2cdetect -y 1
我得到了這個結果。 使用smbus在Raspbery Pi 3中使用I2C
現在我運行此Python代碼
#!/usr/bin/env python
import smbus
import time
import math
from math import sin, cos, pi
bus = smbus.SMBus(1)
print bus
ACC_ADRESS = 0x53
acc_x = 0.0
acc_y = 0.0
acc_z = 0.0
def writeACC (register, value):
bus.write_byte_data(ACC_ADRESS, register, value)
return -1
def readACC_byte (addr):
return bus.read_byte_data(ACC_ADRESS, addr)
def readACC_word (addr):
LSB = bus.read_byte_data(ACC_ADRESS, addr)
MSB = bus.read_byte_data(ACC_ADRESS, addr + 1)
val = (MSB << 8) | LSB
return val
def setupACC():
# Sleep mode
writeACC(0x2D, 0)
# Mesurement mode
writeACC(0x2D, 8)
# enable Autu sleep mode
writeACC(0x2D, 16)
while True:
time.sleep(0.1)
acc_x = readACC_word(0x32)
acc_y = readACC_word(0x34)
acc_z = readACC_word(0x36)
print "Acc_x :\n", acc_x
print "Acc_y :\n", acc_y
print "Acc_z :\n", acc_z
time.sleep(0.5)
if __name__ == '__main__':
setupACC()
由此推斷我的I2C通信成功得到這樣的結果。但在打印結果顯示沒有任何價值
可以請幫助我面臨的問題。我做錯了什麼?
謝謝