Interrupt Driven UART

Author:
Andrey Yurovsky <http://andrey.thedotcommune.com>

Introduction

This is an interrupt driven UART driver for Atmel AVR microcontrollers that use the 'avr5' core and have one USART (for example the ATmega48 and similar chips). It can also be used on other AVRs with minor modifications to the register names.

This driver uses two identical ring buffers (circular queues) which by default take up 130 bytes each. You may change the buffer data structure size by defining BUFFER_BITS in your compiler flags (the size if 2^BUFFER_BITS+1). See buffer.h for more information.

Quick Start

To use this driver, include uart.h and define BAUD to be a desired baud rate (the default is 9600). You may want to define UART_X2 to use X2 clock mode if it results in a lower error rate for your combination of CPU frequency and baud rate. See the data sheet for a table of error rates. The CPU speed must be defined as F_CPU in Hz. The BAUD_SETTING marco is provided to conveniently calculate the register settings for your desired baud rate.

The following example (main.c) initializes the UART, prints a string, and then echoes any received characters back:

#include <avr/io.h>
#include "uart.h"

int main( void )
{
    char c;

    /* initialize */
    uart_init( BAUD_SETTING );
    
    /* enable interrupts */
    asm("sei");

    uart_puts( "Hello, world!\n" );

    while( 1 ) {
        while( !uart_getchar( &c ) ); /* wait for a character */
        uart_putchar( c ); /* echo it */
    }
    return 0;
}

This file and a Makefile to build it are included. The Makefile is based on the Makefile provided with the avr-libc project and it has only been tested on Linux.


Generated on Mon Mar 26 00:29:13 2007 for UARTDriver by  doxygen 1.5.1