180
AT90PWM216/316 [DATASHEET]
7710H–AVR–07/2013
the Shift Register is loaded with new data, it will transfer one complete frame at the rate given by the Baud Regis-
ter, U2X 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
(UDRE) flag. When using frames with less than eight bits, 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.
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 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.
17.6.2
Sending Frames with 9 Data Bit
If 9-bit characters are used (UCSZ = 7), the ninth bit must be written to the TXB8 bit in UCSRB before the low byte
of the character is written to UDR. The following code examples show a transmit function that handles 9-bit charac-
ters. For the assembly code, the data to be sent is assumed to be stored in registers R17:R16.
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
sts
UDR,r16
ret
C Code Example
(1)
void
USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while
( !( UCSRA & (1<<UDRE)) )
;
/* Put data into buffer, sends the data */
UDR = data;
}