Datasheet

192
ATtiny828 [DATASHEET]
8371A–AVR–08/12
16-bit data transfer can be achieved by writing two data bytes to UDR. A USART Transmit Complete interrupt will then
signal that the 16-bit value has been shifted out.
18.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, 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 XCK output the baud-rate register (UBRR) must be zero at the time the
transmitter is enabled. Contrary to the normal mode USART operation the UBRR must then be written to the
desired value after the transmitter is enabled, but before the first transmission is started. Setting UBRR to zero
before enabling the transmitter is not necessary if the initialization is done immediately after a reset since UBRR
is reset to zero.
Before doing a re-initialization with changed baud rate, data mode, or frame format, be sure that there is no ongoing
transmissions during the period the registers are changed. The TXC flag can be used to check that the transmitter has
completed all transfers, and the RXC flag can be used to check that there are no unread data in the receive buffer. Note
that the TXC flag must be cleared before each transmission (before UDR 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 registers R17:R16.
Assembly Code Example
(1)
USART_Init:
clr r18
out UBRRH,r18
out UBRRL,r18
; Setting the XCK port pin as output, enables master mode.
sbi XCK_DDR, XCK
; Set MSPI mode of operation and SPI data mode 0.
ldi r18, (1<<UMSEL1)|(1<<UMSEL0)|(0<<UCPHA)|(0<<UCPOL)
out UCSRC,r18
; Enable receiver and transmitter.
ldi r18, (1<<RXEN)|(1<<TXEN)
out UCSRB,r18
; Set baud rate.
; IMPORTANT: Baud Rate must be set after the transmitter is enabled!
out UBRRH, r17
out UBRRL, r18
ret