
141
ATmega8515(L)
2512A–AVR–04/02
Receiving Frames with 5 to 8
Data Bits
TheReceiverstartsdata reception when it detects avalidstart bit. Each bit that follows
the start bit will be sampled at the baudrate orXCKclock, andshifted into the receive
Shift Register until the first stopbit of a frame isreceived.Asecondstopbit will be
ignoredbythe receiver. When the first stopbit isreceived(i.e., a complete serialframe
ispresent in theReceive Shift Register), the contents of the Shift Registerwill bemoved
into the receive buffer.The receive buffercan then be readbyreading the UDR I/O
location.
The following codeexample shows a simple USART receive function based on polling
of theReceive Complete (RXC) flag. When using frameswithless than eight bits the
most significant bits of the data readfrom the UDR will bemasked to zero. The USART
has to beinitializedbeforethe function can beused.
Note: 1. Theexample codeassumes that the part specificheaderfileis included.
The function simply waitsfordata to be present in the receive bufferbychecking the
RXC flag,before reading the buffer andreturning thevalue.
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
CCode 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;
}
Comentarios a estos manuales