Programming: UART/USART Creating a Receive Data Function (Specifically for a Library or Include File)

This is a function that is created for a library to be reused whenever the microcontroller needs to receive data from the UART/USART. The RXC is the Receive Complete Flag and lets us know when the receive is complete so we can get the data in the UDR (UART Data Register). The return type is unsigned char because we want the returned data in the positive realm of 0-255 so it conforms to the ASCII set of characters. The 0's at the end of each register is used because the microcontroller has two USARTs.



unsigned char ReceiveUART0(void) { while (! (UCSR0A & (1 << RXC0)) ); return UDR0; } If the microcontroller only has one USART, then use the following code: unsigned char ReceiveUART0(void) { while (! (UCSRA & (1 << RXC)) ); return UDR; }
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.