27.7.1 Master Transmitter Mode

In the Master Transmitter (MT) mode, a number of data bytes are transmitted to a Slave Receiver, see figure below. In order to enter a Master mode, a START condition must be transmitted. The format of the following address packet determines whether MT or Master Receiver (MR) mode is to be entered: If SLA+W is transmitted, MT mode is entered, if SLA+R is transmitted, MR mode is entered. All the status codes mentioned in this section assume that the prescaler bits are zero or are masked to zero.

Figure 27-11. Data Transfer in Master Transmitter Mode

A START condition is sent by writing a value to the TWI Control Register n (TWCRn) of the type TWCRn=1x10x10x:

  • The TWI Enable bit (TWCRn.TWEN) must be written to '1' to enable the 2-wire Serial Interface
  • The TWI Start Condition bit (TWCRn.TWSTA) must be written to '1' to transmit a START condition
  • The TWI Interrupt Flag (TWCRn.TWINT) must be written to '1' to clear the flag.
The TWI n will then test the 2-wire Serial Bus and generate a START condition as soon as the bus becomes free. After a START condition has been transmitted, the TWINT Flag is set by hardware, and the status code in TWSRn will be 0x08 (see Status Code table below). In order to enter MT mode, SLA+W 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 have 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 2-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 27-3. Status Codes for Master Transmitter Mode
Status Code

(TWSR)

Prescaler Bits are 0

Status of the 2-wire Serial Bus and 2-wire Serial Interface HardwareApplication Software ResponseNext Action Taken by TWI Hardware
To/from TWDRTo TWCRn
STASTOTWINTTWEA
0x08A START condition has been transmittedLoad SLA+W001XSLA+W will be transmitted;

ACK or NOT ACK will be received

0x10A repeated START condition has been transmittedLoad SLA+W or001XSLA+W will be transmitted;

ACK or NOT ACK will be received

Load SLA+R001XSLA+R will be transmitted;

Logic will switch to Master Receiver mode

0x18SLA+W has been transmitted;

ACK has been received

Load data byte or001XData byte will be transmitted and ACK or NOT ACK will be received
No TWDR action or101XRepeated START will be transmitted
No TWDR action or011XSTOP condition will be transmitted and TWSTO Flag will be reset
No TWDR action111XSTOP condition followed by a START condition will be transmitted and TWSTO Flag will be reset
0x20SLA+W has been transmitted;

NOT ACK has been received

Load data byte or001XData byte will be transmitted and ACK or NOT ACK will be received
No TWDR action or101XRepeated START will be transmitted
No TWDR action or011XSTOP condition will be transmitted and TWSTO Flag will be reset
No TWDR action111XSTOP condition followed by a START condition will be transmitted and TWSTO Flag will be reset
0x28Data byte has been transmitted;

ACK has been received

Load data byte or001XData byte will be transmitted and ACK or NOT ACK will be received
No TWDR action or101XRepeated START will be transmitted
No TWDR action or011XSTOP condition will be transmitted and TWSTO Flag will be reset
No TWDR action111XSTOP condition followed by a START condition will be transmitted and TWSTO Flag will be reset
0x30Data byte has been transmitted;

NOT ACK has been received

Load data byte or001XData byte will be transmitted and ACK or NOT ACK will be received
No TWDR action or101XRepeated START will be transmitted
No TWDR action or011XSTOP condition will be transmitted and TWSTO Flag will be reset
No TWDR action111XSTOP condition followed by a START condition will be transmitted and TWSTO Flag will be reset
0x38Arbitration lost in SLA+W or data bytesNo TWDR action or001X2-wire Serial Bus will be released and not addressed Slave mode entered
No TWDR action101XA START condition will be transmitted when the bus becomes free
Figure 27-12. Formats and States in the Master Transmitter Mode