void i2cinit(void)
{
TWSR=0x00; //set prescaler bits to zero
TWBR=0x128; // SCL frequency is 60lhz for Xtal= 16M
}
uint8_t i2cstart( uint8_t address)
{ TWSR=0x00;
//transmission start condition
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
//wait for end of transmission
while (!(TWCR & (1<<TWINT))==0);
//check if the start condition was successfully transmitted
bit1(TWCR);
if ((TWSR & 0xF8)!= 0x08)
return 1;
//load slave address into data register
TWDR=address;
//start transmission of address
TWCR= (1<<TWINT)|(1<<TWEN);
//wait for end of transmission
while(!(TWCR&(1<<TWINT))) ;
// check if the device has acknowledged the READ / WRITE mode
uint8_t twst = TWCR & 0xF8;
if ( (twst != 0x18) && (twst != 0x40) ) return 2;
return 0;
}
this is my start function for the i2c master configuration.
Now this function is returning 1 and when i check the TWSR value it is printing 11111000.
and i am not getting what is wrong in this???
also one dout I am confused on one part
To start I2C transmission i have to SET(1) TWINT bit of TWCR register to CLEAR the flag .
And after completion of the event - TWINT flag is SET that is TWINT bit is cleared(0) by the application hardware and TWSR register is updated with the suitable value
So we monitor the TWINT bit to see whether event is completed or not and TWSR register to see which event is done.
Now confusion is on this statement -
while (!(TWCR & (1<<TWINT))); - Now when TWINT (this is 7th bit in TWCR register) bit is cleared by hardware on completion of event my TWCR register will be - 0*******
So -TWCR & (1<<TWINT) = 00000000 ........ and therefore ( !(TWCR & (1<<TWINT))) - gives 1. So in this case my while loop becomes true and it will be inside the while loop for all the time . Which should not be the case because as on completion of event TWINT bit is cleared indicates event has occoured so it should come out from while loop. So rather then this statement should it be not
while ((TWCR & (1<<TWINT))); ... now it will come out of the loop only when my TWINT becomes 0 ...for rest of the time till it is TWINT i s 1 ( not ceared by hardware ) it will remain inside the loop waiting for hardware response.