Datasheet
must be transmitted. This is done by writing SLA+W to the TWI Data Register (TWDRn). Thereafter, the
TWCRn.TWINT flag should be cleared (by writing a '1' to it) to continue the transfer. This is accomplished
by writing a value to TWRC of the type TWCR=1x00x10x.
When SLA+W has been transmitted and an acknowledgment bit has been received, TWINT is set again
and a number of status codes in TWSR are possible. Possible status codes in Master mode are 0x18,
0x20, or 0x38. The appropriate action to be taken for each of these status codes is detailed in the status
code table below.
When SLA+W has been successfully transmitted, a data packet should be transmitted. This is done by
writing the data byte to TWDR. TWDR must only be written when TWINT is high. If not, the access will be
discarded, and the Write Collision bit (TWWC) will be set in the TWCRn register. After updating TWDRn,
the TWINT bit should be cleared (by writing '1' to it) to continue the transfer. This is accomplished by
writing again a value to TWCRn of the type TWCRn=1x00x10x.
This scheme is repeated until the last byte has been sent and the transfer is ended, either by generating
a STOP condition or a by a repeated START condition. A repeated START condition is accomplished by
writing a regular START value TWCRn=1x10x10x. A STOP condition is generated by writing a value of
the type TWCRn=1x01x10x.
After a repeated START condition (status code 0x10), the two-wire serial interface can access the same
slave again, or a new slave without transmitting a STOP condition. Repeated START enables the master
to switch between slaves, Master Transmitter mode, and Master Receiver mode without losing control of
the bus.
Table 26-3. Status Codes for Master Transmitter Mode
Status Code
(TWSR)
Prescaler
Bits are 0
Status of the Two-Wire
Serial Bus and Two-Wire
Serial Interface Hardware
Application Software Response Next Action Taken by TWI
Hardware
To/From
TWDR
To TWCRn
STA STO TWINT TWEA
0x08 A START condition has been
transmitted
Load SLA+W 0 0 1 X SLA+W will be transmitted;
ACK or NOT ACK will be
received
0x10 A repeated START condition
has been transmitted
Load SLA+W
or
0 0 1 X SLA+W will be transmitted;
ACK or NOT ACK will be
received
Load SLA+R 0 0 1 X SLA+R will be transmitted;
Logic will switch to Master
Receiver mode
0x18 SLA+W has been transmitted;
ACK has been received
Load data
byte or
0 0 1 X Data byte will be transmitted
and ACK or NOT ACK will be
received
No TWDR
action or
1 0 1 X Repeated START will be
transmitted
No TWDR
action or
0 1 1 X STOP condition will be
transmitted and TWSTO Flag
will be reset
No TWDR
action
1 1 1 X STOP condition followed by a
START condition will be
ATmega48PA/88PA/168PA
Two-Wire Serial Interface (TWI)
© 2018 Microchip Technology Inc.
Datasheet Complete
DS40002011A-page 284