0
\$\begingroup\$

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:

Image

The Code for ATmega32 in Atmel 6:

#include <avr/io.h>
#include "I2C_slave.h"
void i2c_initSlave(unsigned char slaveAddress)
 {
 TWCR = 0x04;
 TWAR = slaveAddress;
 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
 }
 //*************************************************
 void i2c_send(unsigned char data)
 {
 TWDR = data;
 TWCR = (1<<TWINT) | (1<<TWEN);
 while ((TWCR & (1<<TWINT))== 0);
 }
 //*************************************************
 void i2c_listen()
 {
 while ((TWCR & (1<<TWINT)) == 0);
 }
int main(void)
{
 int PIN = 0x02;
 DDRC &= ~PIN; 
 i2c_initSlave(0x90);
 i2c_listen();
 i2c_send("G");
 while(1)
 {
 return 0; 
 }
}

The Code for Arduino Board:

#include <Wire.h>
#define TRANCEIVER_ADDRESS 0x90
void setup()
{
 Wire.begin(TRANCEIVER_ADDRESS); // join i2c bus (address optional for master)
 Serial.begin(115200); // start serial for output
}
void loop()
{
 Serial.println("HALLO");
 Wire.requestFrom(TRANCEIVER_ADDRESS, 2);
 while(Wire.available()) // slave may send less than requested
 { 
 char c = Wire.read(); // receive a byte as character
 Serial.print(c); // print the character
 }
 delay(500);
}

As described in the code, I use Address 0x90 to establish the connection and send the character "G" from ATmega32 to Arduino. The output i got in the SerialCommand Window in Arduino IDE is like in the image below:
enter image description here

That means NO CONNACTION !!!

Could someone spot the problem? I think the problem is in the ATmega32 code.

Is the DDRC and PORTC configured correct to the MASTER READ?

DDRC=0xFF;
PORTC=0x00;

I'am not sure. Or should the PORTC be

PORTC=0xFF;

Could someone explain me?

asked Sep 26, 2014 at 16:55
\$\endgroup\$
9
  • \$\begingroup\$ I don't think anyone can explain you but they might be able to explain something else. \$\endgroup\$ Commented Sep 26, 2014 at 20:27
  • \$\begingroup\$ Do you have pull-up resistors connected? And setting GPIO pin modes is unnecessary because peripherals override it with their own setting. Also you should try to isolate the problem by first checking if the slave address is acknowledged. \$\endgroup\$ Commented Sep 26, 2014 at 20:28
  • \$\begingroup\$ I do have pull-up resistors connected (10kOhm). Not success. \$\endgroup\$ Commented Sep 26, 2014 at 20:32
  • \$\begingroup\$ In the arduino code you are using 0x90 which is outside the 7-bit address space. \$\endgroup\$ Commented Sep 26, 2014 at 20:36
  • \$\begingroup\$ Hmm can suggest me another Address. I have tried several without success. \$\endgroup\$ Commented Sep 26, 2014 at 20:40

2 Answers 2

1
\$\begingroup\$

In PIC code as slave I2C, use:

i2c(Slave, sda=PIN_C4, scl=PIN_C3, address=0xa0, FORCE_HW, FAST);

In Arduino code as master I2C, use:

const int SLAVE_ADDRESS2 = 0xA0; // esclavo PIC
Wire.beginTransmission(SLAVE_ADDRESS2 >> 1);
answered Jan 7, 2015 at 22:13
\$\endgroup\$
0
\$\begingroup\$

Here are some errors i noticed in your code:

1) Slave address is 7 bit only (from 0x00 to 0x7F) so 0x90 is out of range, also it placed form b1 ~ b7 in TWAR Register so slave address should be shifted by 1.

void i2c_initSlave(unsigned char slaveAddress)
{
 TWCR = 0x04;
 TWAR = (slaveAddress << 1); // It shoule be shifted by 1
 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
}

2) In i2c_send("G") you are sending pointer while function void i2c_send(unsigned char data) is taking unsigned char argument, fix this by calling it like i2c_send('G');

3) You place i2c_send("G") outside the while loop so it will only be transmitted once.

answered Sep 2, 2015 at 12:05
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.