You are on page 1of 3

//**************************************************************

//******** FUNCTIONS FOR SERIAL COMMUNICATION USING UART *******


//**************************************************************
//Controller: ATmega8 (Clock: 8 Mhz-internal)
//Compiler: AVR-GCC
//Version : 2.0
//Author: CC Dharmani, Chennai (India)
// www.dharmanitech.com
//Date: 26 Feb 2009
//**************************************************************
//**************************************************
// ***** HEADER FILE : UART_routines.h ******
//**************************************************
#ifndef _UART_ROUTINES_H_
#define _UART_ROUTINES_H_
#define CHAR 0
#define INT 1
#define LONG 2
#define TX_NEWLINE {transmitByte(0x0d); transmitByte(0x0a);}
unsigned char receiveByte(void);
void transmitByte(unsigned char);
void transmitHex( unsigned char dataType, unsigned long data );
void transmitString_F(char*);
void transmitString(unsigned char*);

#endif
//**************************************************************
//******** FUNCTIONS FOR SERIAL COMMUNICATION USING UART *******
//**************************************************************
//Controller: ATmega8 (Clock: 8 Mhz-internal)
//Compiler: AVR-GCC
//Version : 2.0
//Author: CC Dharmani, Chennai (India)
// www.dharmanitech.com
//Date: 26 Feb 2009
//**************************************************************
//**************************************************
// ***** SOURCE FILE : UART_routines.c ******
//**************************************************
#include "UART_routines.h"
#include <avr/io.h>
#include <avr/pgmspace.h>

//**************************************************
//Function to receive a single byte
//*************************************************
unsigned char receiveByte( void )
{
unsigned char data, status;
while(!(UCSRA & (1<<RXC)));

// Wait for incomming data

status = UCSRA;
data = UDR;
return(data);
}
//***************************************************
//Function to transmit a single byte
//***************************************************
void transmitByte( unsigned char data )
{
while ( !(UCSRA & (1<<UDRE)) ); // Wait for empty transmit buffer
UDR = data;
//Start transmition
}

//***************************************************
//Function to transmit hex format data
//first argument indicates type: CHAR, INT or LONG
//Second argument is the data to be displayed
//***************************************************
/*void transmitHex( unsigned char dataType, unsigned long data )
{
unsigned char count, i, temp;
unsigned char dataString[] = "0x
";
if (dataType == CHAR) count = 2;
if (dataType == INT) count = 4;
if (dataType == LONG) count = 8;
for(i=count; i>0; i--)
{
temp = data % 16;
if((temp>=0) && (temp<10)) dataString [i+1] = temp + 0x30;
else dataString [i+1] = (temp - 10) + 0x41;
data = data/16;
}

transmitString (dataString);
}
*/
//***************************************************
//Function to transmit a string in Flash
//***************************************************
void transmitString_F(char* string)
{
while (pgm_read_byte(&(*string)))
transmitByte(pgm_read_byte(&(*string++)));
}
//***************************************************
//Function to transmit a string in RAM
//***************************************************
void transmitString(unsigned char* string)
{
while (*string)
transmitByte(*string++);
}
//************ END ***** www.dharmanitech.com *******

You might also like