I am learning how to use TWI (I²C) by reading the documentation and tutorials on the Internet. I have a little problem about how the TWINT flag in the TWSR register is used.
Let's talk about the basics : send a START condition. To do it, as the documentation says:
TWEN must be set to enable the 2-wire Serial Interface, TWSTA must be written to one to transmit a START
condition and TWINT must be written to one to clear the TWINT Flag.
Ok. So I write those 3 bits to one : TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
My TWSR register is now : 10100100b (the MSB is TWINT).
What I understand is that we clear the TWINT Flag by setting it to 1. So TWINT is a 1.
Then, we need to wait that the START condition has been sent. For that, as it is write in the documentation :
After a START condition has been transmitted, the TWINT Flag is set by hardware.
OK, so I need to wait for the TWINT Flag to be set, so equal to 0.
To do that, what I would do would be to code a while condition like it: while(TWINT FLAG is equal to 1);
For me, I write it like this : while(TWSR & (1<<TWINT));
BUT, EVERYWERE ON THE INTERNET I FIND : while(!(TWSR & (1<<TWINT)));
Even on the AVR documentation.
I am on it for hours, maybe that I am missing the obvious... Why does everybody check the opposite?
Thank you. :)