I'm trying TWI for the first time and I run into a problem that got me stuck. The following piece of code supposed to initialize TWI, send the address of the slave and then send another piece of data to the slave before closing. I have a blinking led that tells me that the function was run. I added the commented section in the middle to check for error and exit, then the function executed and got the blinking led. Without the commented section it hangs at the third while. I put this in a loop so I can see what was happening at the communication lines,
void eraseDisplay(void)
{
TWSR = 0x00;
TWBR = 0x48;
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN)|(1<<TWIE); //Starting TWI communication
while(!(TWCR & (1<<TWINT)));
TWDR = LCDA ; // Write the device address to the bus
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWIE);
while(!(TWCR & (1<<TWINT)));
// if (((TWSR & 0xF8) != TW_START) && ((TWSR & 0xF8)!=TW_REP_START))
// {
// TWCR=(1<<TWINT)|(1<<TWSTO)|(1<<TWEN)|(1<<TWIE);
// return;
// }
TWDR = 0x1F; // Write the data to be transmitted
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWIE);
while(!(TWCR & (1<<TWINT)));
TWCR=(1<<TWINT)|(1<<TWSTO)|(1<<TWEN)|(1<<TWIE);
setCursor(0,0);
}
On the scope I can see the SDA line coming down from high and I can see the SCL line with the clock at about 100Khz. The code on the SDA corresponds to the slave address + write. Then the two lines get pulled down for several mS, then the address is repeated (I think) before the TWSTO.
I used a I2C scanner and that reported the address of the device at 0X27. Which is the address that I'm using here. By looking at DSC_1191, looks to me that the device is not ACK, therefore the buffer for the data is not empty for new data, reason why it hangs.
Is that correct? How I troubleshoot this problem.
Thanks guys.