I'm trying to get connect with an IC using I2C, to initialize the I2C communication I'm using the following function :
void init_i2c(void ) {
uint8_t twst;
TWSR = 0; // no prescaler
TWBR = ((F_CPU/SCL_CLOCK)-16)/2;
TWCR = (1<<(TWINT))|(1<<TWSTA )|(1<<TWEN);
printf("TWCR 0x%x \n",TWCR);
while(!(TWCR & (1<< TWINT)));
printf(" Start condition has been transmitted \n");
if( (TWSR&0xF8) != TW_START){
printf(" Error at TWSR start 0x%x\n",TWSR);
}
// Setting Address
TWDR = 0x70;
// cleating TWCR
TWCR = (1<<TWINT) |(1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != TW_MT_SLA_ACK){
printf(" Error at TWSR 0x%x SLA_ACK 0x%x \n", TWSR, TW_MT_SLA_ACK); // here is the problem !!!!!!!!! TWSR value should be 0x18
}else {
printf(" tell now is good \n");
}
the wiring is good I've measured every pin using an oscilloscope, including the frequency of the SCL[50Khz]. the TWI status register ** at the final if statement is **0x20 instead of 0x18
Any hint what I'm doing wrong here?