Author Topic: TWI communication problem  (Read 1518 times)

0 Members and 1 Guest are viewing this topic.

Offline dan3460Topic starter

  • Frequent Contributor
  • **
  • Posts: 326
  • Country: us
TWI communication problem
« on: December 28, 2016, 03:08:41 am »
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,
Code: [Select]
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.

 

Offline dan3460Topic starter

  • Frequent Contributor
  • **
  • Posts: 326
  • Country: us
Re: TWI communication problem
« Reply #1 on: December 29, 2016, 12:12:24 pm »
In case anyone follows this post. I solved the problem and it was a software problem. I had commented out other part of the software but I missed one statement that was messing up the sequence of calls to TWI, nothing to do with hardware.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf