beautypg.com

3 receive complete flag and interrupt, 4 receiver error flags – Rainbow Electronics ATmega128RFA1 User Manual

Page 350

background image

350



8266A-MCU Wireless-12/09

ATmega128RFA1

C Code Example

(1)

unsigned int USART_Receive( void )

{

unsigned char status, resh, resl;

/* Wait for data to be received */

while ( !(UCSRnA & (1<

/* Get status and 9th bit, then data */

/* from buffer */

status = UCSRnA;

resh = UCSRnB;

resl = UDRn;

/* If error, return -1 */

if ( status & (1<

return -1;

/* Filter the 9th bit, then return */

resh = (resh >> 1) & 0x01;

return ((resh << 8) | resl);

}

Note:

1. See

"About 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.

23.7.3 Receive Complete Flag and Interrupt

The USART receiver has one flag that indicates the receiver state.

The Receive Complete Flag (RXCn) indicates if there are unread data present in the
receive buffer. This flag is one when unread data exist in the receive buffer, and zero
when the receive buffer is empty (i.e., does not contain any unread data). If the receiver
is disabled (RXENn = 0), the receive buffer will be flushed and consequently the RXCn
bit will become zero.

When the Receive Complete Interrupt Enable (RXCIEn) in UCSRnB is set, the USART
receive complete interrupt will be executed as long as the RXCn flag is set (provided
that global interrupts are enabled). When interrupt-driven data reception is used, the
receive complete routine must read the received data from UDRn in order to clear the
RXCn flag, otherwise a new interrupt will occur once the interrupt routine terminates.

23.7.4 Receiver Error Flags

The USART receiver has three error flags: Frame Error (FEn), Data OverRun (DORn)
and Parity Error (UPEn). All can be accessed by reading UCSRnA. Common for the
error flags is that they are located in the receive buffer together with the frame for which
they indicate the error status. Due to the buffering of the error flags, the UCSRnA must
be read before the receive buffer (UDRn), since reading the UDRn I/O location changes
the buffer read location. The error flags cannot be altered by the application software
doing a write to the flag location. However, all flags must be set to zero when the
UCSRnA is written for upward compatibility of future USART implementations. None of
the error flags can generate interrupts.

The Frame Error Flag (FEn) indicates the state of the first stop bit of the next readable
frame stored in the receive buffer. The FEn flag is zero when the stop bit was correctly