187
7679H–CAN–08/08
AT90CAN32/64/128
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.
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.
17.8.2
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 UCS-
RnB
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.
Assembly Code Example
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
unsigned char
USART0_Receive (
void
)
{
/*
Wait for data to be received
*/
while
( ! (UCSR0A & (1<<RXC0)));
/*
Get and return received data from buffer
*/
return
UDR0;
}