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

2024-01-14

我正在尝试在 Raspberry Pi 上使用 python-smbus 使用 I2C 与 MMA7660 加速计芯片进行通信。

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

Output:

...
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
...

Code:

  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 http://wiki.erazor-zone.de/wiki:linux:python:smbus:doc.

我已经验证该芯片工作正常 - 我可以使用 Arduino 与它进行良好的通信,并按照与上述相同的顺序设置寄存器。

更新 #1(2013 年 6 月 28 日):

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

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

更新#2:

我猜想 Raspberry Pi 上的 I2C 存在一个已知问题 - 没有“重复启动”。

https://raspberrypi.stackexchange.com/questions/7138/mma8452-i2c-module 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

但当我尝试时,示波器在重复启动(S)之前清楚地显示了停止(P)。

所以我想我不太幸运在 Pi 上使用 I2C 硬件与 MMA7760 通信。


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

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] 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Raspberry Pi 上使用 Python smbus - 与语法混淆 的相关文章

随机推荐