#pragma chip PIC16F684
#include "delay.h"
void main(void){
int adval, a, b; // adval is the ADC value
TRISA.0 = 1; // Set RA0/AN0 (pin 13) as I/P
TRISC.2 = 0; // Set RC2/AN6 (pin 8) as a dig O/P
ANSEL.0 = 1; // Set RA0/AN0 (pin 13) as analog I/P
ADCON1 = 0b.0011.0000; // Clock set to internal Frc
while(1){
//Do the sensor conversion
delay_us(50); // Set delay of 50 us for sampling (TAD)
ADCON0 = 0b.1000.0011; // Right justified, Vref = Vdd,
// Read from AN0 (pin 13), Go/Done = 1, ADC Enabled
while(ADCON0.1 == 1){} // A/D conversion started, continue to check Go/Done, when 0 it is completed
// Bit 1 of ADCON0 is cleared when finished
a = ADRESH.1; // Read bit 1 of ADRESH into variable a
a = a * 512; // Multiply "a" by the position of that bit (2^9)
b = ADRESH.0; // Read bit 0 of ADRESH into variable b
b = b * 256; // Multiply "b" by the position of that bit (2^8)
adval = ADRESL + a + b; // ADC value is the sum of ADRESL, a, and b
if(adval <369){ // LED on at RC2 (pin 8) when AN0 < 1.8V, off > 1.8V
PORTC = 0b.0000.0100;
}
else {
PORTC = 0b.0000.0000;
}
}
}