您好我很难从Adafruit' 16频道驱动程序中读取输出值'。如果我的词汇不合适,请原谅。
该项目是通过Adafruit的16通道I2C驱动器将HC-SRO4超声波传感器连接到Arduino。为了使这个电路工作,我需要通过I2C驱动程序写入和读取值。
当然,HC-SRO4超声波传感器具有VCC,GND, TRIG 和 ECHO 引脚。为了运行传感器,我增加和减少TRIG上的值,然后通过ECHO引脚监控信号响应。
问题是我无法通过I2C写入或读取数字值。我喜欢使用writeDigital或pulseIn,但他们的寻址系统似乎并没有扩展到驱动程序的地址。
我尝试重写pulseIn库标准函数来读取I2C板的十六进制地址。
unsigned long pulseIn2(uint8_t pin, uint8_t state, unsigned long timeout)
{
// cache the port and bit of the pin in order to speed up the
// pulse width measuring loop and achieve finer resolution. calling
// digitalRead() instead yields much coarser resolution.
uint8_t bit = pin;
uint8_t port = digitalPinToPort(pin);
uint8_t stateMask = (state ? bit : 0);
unsigned long width = 0; // keep initialization out of time critical area
// convert the timeout from microseconds to a number of times through
// the initial loop; it takes 16 clock cycles per iteration.
unsigned long numloops = 0;
unsigned long maxloops = microsecondsToClockCycles(timeout) / 16;
// wait for any previous pulse to end
while ((*portInputRegister(port) & bit) == stateMask)
if (numloops++ == maxloops)
return 0;
// wait for the pulse to start
while ((*portInputRegister(port) & bit) != stateMask)
if (numloops++ == maxloops)
return 0;
// wait for the pulse to stop
while ((*portInputRegister(port) & bit) == stateMask) {
if (numloops++ == maxloops)
return 0;
width++;
}
// convert the reading to microseconds. The loop has been determined
// to be 20 clock cycles long and have about 16 clocks between the edge
// and the start of the loop. There will be some error introduced by
// the interrupt handlers.
return clockCyclesToMicroseconds(width * 21 + 16);
}
我认为这适用于使用I2C地址的函数调用
pulseIn2(0x41, HIGH);
但那完全没有用。我的问题是 - 有没有更简单的方法来通过I2C寻址引脚?我需要一种轻松读取和写入值的方法 - 并使用标准函数,如 pulseIn 。
非常感谢任何帮助。我甚至都不知道FRTM的手册。