UART: Serial Port

This code implements basic functions to use the UART as a serial port.

Download Source Files

Example Application

A very simple example prints "UART Example" at startup, then waits for keystrokes. Each byte is echoed back with a short message.

TODO: screenshot

Functions

#include <uart.h>

uart_available()

How many characters are waiting in the receive buffer?

This function returns the number of buffered bytes, or 0 if nothing is in the receive buffer. If your application must do other functions while waiting for data, or you wish to implement timeouts, this should be called before uart_getchar() to check if any data is available.

uart_getchar()

Receive a character.

The next byte is returned (0 to 255). If no data is available, this function waits for a byte to arrive.

uart_putchar(character)

Transmit a single character.

A transmit buffer is used, so this function returns quickly if your byte can be stored in the buffer. Otherwise, it waits for space to become available in the buffer.

uart_init(baud)

Initialize the UART controller at a particular baud rate.

Example Usage

int main(void)
{
        uint8_t c;

        CPU_PRESCALE(0);  // run at 16 MHz
        uart_init(BAUD_RATE);
        uart_print("UART Example\r\n");
        while (1) {
                if (uart_available()) {
                        c = uart_getchar();
                        uart_print("Byte: ");
                        uart_putchar(c);
                        uart_putchar('\r');
                        uart_putchar('\n');
                }
        }
}