我需要从 TTP229(16 键或 8 键触摸板检测器)读取 2 个不同的字节。
我在 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()
函数?
最佳答案
尝试:
import smbus, time
bus = smbus.SMBus(1)
while True:
print bus.read_word(0xAF)
time.sleep(0.1)
不要更改地址,总线会进行转换,如果您读取字节,您将始终获得相同的第一个字节。你想一次读一个字 = 2 个字节
未测试,但可能有效,已订购并将测试
关于Python I2C 通信 TTP229,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41710567/