Datasheet

80
ATmega323(L)
1457E11/01
The following code examples show a simple USART transmit function based on polling
of the Data Register Empty (UDRE) flag. When using frames with less than eight bit, the
most significant bits written to the UDR are ignored. The USART has to be initialized
before the function can be used. For the assembly code, the data to be sent is assumed
to be stored in Register R16.
Note: 1. The example code assumes that the part specific header file is included.
The function simply waits for the transmit buffer to be empty by checking the UDRE flag,
before loading it with new data to be transmitted. If the data register empty interrupt is
utilized, the interrupt routine writes the data into the buffer.
Assembly Code Example
(1)
USART_Transmit:
; Wait for empty transmit buffer
sbis UCSRA,UDRE
rjmp USART_Transmit
; Put data (r16) into buffer, sends the data
out UDR,r16
ret
C Code Example
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) ) {};
/* Put data into buffer, sends the data */
UDR = data;
}