Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Communication between two ATMEGA32 using I2C

Status
Not open for further replies.

suhaila38

Newbie level 2
Joined
Apr 1, 2010
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
malaysia
Activity points
1,297
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 compared 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. As what I have read, 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 real hardware...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 wanna help from the expert..please :-D

** coding for MASTER**

#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);
}

** coding for SLAVE**

// Program for Slave Mode
#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;
}
 

Since there is no reply, i managed to solve the problem by myself..

Guess what??

i think I've solved it !
data 0x05.jpg
the signals...

photo.JPG
the circuit...
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top