Atmega128(l) – Rainbow Electronics ATmega128L User Manual

Page 175

Advertising
background image

175

ATmega128(L)

2467B–09/01

The following code example shows a simple USART receive function based on polling
of the Receive Complete (RXC) flag. When using frames with less than eight bits the
most significant bits of the data read from the UDR will be masked to zero. The USART
has to be initialized before the function can be used.

Note:

1. The example code assumes that the part specific header file is included.

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
RXC flag, before reading the buffer and returning the value.

Assembly Code Example

(1)

USART_Receive:

; Wait for data to be received

sbis

UCSRA, RXC

rjmp

USART_Receive

; Get and return received data from buffer

in

r16, UDR

ret

C Code Example

(1)

unsigned char USART_Receive( void )

{

/* Wait for data to be received */

while ( !(UCSRA & (1<<RXC)) )

;

/* Get and return received data from buffer */

return UDR;

}

Advertising