【问题标题】:Python I2C communication TTP229Python I2C通信TTP229
【发布时间】:2017-01-18 02:47:18
【问题描述】:

我需要从 TTP229 读取 2 个不同的字节(16 键或 8 键触摸板检测器)。
我在 Python 中使用 I2C。 TTP229 datasheet PDF.
我无法读取第二个字节,但我可以获取第一个字节。

Python 代码:

import smbus
bus = smbus.SMBus(1)
adressTTP229 =  0x57 #0xAF>>1
byte1 = bus.read_byte(adressTTP229)
byte2 = bus.read_byte(adressTTP229)

byte1 始终等于byte2

这个 Arduino 代码,工作正常:

#include <Wire.h>
#define ttp229 (0xAF>>1) 

void setup() {
  Serial.begin(9600);  // start serial for output
  Wire.begin();
}

void loop() {
  delay(50);
  bool isNewData = false;
  Wire.requestFrom(ttp229,2,true);
  while (Wire.available()) { 
    uint16_t b1 = Wire.read(); // receive a first byte
    uint16_t b2 = Wire.read(); // receive a second byte
    if (b1==b2 && b2==0) {break;}
    //... 

    }
  }

如何在 Python 中使用 Arduino 的 requestFrom() 函数?

【问题讨论】:

  • 用 C 语言编写的模块,用于 Python,与芯片 TTP229 配合使用。 github libttp229

标签: python linux arduino raspberry-pi i2c


【解决方案1】:

尝试:

import smbus, time
bus = smbus.SMBus(1)
while True:
   print bus.read_word(0xAF)
   time.sleep(0.1)

不要更改地址,总线会进行转换,如果您读取字节,您将始终获得相同的第一个字节。您想一次读取一个单词 = 2 个字节

未测试,但可能有效,已订购并会测试

【讨论】:

    猜你喜欢
    • 2017-07-16
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 2023-04-02
    • 2023-03-23
    • 1970-01-01
    • 2020-12-19
    • 2021-12-23
    相关资源
    最近更新 更多