This is my I2C packet, and I am not understanding how to send data to my slave

Click the above to check the I2C packet structure. I am not getting any response from the slave. This is my master code.

I am writing to the slave and requesting for every 100 ms, and there is no response form the slave device. help me.

#include <Wire.h>
#include <TimerOne.h> // Header file for TimerOne library

//#ifndef TWI_FREQ
//#define TWI_FREQ 100000L
//#endif

#define LED0 13 // Pin 3
#define TIMER_US 100000 // 100mS set timer duration in microseconds

char send_data[8] = {'F','F','F','F','F','F','F','F'}; //dummy initial values
char receive_data[8] = {'1','1','1','1','1','1','1','1'}; //dummy initial values

int message_flag = 0;

void setup()
{
pinMode(LED0, OUTPUT); // Set the pins as outputs
Timer1.initialize(TIMER_US); // Initialise timer 1
Wire.begin();
Wire.setClock(100000);
Serial.begin(9600);
}

void loop()
{
Timer1.attachInterrupt( timerIsr ); // attach the ISR routine here

if(message_flag == 1)
{
Wire.beginTransmission(0x20); // transmit to device #8
Serial.println("Arduino to HMI");
// for(int i=0; i<8; i++)
// {
// Wire.write(send_data*); // sends five bytes*
// Serial.print(send_data*);
_
// }_
_
Wire.write(0x40);_
_
Wire.write(0x00);_
_
Wire.write(0x00);_
_
Wire.write(0x00);_
_
Wire.write(0x41);*_

* Serial.println(" ");*
* Wire.endTransmission(); // stop transmitting*

* Wire.requestFrom(0x20, 8);*
* while(Wire.available() > 1)*
* {*
* Serial.println("HMI to Arduino");*
* for(int j =0; j<8; j++)*
* {*
* receive_data[j] = Wire.read();
Serial.print(receive_data[j]);
_
}_
_
Serial.println("");_
_
}_
message_flag = 0;
_
}_
_
}_
void timerIsr()
_
{_
_
digitalWrite( LED0, digitalRead( LED0 ) ^ 1 ); // Toggle LED 0*_
* message_flag = 1;
_
}*_

I think it's about time to get used to using code tags, don't you?

what is the slave? please share the datasheet if possible

Thanks for that Awol,

Lets do this,

Here is the code:

#include <Wire.h> //includes I2C library
#include <TimerOne.h> // Header file for TimerOne library to generate timer interrupt

#ifndef TWI_FREQ //code to set 100KHZ
#define TWI_FREQ 100000L
#endif

#define LED0 13 // Pin 3
#define TIMER_US 100000 // 100mS set timer duration in microseconds

char send_data[8] = {'F','F','F','F','F','F','F','F'}; //dummy initial values
char receive_data; //dummy initial values

int message_flag = 0;

void setup()
{
pinMode(LED0, OUTPUT); // Set the pins as outputs
Timer1.initialize(TIMER_US); // Initialise timer 1
Wire.begin();
Wire.setClock(100000);
Serial.begin(9600);
}

void loop()
{
Timer1.attachInterrupt( timerIsr ); // attach the ISR routine here

if(message_flag == 1)
{
Wire.beginTransmission(0x20); // transmit to device #2
Serial.println("Arduino to HMI");

Wire.write(0x40); //send data 1
Wire.write(0x00); //send data 2
Wire.write(0x00); //send data 3

Serial.println(" ");
Wire.endTransmission(); // stop transmitting

Wire.requestFrom(0x20, 1); //requesting 1 byte of data from 20 slave
while(Wire.available() > 1)
{
Serial.println("HMI to Arduino");

receive_data = Wire.read(); //reading
Serial.print(receive_data); //print in serial

Serial.println("");
}
message_flag = 0; // flag to run for every 100 milli seconds
}
}

void timerIsr()
{
digitalWrite( LED0, digitalRead( LED0 ) ^ 1 ); // Toggle LED 0
message_flag = 1; // flag to run for every 100 milli seconds
}

Please see reply #1

oops..!! still you need some more code tags to understand the code ? :frowning:

britto:
oops..!! still you need some more code tags to understand the code ? :frowning:

Why can't you just do as requested?

code:

#include <Wire.h> // included i2c library
byte data_1 = 0x02; // first dummy data byte stored in an variable to pass to HMI
byte data_2 = 0x00; // second data byte (not used for testing now)
byte data_3 = 0x00; // third data byte (not used for testing now)
byte message_1; // Variable to receive first byte
byte message_2; // Variable to receive second byte
byte value_1; // acknowleding the status of transmission

void setup() {
Wire.begin(); // beginning the i2c transmission
Serial.begin(9600); // beginning serial for display
}

void loop(){
Wire.beginTransmission(0x20); // begin to transmit to device 0x20
Wire.write(data_1); // sends one byte
Wire.write(data_2); // second byte transmission, not under test now
Wire.write(data_3); // third byte transmission, not under test now
value_1 = Wire.endTransmission(); // stop transmitting & return the acknowledgement
Serial.println(value_1); // printing the transmission status in serial display

Wire.requestFrom(0x20, 2); // request 2 bytes from slave device #8
while (Wire.available()) { // read the bytes in the data pin. if any
message_1 = Wire.read(); // reading first byte
message_2 = Wire.read(); // reading second byte
Serial.println(message_1); // print the readed messages
Serial.println(message_2); // print the readed messages
}
}

while, writing to slave device, this code works fine. when I try to request data byte from slave, the communication is getting interrupted. Help me to solve this issue.

your problem could be here (THIS IS WHAT USING CODE TAG MEANS BTW):

  while (Wire.available()) {                            // read the bytes in the data pin. if any
    message_1 = Wire.read();                            // reading first byte
    message_2 = Wire.read();                            // reading second byte
    Serial.println(message_1);                          // print the readed messages
    Serial.println(message_2);                          // print the readed messages
  }

did you try doing this instead?

  while (Wire.available()) {                      // output all bytes received to serial monitor
    Serial.println(Wire.read());                          // print the readed messages
  }