Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Wire: I2CSlave: move out slave->read from critical section#1061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
pennam merged 1 commit intoarduino:mainfrompennam:i2c-slave-fix-3
May 13, 2025

Conversation

@pennam
Copy link
Contributor

@pennampennam commentedMay 12, 2025
edited
Loading

Fixes master writer / slave reader

Master

#include <Wire.h>void setup(){  Wire.begin(); // join i2c bus (address optional for master)}byte x = 0;void loop(){  Wire.beginTransmission(4); // transmit to device #4  Wire.write("x is ");        // sends five bytes  Wire.write(x);              // sends one byte    Wire.endTransmission();    // stop transmitting  x++;  delay(500);}

Slave

#include <Wire.h>void setup(){  Serial.begin(9600);           // start serial for output  while(!Serial);  Wire.begin(4);                // join i2c bus with address #4  Wire.onReceive(receiveEvent); // register event  }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){  Serial.println("REQ");  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}

Tested also with Master reader / Slave writer

Master

#include <Wire.h>void setup() {  Wire.begin();        // join I2C bus (address optional for master)  Serial.begin(9600);  // start serial for output}void loop() {  Wire.requestFrom(16, 6);    // request 6 bytes from slave device #8  while (Wire.available()) { // slave may send less than requested    Serial.write(Wire.read());         // print the character  }  Serial.println();  delay(500);}

Slave

#include <Wire.h>void setup() {  Wire.begin(16);                // join I2C bus with address #8  Wire.onRequest(requestEvent); // register event}void loop() {  delay(1);}// function that executes whenever data is requested by master// this function is registered as an event, see setup()void requestEvent() {  Wire.write("hello "); // respond with message of 6 bytes  // as expected by master}

@pennampennam merged commit142f272 intoarduino:mainMay 13, 2025
11 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant

@pennam

[8]ページ先頭

©2009-2025 Movatter.jp