Weired behaviour in Wire.write instruction

Hi there,
I've realized a sensor device and trying to send three bytes.

I goes well e.g. by sending data like 0x7F 0x45 0x10 and data are received from the master device properly.

However as soon as my databyte (which shall be transmitted) exceed 0x7F, only 0xFF will be received.

This shouldn't be the proper behaviour????

Example code, which increases the first transmission byte every time data is requested (all fine unless byte[0] steps above 0x7F)


#define SLAVE_ADDRESS 50 //50 0x64
const long BAUDRATE = 38400;

#include <Wire.h>


// Data to be received from the master
byte receivedData = 0;

byte myData=0;
void setup()
{


  // Start the I2C communication as a master
  Wire.begin();
  // Start the I2C communication as a slave with the given address
  Wire.begin(SLAVE_ADDRESS);
  // Register a callback function to be called when data is received from the slave

  Wire.onRequest(requestEvent); // register event

}

void loop()
{
}


void requestEvent() {

  byte buffer[10];

  buffer[0] = (myData+=10);  
  buffer[1] = 10; // 0x0A


  Serial.println("I2C Request ");
  Serial.print(buffer[0]);
  Serial.print("\t");
  Serial.print(buffer[1]);
  // as expected by master

  Wire.write( buffer[0]);
  Wire.write( buffer[1]);
  Wire.write('c');

}

I'm using a I²C sniffer and confirms the behaviour!

What am I doing wrong?

In addition the sniffer output
image
s=START
p=STOP
a=ACK
Writing 0x7C goes well, however writing (0x7C+10) results in a STOP condition??

Your requestEvent() takes too much time, most probably ending up in a master timeout. Remove at least Serial output.

1 Like

No, this is definetly not causing the problem:

  1. with or without Serial output, the problem doesn't change
  2. even with serail output, the communication goes well in case of data byte <0x80

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.