Send two different data types over I2C?
so m trying send values on i2c master slave. want send both characters , integers , able tell 1 being received. example program shows how @ same time if want send integer larger 2 digit number have split integer on master side , send individual bytes. (char -> 1byte....int -> 2bytes) m trying understand meaning of why example slave receiver sketch reading 2 values, character , byte. if write can send 1 byte @ time master why slave receiver receiving character , and int?
i thinking of having 2 onreceive methods receiving seperate data types doesnt appear can this.
i thinking of having 2 onreceive methods receiving seperate data types doesnt appear can this.
so figured out alternative. wanted send characters because using characters commands , have used byte command. future sake if wants send full integer master writer slave sender here 1 way of doing so.
the master writer sends integer of 700 , command of 0x03 follows
the slave receiver receiving int of 700 , command of 0x03 follows
the master writer sends integer of 700 , command of 0x03 follows
code: [select]
// wire master writer
// nicholas zambetti <http://www.zambetti.com>
// demonstrates use of wire library
// writes data i2c/twi slave device
// refer "wire slave receiver" example use this
// created 29 march 2006
// example code in public domain.
#include <wire.h>
int val = 700;
int myarray[2];
void setup()
{
wire.begin(); // join i2c bus (address optional master)
myarray[0] = (byte) (val & 0xff);
myarray[1] = (byte) ((val >> 8) & 0xff);
}
byte x = 0x03;
void loop()
{
send_data();
delay(1000);
}
void send_data()
{
wire.begintransmission(100); // transmit device #4
wire.write(myarray[0]); // sends 5 bytes
wire.write(x); // sends 1 byte
wire.endtransmission(); // stop transmitting
delay(500);
wire.begintransmission(100); // transmit device #4
wire.write(myarray[1]); // sends 5 bytes
wire.write(x); // sends 1 byte
wire.endtransmission(); // stop transmitting
delay(500);
}
the slave receiver receiving int of 700 , command of 0x03 follows
code: [select]
// wire slave receiver
// nicholas zambetti <http://www.zambetti.com>
// demonstrates use of wire library
// receives data i2c/twi slave device
// refer "wire master writer" example use this
// created 29 march 2006
// example code in public domain.
#include <wire.h>
volatile int = 0;
volatile int number;
int mybytes[2];
void setup()
{
wire.begin(100); // join i2c bus address #4
wire.onreceive(receiveevent); // register event
serial.begin(115200); // start serial output
}
void loop()
{
delay(100);
}
// function executes whenever data received master
// function registered event, see setup()
void receiveevent(int howmany)
{
while(1 < wire.available()) // loop through last
{
mybytes[i] = wire.read(); // receive byte character
//serial.println(c); // print character
}
byte x = wire.read(); // receive byte integer
serial.println(x); // print integer
i++;
if(i == 2)
{
i = 0;
number = mybytes[0] | mybytes[1] << 8;
serial.println("the number is: ");
serial.println(number);
}
}
Arduino Forum > Using Arduino > Programming Questions > Send two different data types over I2C?
arduino
Comments
Post a Comment