armos 发表于 2022-1-29 00:10:31

arduino I2C从机接收其他主机写入的数据怎么做呢

下面是和主机读取的代码,写入的话应该怎么做
// Wire Slave Receiver
// 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

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

int addr = 0;

void setup() {
Wire.begin(16);                // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Wire.onRequest(ggyy);
Serial.begin(9600);         // start serial for output
}

void loop() {
delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
while (1 < Wire.available())
{ // loop through all but the last
    char c = Wire.read(); // receive byte as a character
    Serial.print("char:");
    Serial.print(c,HEX);         // print the character
}
addr = Wire.read();    // receive byte as an integer
Serial.print("int:");
Serial.println(addr,HEX);         // print the integer
W
}

void ggyy()
{
//Wire.write("5653454353453543"); // 送出 6 个char给IC 上的主机
if(addr == 0x03)
{
    Wire.write(0x35);
}
   if(addr == 0x04)
{
    Wire.write(0x36);
}
}
页: [1]
查看完整版本: arduino I2C从机接收其他主机写入的数据怎么做呢