/*
**	Project:	Delay Functions
**	Purpose:	Define a series of functions to delay for a specified
**				time.
**
**	Author:		Steven Pickles
**	Date:		Saturday, July 02, 2005
**
**	Notes:		The timer delays were tested using an oscilloscope, but
**				still are not 100% exact... although they are close.
*/

/*
**	Include the following code in the "main" file:
*/

///*
//**	External Global Variable (from delay.c)
//*/
//extern volatile uint16_t delayCount
///*
//**	Output Compare Interrupt Vector (for delay.c)
//**
//*/
//SIGNAL(SIG_OUTPUT_COMPARE0)
//{
//	//	Increment the delay counter variable
//	delayCount++;
//}



/*
**	Header File Information
*/
#ifndef __delay_h__
#define __delay_h__


/*
**	Compiler Include Directives
*/
#include <avr/io.h>
#include <avr/interrupt.h>


/*
**	Compiler Define Directives
*/

//	Frequency of the system clock
#define CLOCK_FREQUENCY		16000000L

//	Set the millisecond, and microsecond count constants
#if CLOCK_FREQUENCY == 16000000L
#define OCR_1MILLISECOND	248
#define OCR_1MICROSECOND	98
#elif CLOCK_FREQUENCY == 8000000L
#define OCR_1MILLISECOND	124
#define OCR_1MICROSECOND	49
#endif



/*
**	Function:		initializeDelayTimerMicrosecond
**	Parameters:		<none>
**	Purpose:		Initialize timer 0 to use the system clock and output
**					compare 0 to generate an interrupt once per	microsecond.
**					This is then used for any general purpose delay.
**	Returns:		<none>
*/
void initializeDelayTimerMicrosecond(void);


/*
**	Function:		initializeDelayTimerMillisecond
**	Parameters:		<none>
**	Purpose:		Initialize timer 0 to use the system clock and output
**					compare 0 to generate an interrupt once per millisecond.
**					This is then used for any general purpose delay.
**	Returns:		<none>
*/
void initializeDelayTimerMillisecond(void);


/*
**	Function:		runDelay
**	Parameters:		delayUnits - the number of times the interrupt must fire
**								 in order for the delay to be complete
**	Purpose:		Performs a delay for a specified number of units.
**	Returns:		<none>
*/
void runDelay(uint16_t delayUnits);


/*
**	Function:		delay50us
**	Parameters:		delayUnits - the number of times the interrupt must fire
**								 in order for the delay to be complete
**	Purpose:		Performs a delay for a specified number of units.
**					This is basically a wrapper function and is meant to be
**					used as a wrapper for the actual delay function.
**	Returns:		<none>
*/
void delay50us(uint16_t delayUnits);




/*
**	End of Header File Information
*/
#endif
