189
8271D–AVR–05/11
ATmega48A/PA/88A/PA/168A/PA/328/P
Note:
1.
See ”About Code Examples” on page 8.
For I/O Registers located in extended I/O map, “IN”, “OUT”, “SBIS”, “SBIC”, “CBI”, and “SBI”
instructions must be replaced with instructions that allow access to extended I/O. Typically
“LDS” and “STS” combined with “SBRS”, “SBRC”, “SBR”, and “CBR”.
The function simply waits for data to be present in the receive buffer by checking the RXCn Flag,
before reading the buffer and returning the value.
20.7.2
Receiving Frames with 9 Data Bits
If 9-bit characters are used (UCSZn=7) the ninth bit must be read from the RXB8n bit in
UCSRnB
before
reading the low bits from the UDRn. This rule applies to the FEn, DORn and
UPEn Status Flags as well. Read status from UCSRnA, then data from UDRn. Reading the
UDRn I/O location will change the state of the receive buffer FIFO and consequently the TXB8n,
FEn, DORn and UPEn bits, which all are stored in the FIFO, will change.
The following code example shows a simple USART receive function that handles both nine bit
characters and the status bits.
Assembly Code Example
USART_Receive:
;
Wait for data to be received
in r16, UCSRnA
sbrs
r16, UDREn
rjmp
USART_Receive
;
Get and return received data from buffer
in
r16, UDRn
ret
C Code Example
unsigned char
USART_Receive(
void
)
{
/*
Wait for data to be received
*/
while
( !(UCSRnA & (1<<RXCn)) )
;
/*
Get and return received data from buffer
*/
return
UDRn;
}