skip navigational linksPJRC
Shopping Cart Checkout Shipping Cost Download Website
Home MP3 Player 8051 Tools All Projects PJRC Store Site Map
You are here: Teensy Code Library UART

PJRC Store
Teensy 3.1, $19.80
Teensy 2.0, $16.00
Teensy++ 2.0, $24.00
USB Cable, $4.00
Teensy
Main Page
Teensy 3.1
Getting Started
How-To Tips
Code Library
Projects
Teensyduino
Reference

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');
                }
        }
}