English
Language : 

MEGA128CAN Datasheet, PDF (181/413 Pages) ATMEL Corporation – Microcontroller WITH 128K BYTES OF ISP FLASH AND CAN CONTROLLER
AT90CAN128
The following code example shows a simple USART0 receive function based on polling
of the Receive Complete (RXC0) flag. When using frames with less than eight bits the
most significant bits of the data read from the UDR0 will be masked to zero. The
USART0 has to be initialized before the function can be used.
Assembly Code Example(1)
USART0_Receive:
; Wait for data to be received
lds r18, UCSR0A
sbrs r18, RXC0
rjmp USART0_Receive
; Get and return received data from buffer
lds r16, UDR0
ret
C Code Example(1)
unsigned char USART0_Receive (void )
{
/* Wait for data to be received */
while ( ! (UCSR0A & (1<<RXC0)))
;
/* Get and return received data from buffer */
return UDR0;
}
Note: 1. The example code assumes that the part specific header file is included.
The function simply waits for data to be present in the receive buffer by checking the
RXC0 flag, before reading the buffer and returning the value.
Receiving Frames with 9 Data
Bits
If 9-bit characters are used (UCSZn=7) the ninth bit must be read from the RXB8n bit in
UCSRnB before reading the low bits from the UDRn. This rule applies to the FEn,
DORn and UPEn Status Flags as well. Read status from UCSRnA, then data from
UDRn. Reading the UDRn I/O location will change the state of the receive buffer FIFO
and consequently the TXB8n, FEn, DORn and UPEn bits, which all are stored in the
FIFO, will change.
4250E–CAN–12/04
181