Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

problem with i2c in atmega32

Status
Not open for further replies.

sefat

Newbie level 4
Joined
Nov 17, 2012
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,317
i'm using two atmega32 for i2c.
master will call a slave & send the address of the slave as data & the slave will show it on PORTA.
Master is transmitter and slave is receiver.
i'm not getting expected result.slave is not showing its address on PORTA.
where's the problem. plzz help.

master code:


Code:
#include <avr/io.h>
#include <util/delay.h>
void i2c_write(unsigned char data){
	TWDR=data;
	TWCR=(1<<TWINT)|(1<<TWEN);
	while((TWCR&(1<<TWINT))==0);
	}
	
void i2c_start(){
	TWCR=(1<<TWINT)|(TWSTA)|(TWEN);
	while((TWCR&(1<<TWINT))==0);	
}

void i2c_stop(void){
	TWCR=(1<<TWINT)|(TWEN)|(TWSTO);
	}
	void i2c_init(void){
		TWSR=0x00;
		TWBR=0x47;
		TWCR=0x04;
		}
int main(void)
{i2c_init();
	_delay_ms(1000);
	i2c_start();
	i2c_write(0x11);
	i2c_write(0x11);
	i2c_stop();

	
    while(1)
    {
    }
}



slave code:



Code:
#include <avr/io.h>
void i2c_initslave(unsigned char address){
	TWCR=0x04;
	TWAR=address;
	TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);
	
}
unsigned char i2c_receive(unsigned char islest){
	if(islest==0)TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);
	else TWCR=(1<<TWINT)|(1<<TWEN);
	while((TWCR&(1<<TWINT))==0);
	return(TWDR);
	}
	
	void i2c_listen(){
		while((TWCR&(1<<TWINT))==0);
	}
	int main(void)
{  
	DDRA=0xFF;
	i2c_initslave(0x11);
	i2c_listen();
	PORTA=i2c_receive(1);
   while(1)
    {
    }
return 0;
}
 
Last edited by a moderator:

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top