+ Post New Thread
Results 1 to 1 of 1

Thread: problem with i2c in atmega32

  1. #1
    Newbie level 3
    Points: 89, Level: 1

    Join Date
    Nov 2012
    Posts
    4
    Helped
    0 / 0
    Points
    89
    Level
    1

    problem with i2c in atmega32

    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 alexan_e; 27-11-12 at 09:06. Reason: added CODE tags

    •   Alt 

      advertising

        
       

+ Post New Thread