ok I'll post some code... look if it could be ok
Master.c
#include <avr/io.h>
#include <compat/twi.h>
#include <inttypes.h>
int TWI_Master_Transmitter(uint8_t data, uint8_t address);
void TWI_init(void);
void TWI_Master_Transmitter_Error(void);
int main(void)
{
DDRA=0xFF;
PORTA=0xFF;
TWI_init();
int a;
a=TWI_Master_Transmitter(0xAA,0xE0);
if(a!=0)
PORTA=a;
}
void TWI_init(void)
{
#define SCL_CLOCK 100000L
#define F_CPU 8000000UL
/* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */
TWSR = 0; /* no prescaler */
TWBR = ((F_CPU/SCL_CLOCK)-16)/2; /* must be > 10 for stable operation */
TWCR=(0<<TWINT|1<<TWEA|1<<TWEN);
}
int TWI_Master_Transmitter(uint8_t data,uint8_t address)
{
uint8_t twst;
PORTA=0xFF;
TWCR=((1<<TWINT)|(1<<TWSTA)|(1<<TWEN)); //Start condition
while(!(TWCR&(1<<TWINT))); // TWINT 1
twst=TWSR;
twst&=0xF8;
//Check TWSR START=0x08 REP_START=0x10
(twst!=TW_START)&&(twst!=TW_REP_START))
{
TWI_Master_Transmitter_Error();
return 1;
}
PORTA=0xFE;
TWDR=address; //Load SLA + Write (Write=0)
TWCR=((1<<TWINT)|(1<<TWEN)); //Send address
PORTA=0xFD;
while(!(TWCR&(1<<TWINT))); //TWINT1
PORTA=0xFC;
twst=TWSR;
twst&=0xF8; // Prescaler
if (twst != TW_MT_SLA_ACK) // check TWSR SLA_ACK=0x18
{
TWI_Master_Transmitter_Error();
return 2;
}
TWDR=data; //data in TWDR
TWCR=((1<<TWINT)|(1<<TWEN)); //Send data
PORTA=0x8B;
while(!(TWCR&(1<<TWINT))); // TWINT 1
PORTA=0xFA;
twst=TWSR;
twst&=0xF8; //Prescaler
if(twst!=TW_MT_DATA_ACK) //check TWSR MT_DATA:ACK 0x28
{
TWI_Master_Transmitter_Error();
return 3;
}
PORTA=0xF9;
TWCR=((1<<TWINT)|(1<<TWEN)|(1<<TWSTO)); //Stop condition
PORTA=0xF8;
return 0;
}
void TWI_Master_Transmitter_Error(void)
{
TWCR=((1<<TWINT)|(1<<TWEN)|(1<<TWSTO)); //TWI - Stop
TWCR=(0<<TWEN); //TWI-Aus
PORTA=0xF7;
}
SLAVE.c
#include <stdlib.h>
#include <stdio.h>
#include <util/twi.h>
#include <inttypes.h>
#include <avr/io.h>
unsigned char s_addr, rdata;
unsigned char tdata;
const char CDATA[]={0x01,0x80,0x02,0x40,0x04,0x20,0x08,0x10};
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x00;
//PORTC = 0b1001111;
//DDRC = 0x00;
PORTD = 0xFF;
DDRD = 0xFF;
}
//TWI initialize
// bit rate:1
void twi_init(void)
{
TWCR= 0X00; //disable twi
TWBR= 0x01; //set bit rate
TWSR= 0x00; //set prescale
TWAR= 0xE0; //set slave address //0x01
TWCR= 0x04; //enable twi
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
//CLI(); //disable all interrupts
port_init();
twi_init();
}
void ERROR(unsigned char edata)
{
PORTD= ~edata;
while(1);
}
unsigned char SlaveReceive(void)
{
TWCR = (1 << TWINT) | (1 << TWEN);
// TWAMR = 0x00;
TWAR = ((s_addr <<1) | 0);
TWCR = (1 << TWEA) | (1 << TWEN);
while (!(TWCR & (1 << TWINT)));
if ((TWSR & 0xF8) != 0x60)
ERROR(3);
TWCR = (1 << TWINT) | (1 << TWEA) | (1 << TWEN);
while (!(TWCR & (1 << TWINT)));
if ((TWSR & 0xF8) != 0x080)
ERROR(4);
rdata = TWDR;
TWCR = (1 << TWINT) | (1 << TWEN);
while (!(TWCR & (1 << TWINT)));
if ((TWSR & 0xF8) != 0x0A0)
ERROR(5);
TWCR = (1 << TWINT) | (1 << TWEN);
return rdata;
}
void SlaveTransmit(void)
{
TWCR = (1 << TWINT) | (1 << TWEN);
// TWAMR = 0xFE;
TWAR = ((s_addr <<1) | 0);
TWCR = (1 << TWEA) | (1 << TWEN);
while (!(TWCR & (1 << TWINT)));
if ((TWSR & 0xF8) != 0xA8)
ERROR(0x30);
TWDR = tdata;
TWCR = (1 << TWINT) | (1 << TWEA) | (1 << TWEN);
while (!(TWCR & (1 << TWINT)));
if ((TWSR & 0xF8) != 0x0C0)
ERROR(0x40);
}
void main(void)
{
init_devices();
s_addr = 0xE0; //0x50
PORTD =0xAA;
while(1)
{
rdata = SlaveReceive();
tdata = CDATA[rdata];
SlaveTransmit();
PORTD = ~rdata;
}
}