I2C interfacing with ATmega32 and ArduinoMega2560 failed

I’m trying out I2C interfacing the ATmega32 with Arduino Mega2560 Board without success.

The Arduino Board is configured to be the Master Read.
The Atmega32 is configured to be the Slave Write.

I have connected the wires like in the Image below + Pull-up resistors(10kOhm):

<IMAGE ATTACH 23>

The Code for ATmega32 in Atmel 6:

// Program for Slave mode (Write)
#include<avr/io.h>
#include<util/delay.h>

void TWI_init_slave(void);
void TWI_match_write_slave(void);
void TWI_write_slave(void);



unsigned char write_data,recv_data;

int main(void)
{
	
	TWI_init_slave(); // Function to initilaize slave
	while(1)
	{

                write_data=0xFC; // Togglem the receive data
		TWI_match_write_slave(); //Function to match the slave address and slave dirction bit(write)
		TWI_write_slave();       // Function to write data
	}
}

void TWI_init_slave(void) // Function to initilaize slave
{
	TWAR=0x20; // Fill slave address to TWAR
}

void TWI_write_slave(void) // Function to write data
{
	TWDR= write_data;           // Fill TWDR register whith the data to be sent
	TWCR= (1<<TWEN)|(1<<TWINT);   // Enable TWI, Clear TWI interrupt flag
	while((TWSR & 0xF8) != 0xC0); // Wait for the acknowledgement
}

void TWI_match_write_slave(void) //Function to match the slave address and slave dirction bit(write)
{
	while((TWSR & 0xF8)!= 0xA8) // Loop till correct acknoledgement have been received
	{
		// Get acknowlegement, Enable TWI, Clear TWI interrupt flag
		TWCR=(1<<TWEA)|(1<<TWEN)|(1<<TWINT);
		while (!(TWCR & (1<<TWINT)));  // Wait for TWINT flag
	}
}

The Code for Arduino Board:

#include <Wire.h>

#define TRANCEIVER_ADDRESS 0x20

int val = 0;
void setup()
{
  Wire.begin();  // join i2c bus (address optional for master)
  Serial.begin(115200);  // start serial for output
}

void loop()
{
  Serial.println("HALLO");
  Wire.beginTransmission(TRANCEIVER_ADDRESS);
    
  while(Wire.available())    // slave may send less than requested
  { 
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();
  Serial.println(x);

  delay(500);
}

As described in the code, I use Address 0x20 to establish the connection and send the hex bit from ATmega32 to Arduino. The output i got in the SerialCommand Window in Arduino IDE is like in the image below:

That means NO CONNACTION !!!

Could someone spot the problem?

The fact is when I interface 2x ATmega32 and transmit data from slave, The master Receives the Data without problems. So the problem is in arduino that it doesn’t receive anything from ATmega32. I was also trying to measure the SDA pin(20) and SCL pin(21) on arduino board with a oscilloscope it shows constant HIGH(5Volt).

jo.PNG

Have a look at the examples for I2C.

To write something, the beginTransmission-write-endTransmission is used: http://arduino.cc/en/Reference/WireWrite
To read something, the requestFrom-available-read is used: http://arduino.cc/en/Reference/WireRead

The Wire.requestFrom is the complete finished I2C session to read from a slave. The Wire.available returns the number of received byte in the buffer.

Peter_n:
Have a look at the examples for I2C.

To write something, the beginTransmission-write-endTransmission is used: http://arduino.cc/en/Reference/WireWrite
To read something, the requestFrom-available-read is used: http://arduino.cc/en/Reference/WireRead

The Wire.requestFrom is the complete finished I2C session to read from a slave. The Wire.available returns the number of received byte in the buffer.

Hi thank you for responding smiley

I also tried to read the data from ATmega32 by using Wire.requestFrom(TRANCEIVER_ADDRESS, 6); like in the code below, but not succeed. Can you suggest what else I can do? I already used days to fix it, without success.

#include <Wire.h>

#define TRANCEIVER_ADDRESS 0x20

void setup()
{
  Wire.begin();  // join i2c bus (address optional for master)
  Serial.begin(115200);  // start serial for output
}

void loop()
{
  Serial.println("HALLO");

Wire.requestFrom(TRANCEIVER_ADDRESS, 6);
  
  while(Wire.available())    // slave may send less than requested
  { 
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();
  Serial.println(x);

  delay(500);
}

When there are no more bytes available, you still do one read. If you remove that, it is okay.
I don't understand the ATmega32 Atmel 6 library.

Peter_n:
When there are no more bytes available, you still do one read. If you remove that, it is okay.
I don't understand the ATmega32 Atmel 6 library.

So you say the code should be fine?

Yes, that extra read() returns zero or rubbish, but reading the 6 bytes is okay.

Did you know that the Wire.requestFrom() handles the complete I2C session. It returns after the I2C has completely finished. The return value is the number of bytes received.
The Wire.available() returns the same number of bytes, but that value is lowered with every Wire.read().

// alternative
int n = Wire.requestFrom( 0x20, 6);
for (int i=0; i<n; i++)
  buffer[i] = Wire.read();