Page 1 of 1

I2C between two ATMEGA32

PostPosted: Sun Jul 08, 2012 4:28 pm
by atmegalover
Hi all~

For the first stage, I have done I2C communication between two ATMEGA32.. I just simulated it using Proteus. Generally, I want to familiarize with I2C where MASTER will send the data "0x05" to the slave (address 0x20). The slave will compare the address that has been sent by the MASTER to the bus.If the address is equal, then it will show the same data (0x05) at PORT A by illuminates the LED.This is my circuit in Proteus...
i2c_atmega32_proteus.png


SDA and SCL produce the correct output at the oscilloscope but at the wrong time interval since the data will be changed only when SCL= 0..
sda_scl_signals_atmega32.png


For the second stage, I've applied the same cct and the same coding onto the hardware part...Unfortunately, I didn't get any output at PORT A same as I got it in Proteus..the signal for SDA and SCL also didn't appeared.

I can't find any solution on website for my problem...This is my simple coding for this I2C..Maybe somebody can help me regarding this problem? :geek:

=Code for master=
CODE: SELECT_ALL_CODE
#include<avr/io.h>

void init_master(void);
void i2c_start(void);
void i2c_write_address(unsigned char data);
void i2c_writedata(unsigned char data);
void i2c_stop(void);

unsigned char address = 0x20, read = 1, write = 0;
unsigned char write_data = 0x05;


int main(void)
{
init_master();

while(1){
i2c_start();
i2c_write_address(address+write);
i2c_writedata(write_data);
i2c_stop();

}
}

void init_master(void)
{
TWSR = 0x00;
TWBR = 0x0C;
}

void i2c_start(void)
{
TWCR= (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while (!(TWCR & (1<<TWINT)));
}

void i2c_write_address(unsigned char data)
{
TWDR = data;
TWCR = (1<<TWINT)|(1<<TWEN);
while (!(TWCR & (1<<TWINT)));
}

void i2c_writedata(unsigned char data)
{
TWDR = data;
TWCR = (1<<TWINT)|(1<<TWEN);
while (!(TWCR & (1<<TWINT)));
}

void i2c_stop(void)
{
TWCR=(1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
}

CODE: SELECT_ALL_CODE

=Code for slave=
#include<avr/io.h>
#include<util/delay.h>

void init_slave(void);
void slave_listen(void);
void read_data(void);


unsigned char recv_data;

int main(void)
{

DDRA=0xFF;

while(1)
{
init_slave();
slave_listen();
read_data();
}
}

void init_slave(void)
{
TWAR=0x20;
}

void slave_listen(void)
{
TWCR=(1<<TWINT)|(1<<TWEA)|(1<<TWEN);
while (!(TWCR & (1<<TWINT)));
}

void read_data(void)
{
TWCR=(1<<TWINT)|(1<<TWEA)|(1<<TWEN);
while (!(TWCR & (1<<TWINT)));
recv_data=TWDR;
PORTA=recv_data;
return TWDR;
}

Re: I2C between two ATMEGA32

PostPosted: Tue Jul 10, 2012 9:55 pm
by zhenning
Hi,

Did you check your fuses settings so that they are set correctly? As far as I know, proteus will not check it.

Re: I2C between two ATMEGA32

PostPosted: Thu Jul 12, 2012 11:11 am
by atmegalover
Thanks zhenning for your ideas...Your ideas made me gone through the datasheet again..lol

I've solved the issue :)
i2c.jpg

Re: I2C between two ATMEGA32

PostPosted: Thu Jul 12, 2012 12:25 pm
by ober
Nice work!