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).