beautypg.com

2 receiving frames with 9 data bits, Atmega128rfa1 – Rainbow Electronics ATmega128RFA1 User Manual

Page 349

background image

349


8266A-MCU Wireless-12/09

ATmega128RFA1

C Code Example

(1)

unsigned char USART_Receive( void )

{

/* Wait for data to be received */

while ( !(UCSRnA & (1<

/* Get and return received data from buffer */

return UDRn;

}

Note:

1. See

"About Code Examples" on page 7

23.7.2 Receiving Frames with 9 Data Bits

If 9 bit characters are used (UCSZn2:0=7) the 9

th

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<

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