int sample(int volt) {
unsigned int cnt, adc_value;
for (cnt=0; cnt<20; cnt++){
adc_value = ADC_Read(0);
if (adc_value > volt);
volt = adc_value;
Delay_us (300);
} // Take 20 samples of ADC at an interval of
//300 uS and choose only the highest one.
}
void main() {
int volt;
TRISB = 0x00;
TRISA = 0xFF;
ADCON1 = 0x00;
do {
sample(volt);
if (volt > 128); // Compare the chosen value with a reference.
(PORTB = 0b00000001); // If greater than reference, do this.
(PORTB = 0b00000000); // Otherwise do this.
} while (1);
}