Is there a way to not have to poll the UART of an AVR?

There are interrupt vectors for both RXC and TXC (RX and TX complete) on AVRs. You should never have to poll for these unless you want to.

AVRFreaks has a nice post on this, and so does the manufacturer.


The interrupt routine stores the data in a buffer (a circular buffer with put and get pointers works nicely). The main loop checks to see if there is data in the buffer and when there is, takes it out. The main loop can do other things but needs to check and remove the data before the interrupt buffer overflows (when the put meets up with the get).

It won't compile but this illustrates the method.

char circ_buf[BUFFER_SIZE];
int get_index, put_index;

void initialize(void) {
    get_index = 0;
    put_index = 0;
}

isr serial_port_interrupt(void) {                       // interrupt
    circ_buf[put_index++] = SERIAL_PORT_REGISTER;
    if(put_index==get_index) error("buffer overflow");  // oops
    if(put_index==BUFFER_SIZE) put_index = 0;           // circular buffer
}

void background routine(void) {
    while(put_index!=get_index) {                       // or if()
        ch = circ_buf[get_index++];
        // do something with ch
        if(get_index==BUFFER_SIZE) get_index = 0;
        }
}

Tags:

Embedded

Avr