Datasheet
is updated with a status code identifying the event. The TWSRn only contains relevant status information
when the TWI interrupt flag is asserted. At all other times, the TWSRn contains a special status code
indicating that no relevant status information is available. As long as the TWINT flag is set, the SCL line is
held low. This allows the application software to complete its tasks before allowing the TWI transmission
to continue.
The TWINT flag is set in the following situations:
• After the TWI has transmitted a START/REPEATED START condition
• After the TWI has transmitted SLA+R/W
• After the TWI has transmitted an address byte
• After the TWI has lost arbitration
• After the TWI has been addressed by own slave address or general call
• After the TWI has received a data byte
• After a STOP or REPEATED START has been received while still addressed as a slave
• When a bus error has occurred due to an illegal START or STOP condition
26.6 Using the TWI
The AVR TWI is byte-oriented and interrupt based. Interrupts are issued after all bus events, like
reception of a byte or transmission of a START condition. Because the TWI is interrupt-based, the
application software is free to carry on other operations during a TWI byte transfer. Note that the TWI
Interrupt Enable (TWIE) bit in TWCRn together with the Global Interrupt Enable bit in SREG allows the
application to decide whether or not an assertion of the TWINT flag should generate an interrupt request.
If the TWIE bit is cleared, the application must poll the TWINT flag in order to detect actions on the TWI
bus.
When the TWINT flag is asserted, the TWI has finished an operation and awaits application response. In
this case, the TWI Status Register (TWSRn) contains a value indicating the current state of the TWI bus.
The application software can then decide how the TWI should behave in the next TWI bus cycle by
manipulating the TWCRn and TWDRn registers.
The following figure illustrates a simple example of how the application can interface to the TWI
hardware. In this example, a master wishes to transmit a single data byte to a slave. A more detailed
explanation follows later in this section. Simple code examples are presented in the table below.
ATmega48PA/88PA/168PA
Two-Wire Serial Interface (TWI)
© 2018 Microchip Technology Inc.
Datasheet Complete
DS40002011A-page 279