/** I N C L U D E S **********************************************************/
#include
#include
/** V A R I A B L E S ********************************************************/
#pragma udata
/** P R I V A T E P R O T O T Y P E S ***************************************/
/** V E C T O R R E M A P P I N G *******************************************/
extern void _startup (void); // See c018i.c in your C18 compiler dir
#pragma code _RESET_INTERRUPT_VECTOR = 0x000800
void _reset (void)
{
_asm goto _startup _endasm
}
#pragma code
#pragma code _HIGH_INTERRUPT_VECTOR = 0x000808
void _high_ISR (void)
{
;
}
#pragma code _LOW_INTERRUPT_VECTOR = 0x000818
void _low_ISR (void)
{
;
}
#pragma code
/** D E C L A R A T I O N S **************************************************/
#pragma code
/******************************************************************************
* Function: void main(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Main program entry point.
*
* Note: None
*****************************************************************************/
/** L E D ***********************************************************/
// Sensor Inputs
#define mInitAllSensors() TRISAbits.TRISA1=1;TRISAbits.TRISA2=1;
#define mInitAllLEDs() TRISBbits.TRISB0=0;TRISBbits.TRISB1=0;
#define SensorLeft PORTAbits.RA2
#define SensorRight PORTAbits.RA1
#define LeftLED PORTBbits.RB0
#define RightLED PORTBbits.RB1
int i=0;
void Delay_ms( long int x)
{
for(i=0;i
}
void Delay_s( long int x)
{
for(i=0;i
void Delay_us(long int x)
{
for(i=0;i
int sensor =0;
int SensorR()
{SetChanADC( ADC_CH1 );
ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for completion
sensor = ReadADC(); // Read result
if (sensor<350) return 1;
if(sensor>350) return 0;
}
int SensorL()
{SetChanADC( ADC_CH2 );
ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for completion
sensor = ReadADC(); // Read result
if (sensor<350) return 1;
if(sensor>350) return 0;
}
void code()
{
if(SensorL()==1 && SensorR()==0) {RightLED=1;LeftLED=0;}
else if (SensorR()==1 && SensorL()==0){LeftLED=1;RightLED=0;}
else {LeftLED=0;RightLED=0;}
}
void main(void)
{
ADCON1 |= 0x0F; // Default all pins to digital
mInitAllSensors();
mInitAllLEDs();
//ADC begins
OpenADC( ADC_FOSC_2 & ADC_RIGHT_JUST & ADC_1ANA , ADC_CH1 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS , 0b1010 );
OpenADC( ADC_FOSC_2 & ADC_RIGHT_JUST & ADC_1ANA , ADC_CH2 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS , 0b1010 );
Delay_ms( 50 ); // Delay for 50
//ADC ends
code();
}
//end main
/** EOF Demo02.c *************************************************************/