How do you transmit and read a uint16_t over I2C?
I'm trying to read two uint16_t values from a slave device, and I'm seeing nonsensical readings.
This is the code on my slave Arduino Uno:
#include <Wire.h>
void send_wire_int(uint16_t num){
// Send low byte
Wire.write((uint8_t)num);
// Send high byte
num >>= 8;
Wire.write((uint8_t)num);
}
// This is called from the I2C host.
void I2C_Send()
{
// send_wire_int(acount_abs);// Send encoder A count.
// send_wire_int(bcount_abs);// Send encoder B count.
send_wire_int(0);
send_wire_int(0);
}
void setup(){
Wire.begin(30); // initialize I²C library and set slave address
Wire.onRequest(I2C_Send); // define I²C slave transmit ISR
Wire.flush();
Wire.setTimeout(1000L);
}
void loop(){
//stuff
}
All it does is wait for the master to request data. Originally, it was sending the values of two registers back, but these values were also garbled or non-changing, so I changed it to just send back 0s but I still get the identical garbled values.
This is the code on my master Arduino Uno:
#include <Wire.h>
uint16_t receive_wire_int(){
while(Wire.available() < 1){}
// Read low byte into rxnum
uint16_t rxnum = Wire.read();
while(Wire.available() < 1){}
// Read high byte into rxnum
rxnum += Wire.read() << 8;
return rxnum;
}
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Wire.setTimeout(1000L);
Serial.begin(9600); // start serial for output
}
void loop() {
Serial.println("Requesting data..."); Serial.flush();
Wire.requestFrom(30, 4);
uint16_t acount = receive_wire_int();
uint16_t bcount = receive_wire_int();
Serial.println(String("acount:")+String(acount)+String(" bcount:")+String(bcount));
delay(500);
}
As you can see, it simply requests the two uint16_t values in 4 bytes, and then prints them to the Serial (my laptop's terminal).
The output I'm seeing is:
acount:65280 bcount:65535
when I would expect:
acount:0 bcount:0
What I am I doing wrong?
Wire.read()
, save for the first, return-1
, which becomes0xffff
when cast touint16_t
. Could you check for this condition?