Datasheet
ATmega48/88/168 Automotive [DATASHEET]
7530K–AVR–07/14
154
The TXCn flag is useful in half-duplex communication interfaces (like the RS-485 standard), where a transmitting application
must enter receive mode and free the communication bus immediately after completing the transmission.
When the transmit compete interrupt enable (TXCIEn) bit in UCSRnB is set, the USART transmit complete interrupt will be
executed when the TXCn flag becomes set (provided that global interrupts are enabled). When the transmit complete
interrupt is used, the interrupt handling routine does not have to clear the TXCn flag, this is done automatically when the
interrupt is executed.
17.5.4 Parity Generator
The parity generator calculates the parity bit for the serial frame data. When parity bit is enabled (UPMn1 = 1), the
transmitter control logic inserts the parity bit between the last data bit and the first stop bit of the frame that is sent.
17.5.5 Disabling the Transmitter
The disabling of the transmitter (setting the TXEN to zero) will not become effective until ongoing and pending transmissions
are completed, i.e., when the transmit shift register and transmit buffer register do not contain data to be transmitted. When
disabled, the transmitter will no longer override the TxDn pin.
17.6 Data Reception – The USART Receiver
The USART receiver is enabled by writing the receive enable (RXENn) bit in the UCSRnB register to one. When the receiver
is enabled, the normal pin operation of the RxDn pin is overridden by the USART and given the function as the receiver’s
serial input. The baud rate, mode of operation and frame format must be set up once before any serial reception can be
done. If synchronous operation is used, the clock on the XCKn pin will be used as transfer clock.
17.6.1 Receiving Frames with 5 to 8 Data Bits
The receiver starts data reception when it detects a valid start bit. Each bit that follows the start bit will be sampled at the
baud rate or XCKn clock, and shifted into the receive shift register until the first stop bit of a frame is received. A second stop
bit will be ignored by the receiver. When the first stop bit is received, i.e., a complete serial frame is present in the receive
shift register, the contents of the shift register will be moved into the receive buffer. The receive buffer can then be read by
reading the UDRn I/O location.
The following code example shows a simple USART receive function based on polling of the receive complete (RXCn) flag.
When using frames with less than eight bits the most significant bits of the data read from the UDRn will be masked to zero.
The USART has to be initialized before the function can be used.
Assembly Code Example
(1)
USART_Receive:
; Wait for data to be received
sbis UCSRnA, RXCn
rjmp USART_Receive
; Get and return received data from buffer
in r16, UDRn
ret
C Code Example
(1)
unsigned char USART_Receive( void )
{
/* Wait for data to be received */
while ( !(UCSRnA & (1<<RXCn)) )
;
/* Get and return received data from buffer */
return UDRn;
}
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”.