#include <htc.h>
#include <math.h>
#include <stdio.h>
#define _XTAL_FREQ 4000000
//#define bitset(var,bitno) (var|=1<<bitno)
//#define bitclr(var,bitno) (var&=~(1<<bitno))
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & UNPROTECT & BORDIS);
void main(void){
int x = 0;
int IRone = 0; //AN6/RC2
int IRtwo = 0; //AN7/RC3
int IRdiff = 0; //for difference in IR measurements
PORTA = 0x00; // PORTA low
PORTC = 0x00; // PORTC low
CMCON0 = 0x07; // turn off comparators
ADCON0 = 0b10011001; //turn on ADC AN6 selected - right justified
ADCON1 = 0b00010000; //ADC clock select FOSC/8 - 2us @ 4Mhz
TRISA = 0x00; //Port A outputs
TRISC = 0b00001100; //RC2 + RC3 input pins (AN6/AN7)
ANSEL = 0b11000000; //AN6/AN7 selected as analogue input pins
ADIE = 1; //enable ADC interrupt
ADIF = 0; //ensure ADC interrupt flag is cleared
while(1){
ADCON0 = 0b10011001; //ADC ON - AN6
ADCON0 = 0b10011011; //ADC GO - AN6
while(ADIF = 0){ //wait for ADC interrupt flag
_delay(1);
}
ADIF = 0; //clear ADC interrupt flag
IRone = (ADRESH*256) + ADRESL; //calc IRone - right justified
ADCON0 = 0b10011101; //ADC ON - AN7
ADCON0 = 0b10011111; //ADC GO - AN7
while(ADIF == 0){ //wait for ADC to finish
_delay(1);
}
ADIF = 0; //clear ADC interrupt flag
IRtwo = (ADRESH*256) + ADRESL; //calc IRtwo - right justified
IRdiff = IRone - IRtwo;
if(IRdiff >= 0){
PORTA = 0b00000001;
for(x = 0; x <=13; x++){
__delay_ms(20);
}
}
else{
PORTA = 0b00000000;
}
}
}