//
// ADC Example with interrupt
// By PEK '2004
//
// MCU: 16F877
// Clock: 10 MHz
//
// To be used with the CCS compiler
//

#include <16F877.h>
#device ADC=10  // Use 10 bits
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=10000000)
#use fast_io(B)

#INT_AD
void AD_isr()
{
   output_b(read_adc(ADC_READ_ONLY) >> 2);

   read_adc(ADC_START_ONLY);     // Start a new conversion
}

void main()
{
   // Setup Ports according to fast I/O
   output_b(0x00);
   set_tris_b(0x00);             // Set all pins as output

   // Setup A/D
   setup_adc_ports(RA0_ANALOG);  // Only RA0 as analog input
   setup_adc(ADC_CLOCK_DIV_32);  // Divide clock by 32
   set_adc_channel(0);           // Channel 0
   delay_us(100);                // Wait A/D acquisition time

   // Enable interrupts
   enable_interrupts(INT_AD);    // Enable A/D interrupts
   enable_interrupts(GLOBAL);    // Enable global interrupts

   read_adc(ADC_START_ONLY);     // Start conversion

   while(1);
}

