I2C between two ATMEGA32
Posted: Sun Jul 08, 2012 4:28 pm
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...
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..
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?
=Code for master=
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...
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..
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?
=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;
}