Data transmission – the usart transmitter, Sending frames with 5 to 8 data bit, Atmega16(l) – Rainbow Electronics ATmega64L User Manual
Page 143

143
ATmega16(L)
2466B–09/01
Data Transmission – The
USART Transmitter
The USART transmitter is enabled by setting the Transmit Enable (TXEN) bit in the
UCSRB register. When the transmitter is enabled, the normal port operation of the TxD
pin is overridden by the USART and given the function as the transmitter’s 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 XCK pin will be
overridden and used as transmission clock.
Sending Frames with 5 to 8
Data Bit
A data transmission is initiated by loading the transmit buffer with the data to be trans-
mitted. The CPU can load the transmit buffer by writing to the UDR I/O location. The
buffered data in the transmit buffer will be moved to the shift register when the shift reg-
ister 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, 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.
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.
Assembly Code Example
USART_Transmit:
; Wait for empty transmit buffer
sbis
UCSRA,UDRE
rjmp
USART_Transmit
; Put data (r16) into buffer, sends the data
out
UDR,r16
ret
C Code Example
void
USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while
( !( UCSRA & (1< ; /* Put data into buffer, sends the data */ UDR = data; }