我有一个 TM4C123 处理器作为 I2C 主处理器,一个 ESP8266 作为从处理器。对于 ESP,我使用的是 Arduino IDE,并在 2.5.2 版安装了 ESP8266 支持,它应该支持 I2C 从模式。但是,我无法让它工作。即使使用 Arduino Slave_receiver 示例,从站也不会确认(ACK)我在示波器上显示的主站请求。
为了确保我至少使用一次正确的地址,我在主机上实施了地址扫描。为了确保我在 ESP 上使用正确的引脚,我首先在 ESP 上实现了主模式,并使用 I2C 从设备进行了引脚扫描。所以我相当确定这都不是问题。
我正在使用 Olimex Mod-wifi 板,带有 SDA 引脚 12 和 SCL 引脚 13(示意图在这里 https://github.com/OLIMEX/ESP8266/blob/master/HARDWARE/MOD-WIFI-ESP8266-DEV/MOD-WiFi-ESP8266-DEV%20revision%20B1/MOD-WiFi-ESP8266-DEV_Rev_B1.pdf)
有人可以帮我解决这个问题吗?这是我的代码:
// Wire Slave Receiver
// by devyte
// based on the example by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this
// This example code is in the public domain.
#include <Wire.h>
#define SDA_PIN 12
#define SCL_PIN 13
const int16_t I2C_SLAVE = 0x12;
void setup() {
Serial.begin(115200); // start serial for output
Wire.begin(SDA_PIN, SCL_PIN, I2C_SLAVE); // new syntax: join i2c bus (address required for slave)
Wire.onReceive(receiveEvent); // register event
}
void loop() {
delay(1000);
Serial.println("Loop");
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(size_t howMany) {
(void) howMany;
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}
我在使用 ESP8266 时遇到了类似的问题。通过 I²C 在 Arduino Nano(从设备)和 ESP8266(主设备)之间发送数据没有问题。但是当我切换模式(Arduino Nano = Master 和 ESP8266 = Slave)时,Wire 示例不起作用。
我针对此问题的解决方法是将 I²C 工作频率从 100 kHz 降低到大约 20kHz。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)