在 Raspberry Pi 上使用 Python smbus - 与语法混淆 [英] Using Python smbus on a Raspberry Pi - confused with syntax

查看:27
本文介绍了在 Raspberry Pi 上使用 Python smbus - 与语法混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use python-smbus on a Raspberry Pi to communicate with an MMA7660 accelerometer chip using I2C.

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

In the code below, I am reading registers 0x00, 0x01, 0x02 and 0x03 of the chip, and I am getting the exact same values for all. Looking at the values, and tilting the chip, I can see that they all correspond to register 0x00, the X value register.

输出:

...
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 语法上做错了吗?我确实查看了此处的文档.

Am I doing something wrong with the smbus syntax? I did look at the documentation here.

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

I have verified that the chip works - I can communicate fine with it using an Arduino and setting the registers in the same order as above.

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

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

As per Sylvain's comment, I am attaching oscilloscope output for SDA/SCL lines for the following code:

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

更新 #2:

我猜树莓派上的 I2C 存在一个已知问题 - 没有重复启动".

I guess there is a known problem with I2C on Raspberry Pi - there is no "Repeated Start".

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

根据 Linux SMBus 规范:

According to the Linux SMBus spec:

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) 之前清楚地显示了一个 STOP (P).

But when I tried it, the osciiloscope clearly shows a STOP (P) before the Repeated Start (S).

所以我想我不走运在 Pi 上使用 I2C 硬件与 MMA7760 通信.

So I guess I am out of luck for using I2C hardware on the Pi to talk to the MMA7760.

推荐答案

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

if you read all the needed registers at once, it works fine:

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] 

这篇关于在 Raspberry Pi 上使用 Python smbus - 与语法混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆