English
Language : 

ATMEGA128RFA1_12 Datasheet, PDF (353/568 Pages) ATMEL Corporation – Microcontroller with Low Power 2.4GHz Transceiver for ZigBee and IEEE 802.15.4
ATmega128RFA1
C Code Example(1)
unsigned char USART_Receive( void )
{
/* Wait for data to be received */
while ( !(UCSRnA & (1<<RXCn)) );
/* Get and return received data from buffer */
return UDRn;
}
Note: 1. See "About Code Examples" on page 8
23.7.2 Receiving Frames with 9 Data Bits
If 9 bit characters are used (UCSZn2:0=7) the 9th bit must be read from the RXB8n bit in
UCSRnB before reading the low bits from the UDRn register. 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(1)
USART_Receive:
; Wait for data to be received
sbis UCSRnA, RXCn
rjmp USART_Receive
; Get status and 9th bit, then data from buffer
in r18, UCSRnA
in r17, UCSRnB
in r16, UDRn
; If error, return -1
andi r18,(1<<FEn)|(1<<DORn)|(1<<UPEn)
breq USART_ReceiveNoError
ldi r17, HIGH(-1)
ldi r16, LOW(-1)
USART_ReceiveNoError:
; Filter the 9th bit, then return
lsr r17
andi r17, 0x01
ret
8266D-MCU Wireless-06/12
353