25.6.2 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 25-11. Data Transfer in Master Transmitter Mode

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

  • The TWI Enable bit (TWCR.TWEN) must be written to '1' to enable the 2-wire Serial Interface
  • The TWI Start Condition bit (TWCR.TWSTA) must be written to '1' to transmit a START condition
  • The TWI Interrupt Flag (TWCR.TWINT) must be written to '1' to clear the flag.
The TWI 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 TWSR 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 (TWDR). Thereafter, the TWCR.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 TWCR Register. After updating TWDR, the TWINT bit should be cleared (by writing '1' to it) to continue the transfer. This is accomplished by writing again a value to TWCR of the type TWCR=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 TWCR=1x10x10x. A STOP condition is generated by writing a value of the type TWCR=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 25-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 TWCR
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 or

Load SLA+R

0

0

0

0

1

1

X

X

SLA+W will be transmitted;

ACK or NOT ACK will be received

SLA+R will be transmitted;

Logic will switch to Master Receiver mode

0x18SLA+W has been transmitted;

ACK has been received

Load data byte or

No TWDR action or

No TWDR action or

No TWDR action

0

1

0

1

0

0

1

1

1

1

1

1

X

X

X

X

Data byte will be transmitted and ACK or NOT ACK will be received

Repeated START will be transmitted

STOP condition will be transmitted and

TWSTO Flag will be reset

STOP 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 or

No TWDR action or

No TWDR action or

No TWDR action

0

1

0

1

0

0

1

1

1

1

1

1

X

X

X

X

Data byte will be transmitted and ACK or NOT ACK will be received

Repeated START will be transmitted

STOP condition will be transmitted and

TWSTO Flag will be reset

STOP 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 or

No TWDR action or

No TWDR action or

No TWDR action

0

1

0

1

0

0

1

1

1

1

1

1

X

X

X

X

Data byte will be transmitted and ACK or NOT ACK will be received

Repeated START will be transmitted

STOP condition will be transmitted and

TWSTO Flag will be reset

STOP 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 or

No TWDR action or

No TWDR action or

No TWDR action

0

1

0

1

0

0

1

1

1

1

1

1

X

X

X

X

Data byte will be transmitted and ACK or NOT ACK will be received

Repeated START will be transmitted

STOP condition will be transmitted and

TWSTO Flag will be reset

STOP 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 or

No TWDR action

0

1

0

0

1

1

X

X

2-wire Serial Bus will be released and not addressed Slave mode entered

A START condition will be transmitted when the bus becomes free

Figure 25-12. Formats and States in the Master Transmitter Mode