在树莓Pi上使用Python smbus-与语法混淆

2024-05-20 20:59:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在一个树莓Pi上使用python smbus与使用I2C的MMA7660加速度计芯片通信

在下面的代码中,我正在读取芯片的寄存器0x00、0x01、0x02和0x03,得到的值完全相同。看看这些值,然后倾斜芯片,我可以看到它们都对应于寄存器0x00,即X值寄存器。

输出:

...
1 1 1 2
3 3 3 3
1 1 1 1
59 60 60 60
51 51 51 51
58 58 58 58
3 3 3 3
62 62 62 62
58 58 58 58
62 62 62 62
...

代码:

  import smbus
  import time

  bus = smbus.SMBus(1)
  # I2C address for MMA7660                                                     
  addr = 0x4C
  try:
    bus.write_byte_data(addr, 0x07, 0x00)
    bus.write_byte_data(addr, 0x06, 0x10)
    bus.write_byte_data(addr, 0x08, 0x00)
    bus.write_byte_data(addr, 0x07, 0x01)
  except IOError, err:
    print err

  while True:
    try:
      x = bus.read_byte_data(addr,0x00)
      y = bus.read_byte_data(addr,0x01)
      z = bus.read_byte_data(addr,0x02)
      tr = bus.read_byte_data(addr,0x03)
      print x, y, z, tr
      time.sleep(0.25)
    except:
      print 'exiting...'
      break

我有没有做错smbus语法?我确实看过文档here

我已经验证了芯片的工作原理——我可以使用Arduino与它进行良好的通信,并按照上面的顺序设置寄存器。

更新1(2013年6月28日)

根据Sylvain的评论,我正在为SDA/SCL线路附加示波器输出,用于以下代码:

bus.write_byte(addr, 0x01)
print bus.read_byte(addr)

enter image description here

更新#2:

我想I2C在覆盆子Pi上有一个已知的问题-没有“重复启动”。

https://raspberrypi.stackexchange.com/questions/7138/mma8452-i2c-module

根据Linux SMBus规范:

SMBus Read Byte:  i2c_smbus_read_byte_data()
============================================

This reads a single byte from a device, from a designated register.
The register is specified through the Comm byte.

S Addr Wr [A] Comm [A] S Addr Rd [A] [Data] NA P

但当我尝试时,示波器在重复的开始之前清楚地显示了停止(p)。

所以我想我是运气不好,使用I2C硬件在Pi上与MMA7760交谈。


Tags: 代码readdatapibytei2c芯片寄存器
3条回答

如果您一次读取所有需要的寄存器,它可以正常工作:

import smbus
bus = smbus.SMBus(1) 

Register = bus.read_i2c_block_data(0x4c, 0x99,4)
acc_x = Register[0]*1.0
acc_y = Register[1]*1.0
acc_z = Register[2]*1.0
acc_tilt     = Register[3] 

在查看了您的示例以及为MMA7455编写的类之后,我能够编写以下内容:

import smbus
import time
import os
import math

# Define a class for the accelerometer readings
class MMA7660():
    bus = smbus.SMBus(1)
    def __init__(self):
        self.bus.write_byte_data(0x4c, 0x07, 0x00) # Setup the Mode
        self.bus.write_byte_data(0x4c, 0x06, 0x10) # Calibrate
        self.bus.write_byte_data(0x4c, 0x08, 0x00) # Calibrate
        self.bus.write_byte_data(0x4c, 0x07, 0x01) # Calibrate
    def getValueX(self):
        return self.bus.read_byte_data(0x4c, 0x00)
    def getValueY(self):
        return self.bus.read_byte_data(0x4c, 0x01)
    def getValueZ(self):
        return self.bus.read_byte_data(0x4c, 0x02)

mma = MMA7660()

for a in range(1000):
    x = mma.getValueX()
    y = mma.getValueY()
    z = mma.getValueZ()
    print("X=", x)
   print("Y=", y)
   print("Z=", z)
    time.sleep(0.2)
    os.system("clear")

这应该能达到目的。

我绝对不确定这是问题所在,但根据规范p22:

MMA7660FC is read using it’s internally stored register address as address pointer, the same way the stored register address is used as address pointer for a write. The pointer generally auto-increments after each data byte is read using the same rules as for a write (Table 5). Thus, a read is initiated by first configuring the device’s register address by performing a write (Figure 11) followed by a repeated start. The master can now read 'n' consecutive bytes from it, with the first data byte being read from the register addressed by the initialized register address.

据我所知,要“读取”寄存器,你必须从写入寄存器地址开始,然后盲目地读取一个字节。我不知道SMBus.read_byte_data是否能帮你解决这个问题,但你可以手动尝试一下:

  bus.write_byte(addr,0x00)
  x = bus.read_byte(addr)

  bus.write_byte(addr,0x01)
  y = bus.read_byte_data(addr)

  bus.write_byte(addr,0x02)
  z = bus.read_byte(addr)

  bus.write_byte(addr,0x03)
  tr = bus.read_byte(addr)

也许甚至可以:

  bus.write_byte(addr,0x00)

  x = bus.read_byte(addr)
  y = bus.read_byte_data(addr)
  z = bus.read_byte(addr)
  tr = bus.read_byte(addr)

相关问题 更多 >