Polling of TWINT bit after STOP Command in TWI Bus --- not allowed! Why?

1. After the assertion of START command, the SDA line makes a transition from High-to-Low while the SCL line is at H-state (Fig-1). After a while, the SDA lines settles at L-state and then the SCL line also settles at L-state. The following codes work well to catch the status code (0x08).

TWCR = 0b10100100;        //TWI bus enable first; TWINT bit cleared; TWSTA bit becomes active
while (bitRead(TWCR, 7) != HIGH)              //TWINT = TWCR.7
    ;
lcd.print((TWSR & 0b11111000), HEX);    //LCD shows 0x08 (expected status code).


Figure-1: Data transfer sequence on TWI Bus (extracted from Microchip data sheet)

2. After the assertion of STOP command, the SDA line makes a transition from Low-to-High while the SCL line is at H-state (Fig-1). After a while, the SDA lines settles at H-state, the SCL line remains at the previous H-state. The following codes hang up in the while() loop. Why? The intention is to see what type of status code STOP command generates.

TWCR = 0b10010100;        //TWI bus remains enabled; TWINT bit cleared; TWSTO bit becomes active
while (bitRead(TWCR, 7) != HIGH)
   ;
lcd.print((TWSR & 0b11111000), HEX);    //LCD shows ? (nothing in Atmel data sheet!)

Take a look at figure 22-10. The activation of TWINT after the start condition is defined but I cannot see the same for the stop condition. Theres also no need to set TWINT as one can check if the stop condition was completed by looking at the TWSTO bit to get cleared.

Sending a stop condition is almost like resetting the TWI subsystem, you don't need to read the TWSR register as the state is defined in the datasheet and there seems to be no exception.

Your referred figure 22-10 is found as :
24-10 in ATmega2650 data sheets
26-10 in ATmega328 data sheets

22-10 could be a typographic error.
//--------------------------------------

1. I recognize that my action of polling the TWINT bit is out of spec, because the BusEvents are not defined after the STOP command.

2. Yes! Completion of the STOP condition could be ensured by monitoring its status. The execution of the following codes have provided satisfying result:

  TWCR = 0b10010100;                        //STOP command
  while (bitRead(TWCR, 4) != LOW)       //polling the TWSTO bit
     ;
  lcd.setCursor ( 8, 1);
  lcd.print("AX");                                  //just a marker
  lcd.print((TWSR & 0b11111000), HEX); //shows: 0xF8 = 11111 000 (equal to initial value)
  lcd.print(TWCR, HEX);            //shows: 0x04 = 00000 1(TWEN) 00 (initial value = 00000000)

3.

Sending a stop condition is almost like resetting the TWI subsystem, you don't need to read the TWSR register as the state is defined in the datasheet and there seems to be no exception.

STOP command has brought the TWSR Register to its initial state (Step-2), but it has not done the same thing for the TWCR Register which still preserves (as per measurement) the active state of the TWEN bit (step-2).

Thanks a lot for the contribution that helped me in filling up my knowledge gap in TWI Bus architecture!

Your referred figure 22-10 is found as :
24-10 in ATmega2650 data sheets
26-10 in ATmega328 data sheets

22-10 could be a typographic error.

24-10 for ATmega2650 DS is correct. It's 22-10 for ATmega328p datasheets. I don't have a ATmega328 datasheet as I don't use that processor.

STOP command has brought the TWSR Register to its initial state (Step-2), but it has not the same thing for the TWCR Register which still preserves (as per measurement) the active state of the TWEN bit (step-2).

That's why I used the word "almost" :-).

1.

It's 22-10 for ATmega328p datasheets.

Just now, I have collected the DS for ATmega328P. The figure is located at: 21-10.

2.

That's why I used the word "almost" :-).

Being a non-native, I have learnt English by practicing the syntax and semantics rules of the English Language; yet, I make mistake in catching the adverb (almost). :slight_smile:

Just now, I have collected the DS for ATmega328P. The figure is located at: 21-10.

OK, seems that these datasheets are changed quite frequently. I downloaded mine about a year ago. I will not refer to figure number anymore...