Datasheet
79
ATmega323(L)
1457E–11/01
Note: 1. The example code assumes that the part specific header file is included.
More advanced initialization routines can be made that include frame format as parame-
ters, 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.
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.
Assembly Code Example
(1)
USART_Init:
; Set baud rate
out UBRRH, r17
out UBRRL, r16
; Enable receiver and transmitter
ldi r16, (1<<RXEN)|(1<<TXEN)
out UCSRB,r16
; Set frame format: 8data, 2stop bit
ldi r16, (1<<URSEL)|(1<<USBS)|(3<<UCSZ0)
out UCSRC,r16
ret
C Code Example
(1)
void USART_Init( unsigned int baud )
{
/* Set baud rate */
UBRRH = (unsigned char)(baud>>8);
UBRRL = (unsigned char)baud;
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}