Datasheet

A frame starts with the least or most significant data bit. Then the next data bits, up to a total of eight, are
succeeding, ending with the most or least significant bit accordingly. When a complete frame is
transmitted, a new frame can directly follow it, or the communication line can be set to an idle (high) state.
The UDORDn bit in UCSRnC sets the frame format used by the USART in MSPIM mode. The receiver
and transmitter use the same setting. Note that changing the setting of any of these bits will corrupt all
ongoing communication for both the receiver and transmitter.
16-bit data transfer can be achieved by writing two data bytes to UDRn. A UART transmit complete
interrupt will then signal that the 16-bit value has been shifted out.
25.5.1 USART MSPIM Initialization
The USART in MSPIM mode has to be initialized before any communication can take place. The
initialization process normally consists of setting the baud rate, setting Master mode of operation (by
setting DDR_XCKn to one), setting frame format and enabling the transmitter and the receiver. Only the
transmitter can operate independently. For interrupt driven USART operation, the global interrupt flag
should be cleared (and thus interrupts globally disabled) when doing the initialization.
Note:  To ensure immediate initialization of the XCKn output the Baud-Rate Register (UBRRn) must be
zero at the time the transmitter is enabled. Contrary to the normal mode USART operation the UBRRn
must then be written to the desired value after the transmitter is enabled, but before the first transmission
is started. Setting UBRRn to zero before enabling the transmitter is not necessary if the initialization is
done immediately after a Reset since UBRRn is reset to zero.
Before doing a re-initialization with changed baud rate, Data mode, or frame format, be sure that there
are no ongoing transmissions during the period the registers are changed. The TXCn flag can be used to
check that the transmitter has completed all transfers, and the RXCn flag can be used to check that there
are no unread data in the receive buffer. Note that the TXCn flag must be cleared before each
transmission (before UDRn is written) if it is used for this purpose.
The following simple USART initialization code examples show one assembly and one C function that are
equal in functionality. The examples assume polling (no interrupts enabled). The baud rate is given as a
function parameter. For the assembly code, the baud rate parameter is assumed to be stored in the
r17:r16 registers.
Example 25-1. Assembly Code Example
clr r18
out UBRRnH,r18
out UBRRnL,r18
; Setting the XCKn port pin as output, enables master mode.
sbi XCKn_DDR, XCKn
; Set MSPI mode of operation and SPI data mode 0.
ldi r18, (1<<UMSELn1)|(1<<UMSELn0)|(0<<UCPHAn)|(0<<UCPOLn)
out UCSRnC,r18
; Enable receiver and transmitter.
ldi r18, (1<<RXENn)|(1<<TXENn)
out UCSRnB,r18
; Set baud rate.
; IMPORTANT: The Baud Rate must be set after the transmitter is enabled!
out UBRRnH, r17
out UBRRnL, r18
ret
Example 25-2. C Code Example
{
UBRRn = 0;
ATmega48PA/88PA/168PA
USART in SPI (USARTSPI) Mode
© 2018 Microchip Technology Inc.
Datasheet Complete
DS40002011A-page 267