



void adc_isr ( void ) interrupt 14 using 1{

   static xdata unsigned int accumulator[ANALOG_INPUTS] ={0};
                                       // Here's where we integrate the
                                       // ADC samples from input AIN0.0
   static xdata unsigned int_dec=INT_DEC;   // Integrate/decimate counter
                                       		// we post a new result when
                                       		// int_dec = 0
   unsigned char i;


  // adc1 conversion complete?
  if (ADCON1 & 0x08)  {
    // clear ADCI1 flag
    ADCON1 &= ~0x08;
    
    // read results from AD1DAT0 - AD1DAT3
    //the inputs are swapped in the hardware so swapp them to correct order ASAP
    accumulator[0] += AD1DAT1;  // Read ADC value and add to running total
    accumulator[1] += AD1DAT0;  // Read ADC value and add to running total
    accumulator[2] += AD1DAT3;  // Read ADC value and add to running total
    accumulator[3] += AD1DAT2;  // Read ADC value and add to running total   

  
    int_dec--;                       // Update decimation counter
                                       // when last of the analog inputs
                                       // sampled
   

    if (int_dec == 0){                  // If zero, then post result
      int_dec = INT_DEC;               // Reset counter
      for(i=0; i<ANALOG_INPUTS; i++) {
         gui_AIResult[-i-] = accumulator[-i-] >> 4; //Copy decimated values into Result 
                                               //as we have added the result 256 times
                                               //we can shift only 4 bits to the right 
                                               //in order to get our extra 4bits of resolution
                                               //if we shift it 8 bits then we get an average of 
                                               //the last 256 samples.
                                               
         accumulator[-i-] = 0;          // Reset accumulators
         
  
         
		}
    }
  }





