我有一个连接到从属 arduino 的 OPT101 来测量光强度。我想将从 OPT101 电路接收到的数据发送到主 arduino,该主 arduino 将在串行监视器上打印数据。当我测试代码时,屏幕上没有显示任何内容。 (我知道这不是我的 i2c 连接,因为我通过发送“hello”来测试它)。我使用 arduino leonardo 作为从属设备,使用 arduino uno 作为主设备。
OPT101电路的代码是:
#define inPin0 0
void setup() {
Serial.begin(9600);
Serial.println();
}
void loop() {
int pinRead0 = analogRead(inPin0);
double pVolt0 = pinRead0 / 1024.00 * 5.0;
Serial.print(pVolt0, 4 );
Serial.println();
delay(100);
}
我厌倦了将从属代码和我的 OPT101 代码结合起来得到这个: #包括
#define inPin0 0
void setup() {
Wire.begin(2);
}
void loop() {
Wire.beginTransmission(2);
Wire.onRequest(requestEvent);
Wire.endTransmission();
}
void requestEvent()
{
int pinRead0 = analogRead(inPin0);
int pVolt0 = pinRead0 / 1024.0 * 5.0;
Wire.write((byte)pVolt0);
}
这是我的主代码:
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(14400);
Wire.requestFrom(2, 8);
while(Wire.available())
{
char c = Wire.read();
Serial.print(c);
}
}
void loop()
{
}
最佳答案
您必须按照下面描述的步骤在主从 I2C 设备之间进行通信:
- 只有master可以发起读或写请求。
- 读取或写入请求必须同步。这意味着,从设备只能在主设备请求数据后才返回数据,反之亦然。
- 请勿使用从机地址 0 - 7。它们是 reserved 。使用范围在 8 到 127 之间的从机地址。
- 在 Arduino I2C 上,您只能发送和接收一个字节。要发送或接收具有多个字节的整数、 double ,您需要首先将它们拆分,然后在另一侧,您必须将它们组合成其等效的数据类型。 (如果我错了,请纠正我。)
您的代码应如下所示:
主草图:
#include <Wire.h>
#define SLAVE_ADDRESS 0x40
// This macro reads two byte from I2C slave and converts into equivalent int
#define I2C_ReadInteger(buf,dataInteger) \
buf[0] = Wire.read(); \
buf[1] = Wire.read(); \
dataInteger = *((int *)buf);
// Returns light intensity measured by 'SLAVE_ADDRESS' device
int GetLightIntensity()
{
byte Temp[2];
int Result;
// To get integer value from slave, two are required
int NumberOfBytes = 2;
// Request 'NumberOfBytes' from 'SLAVE_ADDRESS'
Wire.requestFrom(SLAVE_ADDRESS, NumberOfBytes);
// Call macro to read and convert bytes (Temp) to int (Result)
I2C_ReadInteger(Temp, Result);
return Result;
}
void setup()
{
// Initiate I2C Master
Wire.begin();
// Initiate Serial communication @ 9600 baud or of your choice
Serial.begin(9600);
}
void loop()
{
// Print light intensity at defined interval
Serial.print("Light Intensity = ");
Serial.println(GetLightIntensity());
delay(1000);
}
从属草图:
#include <Wire.h>
#define SLAVE_ADDRESS 0x40
#define inPin0 0
// Preapres 2-bytes equivalent to its int
#define IntegerToByte(buf,intData) \
*((int *)buf) = intData;
// Sends int to Master
void I2C_SendInteger(int Data)
{
byte Temp[2];
// I2C can only send a byte at a time.
// Int is of 2bytes and we need to split them into bytes
// in order to send it to Master.
// On Master side, it receives 2bytes and parses into
// equvivalent int.
IntegerToByte(Temp, Data);
// Write 2bytes to Master
Wire.write(Temp, 2);
}
void setup()
{
// Initiate I2C Slave @ 'SLAVE_ADDRESS'
Wire.begin(SLAVE_ADDRESS);
// Register callback on request by Master
Wire.onRequest(requestEvent);
}
void loop()
{
}
//
void requestEvent()
{
// Read sensor
int pinRead0 = analogRead(inPin0);
int pVolt0 = pinRead0 / 1024.0 * 5.0;
// Send int to Master
I2C_SendInteger(pVolt0);
}
This code is tested on Arduino Version: 1.6.7. For more information regarding I2C communication, refer Arduino Example: Master Reader
关于Arduino 到 arduino i2c 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24168340/