Datasheet
169
ATtiny828 [DATASHEET]
8371A–AVR–08/12
P
EVEN
Parity bit using even parity
P
ODD
Parity bit using odd parity
d
n
Data bit n of the character
If used, the parity bit is located between the last data bit and the first stop bit of a serial frame.
17.5 USART Initialization
The USART has to be initialized before any communication can take place. The initialization process normally consists of
setting the baud rate, setting frame format and, depending on the method of use, enabling the transmitter or the receiver.
For interrupt driven USART operation, the global interrupt flag should be cleared
and the USART interrupts should be
disabled.
Before re-initializating baud rate or frame format, it should be checked that there are 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, if used, the TXC flag
must be cleared before each transmission (before UDR is written).
The following simple USART initialization code examples show one assembly and one C function that are equal in
functionality. The examples assume asynchronous operation using polling (no interrupts enabled) and a fixed frame
format. 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:
; Set baud rate
out UBRRH, r17
out UBRRL, r16
; Enable receiver and transmitter
ldi r16, (1<<RXEN)|(1<<TXEN)
out UCSRB,r16
; Set frame format: 8 data bits, 2 stop bits
ldi r16, (1<<USBS)|(3<<UCSZ0)
out UCSRC,r16
ret