Datasheet
Figure 26-10. Interfacing the Application to the TWI in a Typical Transmission
START SLA+W A Data A STOP
1. Application
writes to TWCR to
initiate
transmission of
START
2.TWINT set.
Status code indicates
START condition sent
4.TWINT set.
Status code indicates
SLA+W sent, ACK
received
6.TWINT set.
Status code indicates
data sent, ACK received
3. Check TWSR to see if START was
sent. Application loads SLA+W into
TWDR, and loads appropriate control
signals into TWCR, making sure that
TWINT is written to one,
and TWSTA is written to zero.
5. CheckTWSR to see if SLA+W was
sent and ACK received.
Application loads data into TWDR, and
loads appropriate control signals into
TWCR, making sure that TWINT is
written to one
7. CheckTWSR to see if data was sent
and ACK received.
Application loads appropriate control
signals to send STOP into TWCR,
making sure that TWINT is written to one
TWI bus
Indicates
TWINT set
Application
Action
TWI
Hardware
Action
1. The first step in a TWI transmission is to transmit a START condition. This is done by writing a
specific value into TWCRn, instructing the TWI n hardware to transmit a START condition. Which
value to write is described later on. However, it is important that the TWINT bit is set to the value
written. Writing a one to TWINT clears the flag. The TWI n will not start any operation as long as
the TWINT bit in TWCRn is set. Immediately after the application has cleared TWINT, the TWI n will
initiate transmission of the START condition.
2. When the START condition has been transmitted, the TWINT flag in TWCRn is set, and TWSRn is
updated with a status code indicating that the START condition has successfully been sent.
3. The application software should now examine the value of TWSRn to make sure that the START
condition was successfully transmitted. If TWSRn indicates otherwise, the application software
might take some special action, like calling an error routine. Assuming that the status code is as
expected, the application must load SLA+W into TWDR. Remember that TWDRn is used both for
address and data. After TWDRn has been loaded with the desired SLA+W, a specific value must
be written to TWCRn, instructing the TWIn hardware to transmit the SLA+W present in TWDRn.
Which value to write is described later on. However, it is important that the TWINT bit is set to the
value written. Writing a one to TWINT clears the flag. The TWI will not start any operation as long
as the TWINT bit in TWCRn is set. Immediately after the application has cleared TWINT, the TWI
will initiate transmission of the address packet.
4. When the address packet has been transmitted, the TWINT flag in TWCRn is set, and TWSRn is
updated with a status code indicating that the address packet has successfully been sent. The
status code will also reflect whether a slave acknowledged the packet or not.
5. The application software should now examine the value of TWSRn, to make sure that the address
packet was successfully transmitted, and that the value of the ACK bit was as expected. If TWSRn
indicates otherwise, the application software might take some special action, like calling an error
routine. Assuming that the status code is as expected, the application must load a data packet into
TWDRn. Subsequently, a specific value must be written to TWCRn, instructing the TWI n hardware
to transmit the data packet present in TWDRn. Which value to write is described later on. However,
it is important that the TWINT bit is set to the value written. Writing a one to TWINT clears the flag.
The TWI n will not start any operation as long as the TWINT bit in TWCRn is set. Immediately after
the application has cleared TWINT, the TWI will initiate transmission of the data packet.
ATmega48PA/88PA/168PA
Two-Wire Serial Interface (TWI)
© 2018 Microchip Technology Inc.
Datasheet Complete
DS40002011A-page 280