/***********************************************************************
* Universal Asynchronus Receiver Transmitter
***********************************************************************
* Function: Provice UART functions and methods.
***********************************************************************
* Processor: ATmega168
* Clock: F_CPU
* Language: ANSI-C
* Compiler: avr-gcc/4.1.3, avr-libc
* Date: $LastChangedDate$
* Author: $LastChangedBy$
* ID: $Id$
***********************************************************************
* License: Creative Commons Attribution-Share Alike 3.0 Germany
*
* Copyright: 2009, Soeren Kornetzki
* Attribution: You are not allowed to remove any license, copyright or
* attribution notice. In addition you have to inform your
* users about the using of this sourc code ( bundle ) in
* the following form ( you are allowed to translate ):
* This software is using #SourceCodeBundleName#
* library created by Soeren Kornetzki (
* http://dev5-media.de ) licensed under the creative
* commons attribution-share alike 3.0 german license.
* Replace #SourceCodeBundleName# with the source code
* bundle name.
**********************************************************************/
/* multiple include prevention */
#ifndef _UART_C_
#define _UART_C_ 1
/* define UART symbols per second, if not defined */
#ifndef UART_BAUD
#define UART_BAUD 9600UL
#endif
#define UART_UBRR ( ( ( F_CPU / 16 ) / UART_BAUD ) - 1 )
/* include librarys */
#include
#include
/* global variables */
/* prototypes */
void uart_init( void );
unsigned char uart_getc( void );
void uart_putc( unsigned char b );
void uart_puts( unsigned char* bs );
void uart_flush( void );
/***********************************************************************
* uart_init
* initialize the UART
***********************************************************************
* Baud: UART_BAUD
* Parity: Disabled
* Stop: 2-bit
* Char: 8-bit
**********************************************************************/
void uart_init( void )
{
/* calculate UART baud rate */
UBRR0H = ( unsigned char )( UART_UBRR >> 8 );
UBRR0L = ( unsigned char )UART_UBRR;
/* USART receiver, transmitter enable and interrupt handling */
#ifdef UART_RXD_INT
#ifdef UART_TXD_INT
/* RXD and TXD interrupt */
UCSR0B = ( _BV( RXCIE0 ) | _BV( TXCIE0 ) | _BV( RXEN0 ) | _BV( TXEN0 ) );
#else
/* RXD interrupt */
UCSR0B = ( _BV( RXCIE0 ) | _BV( RXEN0 ) | _BV( TXEN0 ) );
#endif
#else
#ifdef UART_TXD_INT
/* TXD interrupt */
UCSR0B = ( _BV( TXCIE0 ) | _BV( RXEN0 ) | _BV( TXEN0 ) );
#else
/* no interrupt */
UCSR0B = ( _BV( RXEN0 ) | _BV( TXEN0 ) );
#endif
#endif
/* USART mode: UART with no parity, 2-bit stop and 8-bit chars */
UCSR0C = ( _BV( USBS0 ) | _BV( UCSZ01 ) | _BV( UCSZ00 ) );
}
/***********************************************************************
* uart_getc
* returns last received byte by UART. If no byte received, wait for it
***********************************************************************
* @return char last received byte from UART
**********************************************************************/
unsigned char uart_getc( void )
{
while( !( UCSR0A & _BV( RXC0 ) ) ); /* waits until RX is completed */
return UDR0;
}
/***********************************************************************
* uart_putc
* transmit byte via UART
***********************************************************************
* @param uchar b byte to transmit via UART
**********************************************************************/
void uart_putc( unsigned char b )
{
while( !( UCSR0A & _BV( UDRE0 ) ) ); /* waits until preview TX is completed */
UDR0 = b;
}
/***********************************************************************
* uart_puts
* transmit string of bytes via UART
***********************************************************************
* @param uchar* bs string of bytes to transmit via UART
**********************************************************************/
void uart_puts( unsigned char* bs )
{
uint8_t i;
for( i = 0; bs[i] != 0; i++ )
uart_putc( bs[i] );
}
/***********************************************************************
* uart_flush
* flushes uart receive buffer
**********************************************************************/
void uart_flush( void )
{
unsigned char c;
while( UCSR0A & _BV( RXC0 ) )
c = UDR0;
}
#endif /* multiple include prevention */