
170
ATmega64A [DATASHEET]
8160D–AVR–02/2013
21.6.1
Sending Frames with 5 to 8 Data Bits
A data transmission is initiated by loading the transmit buffer with the data to be transmitted. The CPU can load the
transmit buffer by writing to the UDRn I/O location. The buffered data in the transmit buffer will be moved to the
Shift Register when the Shift Register is ready to send a new frame. The Shift Register is loaded with new data if it
is in idle state (no ongoing transmission) or immediately after the last stop bit of the previous frame is transmitted.
When the Shift Register is loaded with new data, it will transfer one complete frame at the rate given by the baud
register, U2Xn bit or by XCK depending on mode of operation.
The following code examples show a simple USART transmit function based on polling of the Data Register Empty
(UDREn) flag. When using frames with less than eight bits, the most significant bits written to the UDRn 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:
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 the transmit buffer to be empty by checking the UDREn 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.
USART_Transmit:
; Wait for empty transmit buffer
sbis
UCSRnA,UDREn
rjmp
USART_Transmit
; Put data (r16) into buffer, sends the data
out
UDRn,r16
ret
void
USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while
( !( UCSRnA & (1<<UDREn)) )
;
/* Put data into buffer, sends the data */
UDRn = data;
}