// -------------------------------------------------------------------------------------------------------------------------------------------------------------- // UART.c // Routines for interrupt controlled UART // Last modified: 30-July-2007 // Modified by: MrPete from AVR sample code. // -------------------------------------------------------------------------------------------------------------------------------------------------------------- /* Includes */ #include #include #include #include "UART.h" /* UART Buffer Defines */ #define UART_RX_BUFFER_SIZE 16 /* 2,4,8,16,32,64,128 or 256 bytes */ #define UART_TX_BUFFER_SIZE 16 #define UART_RX_BUFFER_MASK ( UART_RX_BUFFER_SIZE - 1 ) #if ( UART_RX_BUFFER_SIZE & UART_RX_BUFFER_MASK ) #error RX buffer size is not a power of 2 #endif #define UART_TX_BUFFER_MASK ( UART_TX_BUFFER_SIZE - 1 ) #if ( UART_TX_BUFFER_SIZE & UART_TX_BUFFER_MASK ) #error TX buffer size is not a power of 2 #endif /* Static Variables -- Tx & Rx Ring Buffers */ static volatile unsigned char UART_RxHead; static volatile unsigned char UART_RxTail; static volatile unsigned char UART_TxHead; static volatile unsigned char UART_TxTail; static unsigned char UART_RxBuf[UART_RX_BUFFER_SIZE]; static unsigned char UART_TxBuf[UART_TX_BUFFER_SIZE]; // -------------------------------------------------------------------------------------------------------------------------------------------------------------- // Init UART - Enable Rx Interrupts void UART_Init( unsigned int baud ) { unsigned char x; UBRRH = (unsigned char)(baud>>8); /* Set baud rate */ UBRRL = (unsigned char)baud; /* Value depends on MPU clock speed */ UCSRB = (1<