slave-receiver2 コード
// 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
// Modified 23 June 2017 by Papaan
#include < Wire . h >
void setup ( ) {
Wire . begin ( 8 ) ; // join i2c bus with address #8
Wire . onReceive ( receiveEvent ) ; // register event
Serial . begin ( 115200 ) ; //Arduino IDE 1.0.6
//Serial.begin(230400);//Arduino IDE 1.6.7
Serial . println ( "===Recieve Start===" ) ;
}
void loop ( ) {
delay ( 100 ) ;
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
char c ;
int x , y ;
void receiveEvent ( int howMany ) {
while ( 2 < Wire . available ( ) ) { // loop through all but the last
c = Wire . read ( ) ; // receive byte as a character
if ( c == '*' ) {
break ;
}
Serial . print ( c ) ; // print the character
}
Serial . print ( " is " ) ;
if ( c == '*' ) {
//debugRegPrint
x = Wire . read ( ) ;
y = Wire . read ( ) ; // receive byte as an integer
x = 256 * y + x ;
Serial . print ( "0x" ) ;
Serial . println ( x , HEX ) ; // print the integer
} else {
//debugPrint
x = Wire . read ( ) ;
y = Wire . read ( ) ; // receive byte as an integer
x = 256 * y + x ;
Serial . println ( x ) ; // print the integer
}
}
【このカテゴリーの最新記事】
- no image
- no image
- no image