浏览代码

All master/slave read & write examples Blink LED

main
PaulStoffregen 7 年前
父节点
当前提交
4e3317945a
共有 4 个文件被更改,包括 16 次插入1 次删除
  1. +5
    -0
      examples/master_reader/master_reader.ino
  2. +5
    -0
      examples/master_writer/master_writer.ino
  3. +5
    -0
      examples/slave_receiver/slave_receiver.ino
  4. +1
    -1
      examples/slave_sender/slave_sender.ino

+ 5
- 0
examples/master_reader/master_reader.ino 查看文件

@@ -12,8 +12,11 @@

#include <Wire.h>

int led = LED_BUILTIN;

void setup()
{
pinMode(led, OUTPUT);
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
@@ -22,6 +25,7 @@ void loop()
{
Serial.print("read: ");

digitalWrite(led, HIGH); // briefly flash the LED
Wire.requestFrom(8, 6); // request 6 bytes from slave device #8

while(Wire.available()) { // slave may send less than requested
@@ -30,5 +34,6 @@ void loop()
}

Serial.println();
digitalWrite(led, LOW);
delay(500);
}

+ 5
- 0
examples/master_writer/master_writer.ino 查看文件

@@ -12,8 +12,11 @@

#include <Wire.h>

int led = LED_BUILTIN;

void setup()
{
pinMode(led, OUTPUT);
Wire.begin(); // join i2c bus (address optional for master)
}

@@ -21,10 +24,12 @@ byte x = 0;

void loop()
{
digitalWrite(led, HIGH); // briefly flash the LED
Wire.beginTransmission(9); // transmit to device #9
Wire.write("x is "); // sends five bytes
Wire.write(x); // sends one byte
Wire.endTransmission(); // stop transmitting
digitalWrite(led, LOW);

x++;
delay(500);

+ 5
- 0
examples/slave_receiver/slave_receiver.ino 查看文件

@@ -12,8 +12,11 @@

#include <Wire.h>

int led = LED_BUILTIN;

void setup()
{
pinMode(led, OUTPUT);
Wire.begin(9); // join i2c bus with address #9
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
@@ -28,10 +31,12 @@ void loop()
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
digitalWrite(led, HIGH); // briefly flash the LED
while(Wire.available() > 1) { // 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
digitalWrite(led, LOW);
}

+ 1
- 1
examples/slave_sender/slave_sender.ino 查看文件

@@ -12,7 +12,7 @@

#include <Wire.h>

int led = BUILTIN_LED;
int led = LED_BUILTIN;

void setup()
{

正在加载...
取消
保存