void iniTWI(void)
{
TWBR = 0x08; /* 0x06 with 11.0592MHz genertes 395KHz with TWPS1:0 =0
0x08 with 11.0592MHz genertes 345KHz with TWPS1:0 =0 */
// R/W6543210 8th bit is the read(1) or write(0)
// TWAR = 0b01101000; // This is used in case Uc in slave mode LSB A0 is grounded =0
TWDR = 0x00; // TWI data register
TWSR = 0x00;
// 76543210
TWCR = 0b00000100; // Control register
// TWCR = (1<<TWEN);
}//void iniTWI(void)
void TWI_stop(void)
{
TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
// TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO); // Sending Stop bit
_delay_ms(1);
}//void TWI_stop(void)
void TWI_start(void)
{
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); // Sending Start bit
while (!(TWCR & (1<<TWINT)));
// TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
// while ((TWCR & (1<<TWINT)) == 0);
}//void TWI_start(void)
void TWI_write(unsigned char TWI_data)
{
TWDR = TWI_data;
TWCR = (1<<TWINT) | (1<<TWEN); // Sending Start bit
while (!(TWCR & (1<<TWINT)));
// TWCR = (1<<TWINT)|(1<<TWEN);
// while ((TWCR & (1<<TWINT)) == 0);
}//void TWI_start(void)
// Read without Ack
unsigned char TWI_readNACK(void)
{
TWCR = (1<<TWINT)|(1<<TWEN);
// while ((TWCR & (1<<TWINT)) == 0);
while (!(TWCR & (1<<TWINT)));
return TWDR;
}//unsigned char TWI_read(void)
// Read with Ack
unsigned char TWI_readACK(void)
{
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA);
while ((TWCR & (1<<TWINT)) == 0);
return TWDR;
}//unsigned char TWI_readACK(void)
unsigned char TWIGetStatus(void)
{
unsigned char TWI_status = 0x00;
//mask status the prescalar bits
TWI_status = TWSR & 0xF8;
return TWI_status;
}