Datasheet
175
ATtiny828 [DATASHEET]
8371A–AVR–08/12
Note: 1. See “Code Examples” on page 7.
The receive function example reads all the I/O registers into the register file before any computation is done. This gives
an optimal receive buffer utilization since the buffer location read will be free to accept new data as early as possible.
17.7.3 Receive Complete Flag and Interrupt
The USART receiver has one flag that indicates the receiver state.
The Receive Complete flag (RXC) indicates if there are unread data present in the receive buffer. This flag is set when
unread data exist in the receive buffer, and cleared when the receive buffer is empty (i.e., it does not contain any unread
data). If the receiver is disabled (RXEN = 0), the receive buffer will be flushed and, consequently, the RXC bit will
become zero.
When the Receive Complete Interrupt Enable (RXCIE) is set, the USART Receive Complete interrupt will be executed as
long as the RXC flag is set (and provided that global interrupts are enabled). When interrupt-driven data reception is
used, the receive complete routine must read the received data from UDR in order to clear the RXC flag, otherwise a new
interrupt will occur once the interrupt routine terminates.
17.7.4 Receiver Error Flags
The USART receiver has three error flags: Frame Error (FE), Data OverRun Error (DOR) and Parity Error (UPE). All error
flags are located in the receive buffer together with the frame for which they indicate the error status, and they can be
accessed via UCSRA. Due to the buffering of error flags, they must be read before the receive buffer (UDR), since
reading UDR changes the buffer.
Error flags can not be changed by software, however, for upward compatibility of future USART implementations all flags
must be cleared when UCSRA is written . None of the error flags can generate an interrupt.
z The Frame Error flag (FE) indicates the state of the first stop bit of the next readable frame stored in the receive
buffer. The flag is zero when the stop bit was correctly read (as one), and the flag is one when the stop bit was
incorrect (zero). This flag can be used for detecting out-of-sync conditions,for detecting break conditions and for
C Code Example
(1)
unsigned int USART_Receive( void )
{
unsigned char status, resh, resl;
/* Wait for data to be received */
while ( !(UCSRA & (1<<RXC)) )
;
/* Get status and 9th bit, then data from buffer */
status = UCSRA;
resh = UCSRB;
resl = UDR;
/* If error, return -1 */
if ( status & (1<<FE)|(1<<DOR)|(1<<UPE) )
return -1;
/* Filter the 9th bit, then return */
resh = (resh >> 1) & 0x01;
return ((resh << 8) | resl);
}