Arduino 到 arduino i2c 代码

标签 arduino arduino-uno

我有一个连接到从属 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/

相关文章:

arduino - 如何通过 adafruit mcp23017.h 库使用多个 mcp23017 芯片

android - 为什么我在通过蓝牙发送 Arduino 数据时在 Android 应用程序的开头看到意外数据?

javascript - Arduino 上的以太网 Web 服务器,具有动态数据

c++ - 函数和 SD 文件创建

`return` 可以在 C 中返回 `break` 吗?

c - 如何延迟关闭继电器(毫秒)

c++ - Arduino 还是树莓派?

Arduino MKGSM 中的 HTTP POST 到 Eventhub

c++ - 使用 I2C 发送 0x00

audio - 数字音频格式