English
Language : 

ATMEGA64RFR2_14 Datasheet, PDF (380/611 Pages) ATMEL Corporation – Microcontroller with Low Power
C Code Example(1)(2)
void USART_Transmit( unsigned int data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRnA & (1<<UDREn))) );
/* Copy 9th bit to TXB8 */
UCSRnB &= ~(1<<TXB8);
if ( data & 0x0100 )
UCSRnB |= (1<<TXB8);
/* Put data into buffer, sends the data */
UDRn = data;
}
Note:
1. These transmit functions are written to be general functions. They can be
optimized if the content of the UCSRnB is static. For example, only the TXB8 bit
of the UCSRnB register is used after initialization.
2. See "About Code Examples" on page 7
The 9th bit can be used for indicating an address frame when using multi processor
communication mode or for other protocol handling as for example synchronization.
23.6.3 Transmitter Flags and Interrupts
The USART transmitter has two flags that indicate its state: USART Data Register
Empty (UDREn) and Transmit Complete (TXCn). Both flags can be used for generating
interrupts.
The Data Register Empty Flag (UDREn) indicates whether the transmit buffer is ready
to receive new data. This bit is set when the transmit buffer is empty, and cleared when
the transmit buffer contains data to be transmitted that has not yet been moved into the
shift register. For compatibility with future devices, always write this bit to zero when
writing the UCSRnA register.
When the USART Data Register Empty Interrupt Enable (UDRIEn) bit in UCSRnB is
written to one, the USART data register empty interrupt will be executed as long as
UDREn is set (provided that global interrupts are enabled). UDREn is cleared by writing
UDRn. When interrupt-driven data transmission is used, the data register empty
interrupt routine must either write new data to UDRn in order to clear UDREn or disable
the data register empty interrupt, otherwise a new interrupt will occur once the interrupt
routine terminates.
The Transmit Complete Flag (TXCn) bit is set one when the entire frame in the transmit
shift register has been shifted out and there are no new data currently present in the
transmit buffer. The TXCn flag bit is automatically cleared when a transmission
complete interrupt is executed, or it can be cleared by writing a one to its bit location.
The TXCn flag is useful in half-duplex communication interfaces (like the RS-485
standard), where a transmitting application must enter receive mode and free the
communication bus immediately after completing the transmission.
When the Transmission Complete Interrupt Enable (TXCIEn) bit in UCSRnB is set, the
USART transmission complete interrupt will be executed when the TXCn flag becomes
set (provided that global interrupts are enabled). When the transmission complete
interrupt is used, the interrupt handling routine does not have to clear the TXCn flag.
This is done automatically when the interrupt is executed.
380 ATmega256/128/64RFR2
8393C-MCU Wireless-09/14