[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [avr-gcc-list] Sought: interrupt-driven buffered UART sender

From: Ron Kreymborg
Subject: RE: [avr-gcc-list] Sought: interrupt-driven buffered UART sender
Date: Tue, 23 Oct 2001 23:27:13 +1000

> Speaking of wanted coding examples...
> Does anybody have an example of a buffered, interrupt-driven UART
> sending routine for the AVRs?  I tried implementing one myself, but it
> isn't working quite correctly, so before re-inventing the wheel, i
> thought i'd ask here.
> --
> J"org Wunsch                                         Unix support engineer
> address@hidden
> http://www.interface-systems.de/~j/

Below and attached a Uart send routine I use quite a lot.


Ron Kreymborg
Jennaron Research
Melbourne, Australia

// UartOut.c header file. Users to include.

extern volatile int TxBusy;

extern void UartOutInit(void);
extern void UartSend(char *buffer, int length);

// Transmit a known length buffer of bytes. Check that the public
// boolean <TxBusy> is false before calling UartSend(). If there
// can be a number of callers, check and set the flag from within
// a critical region first (ie bracket with interrupts off). If
// you are using an rtos, replace the flag with a semaphore.
// Only bits 0 and 1 of PORTD need be defined here.
// Ron Kreymborg

#include <io.h>
#include <sig-avr.h>
#include "UartOut.h"

// Baud rates
#define UART_BAUD_19200           12
#define UART_BAUD_9600            25
#define UART_BAUD_2400            103

volatile int TxBusy;

static volatile char *TxPntr;
static volatile int MessageLen;

// Init the hardware
void UartOutInit(void)
    // PORTD
    outp(0x00, PORTD);              // all low
    outp(0xfc, DDRD);               // 0 & 1 input for UART
    // Enable RxD/TxD and ints
    outp((1<<RXCIE) | (1<<TXCIE) | (1<<RXEN) | (1<<TXEN), UCR);
    // Set baud rate
    outp(UART_BAUD_9600, UBRR);
    // Flag as not busy
    TxBusy = 0;

// Call with a pointer to the bytes to send in <buffer>, the number
// of bytes to send in <length>. This function writes the first
// byte to the Uart then returns. The interrupt routine sends the rest.
void UartSend(char *buffer, int length)
    TxBusy = 1;                     // Tx now busy
    MessageLen = length;            // save length
    TxPntr = buffer;                // and pointer
    outp(*TxPntr++, UDR);           // write first byte from data buffer

// Signal handler for uart txd ready interrupt.
    if (--MessageLen > 0)           // while bytes to send
        outp(*TxPntr++, UDR);       // write next byte from data buffer
        TxBusy = 0;                 // once again available for transmitting

Attachment: UartOut.zip
Description: Zip compressed data

reply via email to

[Prev in Thread] Current Thread [Next in Thread]