0

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?

AdiT
  • 101
  • 1
  • I don't think anyone can explain *you* but they might be able to explain something else. – JYelton Sep 26 '14 at 20:27
  • 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. – venny Sep 26 '14 at 20:28
  • I do have pull-up resistors connected (10kOhm). Not success. – AdiT Sep 26 '14 at 20:32
  • In the arduino code you are using 0x90 which is outside the 7-bit address space. – venny Sep 26 '14 at 20:36
  • Hmm can suggest me another Address. I have tried several without success. – AdiT Sep 26 '14 at 20:40
  • @AdiT Wire.begin with 0x48 and TWAR with 0x90 because it is shifted one bit left. – venny Sep 26 '14 at 20:42
  • @venny I can't test it yet, because of I don't have the pull-up resistors connected. When I'm able to test it, I will give response. :) – AdiT Sep 26 '14 at 20:48
  • @venny is it a MUST to have Pull-ups resistors connected, or is it optional?? – AdiT Sep 27 '14 at 19:41
  • @AdiT Yes, it is mandatory. There might be cases where it worked with just the internal pull-ups of the MCUs, but it is unreliable. I am sure it is specified in official I2C documentation. In most cases something between 1k and 5k will work fine. – venny Sep 28 '14 at 02:27

2 Answers2

1

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);
Juanjo
  • 11
  • 1
0

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.

Nasr
  • 136
  • 1