/*
Chris @ http://www.pyroelectro.com
Date: 2/5/2008

Ir Range Sensor Test

Program Description: This program takes an analog single
through the pic adc and uses the digital value to blink an
led at a unique frequency dependant upon the analog voltage
value.

*/

//Initial Include Libraries
#include <p18f452.h>
#include <adc.h>
#include <stdlib.h>
#include <delays.h>

int result;	//ADC Result Stored Here

void main(void)
{

//Initialize PORTA where the led is attached.
TRISA = 0x00;
PORTA = 0x00;

while(1)
{

PORTA = 0x00;	//Turn the LED Off
if(result == 0)	//Result should never be 0 but if it is, make it 1
	result = 1;			//This avoids any 'false' led blinks
Delay10KTCYx(result*2);	//Pause the Program This Much Time
PORTA = 0x01;			//Turn the LED on
Delay10KTCYx(result*2);	//Pause the Program This Much Time

OpenADC( ADC_FOSC_32 	//Configure & Turn on ADC
		& ADC_RIGHT_JUST 
		& ADC_8ANA_0REF, 
		ADC_CH1 & ADC_INT_OFF );

Delay10TCYx(5);			//Pause for AD Conversion
ConvertADC();			//Convert the Value
while( BusyADC() );		//When ADC isn't busy get the adc value
	result = ReadADC();
CloseADC();				//turn off adc's

}

}
