/***************************************************************************
Function getchreading
Calculate minivolts on selected channel
****************************************************************************/
int16 getchreading(int channel)
{
SETUP_ADC_PORTS(sAN0|sAN1); // Select Analog channels on PIC
SETUP_ADC(ADC_CLOCK_DIV_8); // Select ADC conversion Clock
delay_ms(40); // give some time for charge capicitor to stabilize
int counter=0; // temporary counter
signed int16 temperature=0; // temporary adc_value variable
int16 highest=0; // saves highest reading for filtering
int16 lowest=0xFFFF; // saves lowest reading for filtering
int16 temporary=0;
int32 temp_result, adc_value, tlong;;
set_adc_channel(channel); // Select Channel
// Average ADC value
delay_us(15); // small delay to settle
counter=0; // clear counter
lowest=0xFFFF; // reset lowest
highest=0; // reset highest
while(counter<18) // Take 18 readings and average (16+highest+lowest)
{
temporary=READ_ADC(); // take an ADC reading
if(temporary<lowest) // check if smaller than lowest
lowest=temporary; // save value if smaller
if(temporary>highest) // check if larger than highest
highest=temporary; // save value if larger
temp_result-=temporary; // substract the ADC values for averaging (adc i\p)
counter++;
}
temp_result+=highest; // remove highest value
temp_result+=lowest; // remove lowest value
temp_result/=16; // average by 16
adc_value = temp_result
tlong = (int32)ADC_value*5000; //Convert the result in millivolts
tlong = tlong/1023; // 0..1023 -> 0-5000mV
SETUP_ADC(ADC_OFF); // turn off ADC module
return (int32) tlong;
}
/*****************************************************************************
Function ProcessMains
Reads Form AN0 ADC_Value and Reads Form AN1 ADC_Value
****************************************************************************/
int ProcessMains(void)
{
ch_0 = getchreading(0);
ch_1 = getchreading(1);
.here do somthing ch_0 & ch_1
.
.
.
.}