1 parity bit calculation, 5 usart initialization, Atmega128rfa1 – Rainbow Electronics ATmega128RFA1 User Manual
Page 344

344
8266A-MCU Wireless-12/09
ATmega128RFA1
The USART Character Size (UCSZn2:0) bits select the number of data bits in the
frame. The USART Parity Mode (UPMn1:0) bits enable and set the type of parity bit.
The selection between one or two stop bits is done by the USART Stop Bit Select
(USBSn) bit. The receiver ignores the second stop bit. A frame error will therefore only
be detected in cases where the first stop bit is zero.
23.4.1 Parity Bit Calculation
The parity bit is calculated by doing an exclusive-or of all the data bits. If odd parity is
used, the result of the exclusive or is inverted. The parity bit is located between the last
data bit and first stop bit of a serial frame. The relation between the parity bit and data
bits is as follows:
1
0
0
1
2
3
1
0
1
2
3
1
⊕
⊕
⊕
⊕
⊕
⊕
=
⊕
⊕
⊕
⊕
⊕
⊕
=
−
−
d
d
d
d
d
P
d
d
d
d
d
P
n
odd
n
even
K
K
P
even
Parity bit using even parity
P
odd
Parity bit using odd parity
d
n
Data bit n of the character
23.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
enabling the transmitter or the receiver depending on the usage. For interrupt driven
USART operation, the global interrupt flag should be cleared (and interrupts globally
disabled) when doing the initialization.
Before doing a re-initialization with changed baud rate 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 RXC 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 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 the r17:r16 Registers.
Assembly Code Example
(1)
USART_Init:
; Set baud rate
out UBRRnH, r17
out UBRRnL, r16
; Enable receiver and transmitter
ldi r16, (1< out UCSRnB,r16 ; Set frame format: 8data, 2stop bit ldi r16, (1< out UCSRnC,r16 ret