
171
ATmega64A [DATASHEET]
8160D–AVR–02/2013
21.6.2
Sending Frames with 9 Data Bits
If 9-bit characters are used (UCSZn = 7), the ninth bit must be written to the TXB8n bit in UCSRnB before the low
byte of the character is written to UDRn. The following code examples show a transmit function that handles 9-bit
characters. For the assembly code, the data to be sent is assumed to be stored in registers r17:r16.
Note:
1. These transmit functions are written to be general functions. They can be optimized if the contents of the UCSRnB
is static. For example, only the TXB8n bit of the UCSRnB Register is used after initialization.
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 ninth bit can be used for indicating an address frame when using Multi-processor Communication mode or for
other protocol handling as for example synchronization.
21.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 (UDREn) flag 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 Data Register empty Interrupt Enable (UDRIEn) bit in UCSRnB is written to one, the USART Data Reg-
ister 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
USART_Transmit:
; Wait for empty transmit buffer
sbis
UCSRnA,UDREn
rjmp
USART_Transmit
; Copy ninth bit from r17 to TXB8
cbi
UCSRnB,TXB8n
sbrc
r17,0
sbi
UCSRnB,TXB8n
; Put LSB data (r16) into buffer, sends the data
out
UDRn,r16
ret
void
USART_Transmit( unsigned int data )
{
/* Wait for empty transmit buffer */
while
( !( UCSRnA & (1<<UDREn)) )
;
/* Copy ninth bit to TXB8 */
UCSRnB &= ~(1<<TXB8n);
if ( data & 0x0100 )
UCSRnB |= (1<<TXB8n);
/* Put data into buffer, sends the data */
UDRn = data;
}