Datasheet

UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}
More advanced initialization routines can be written to include frame format as parameters, disable
interrupts, and so on. However, many applications use a fixed setting of the baud and control registers,
and for these types of applications the initialization code can be placed directly in the main routine, or be
combined with initialization code for other I/O modules.
Related Links
9. About Code Examples
24.7 Data Transmission – The USART Transmitter
The USART transmitter is enabled by setting the Transmit Enable (TXEN) bit in the UCSRnB register.
When the transmitter is enabled, the normal port operation of the TxDn pin is overridden by the USART
and given the function as the transmitters serial output. The baud rate, mode of operation and frame
format must be set up once before doing any transmissions. If synchronous operation is used, the clock
on the XCKn pin will be overridden and used as transmission clock.
24.7.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 an 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 XCKn depending on the 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 UDR0 are ignored. The USART 0 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 R17.
Assembly Code Example
USART_Transmit:
; Wait for empty transmit buffer
in r17, UCSR0A
sbrs r17, UDRE
rjmp USART_Transmit
; Put data (r16) into buffer, sends the data
out UDR0,r16
ret
C Code Example
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE)) )
;
/* Put data into buffer, sends the data */
UDR0 = data;
}
ATmega48PA/88PA/168PA
Universal Synchronous Asynchronous Receiver ...
© 2018 Microchip Technology Inc.
Datasheet Complete
DS40002011A-page 243