Adc.c51


1
#pragma cd
2
#include "regcc01.h"
3
4
#define uchar  unsigned char
5
#define uint  unsigned int
6
7
#define XTAL    20e6
8
#define ADC_CLOCK  700e3    // max 700kHz
9
10
#define ADC_CHANNELS  8    // 1 .. 8
11
#define SAMPLE_COUNT  16    // 16 samples for sliding average
12
13
int idata adc_input[ADC_CHANNELS];
14
15
16
void adc_conversion( void )
17
{
18
  static unsigned char channel;
19
20
  ADCF = (uchar)((int)0xFF00 >> ADC_CHANNELS);
21
  ADCLK = (uchar)(XTAL / ADC_CLOCK / 4 + 1);    // 11 * clock = 17æs
22
23
  EADC = 1;
24
  EA = 1;
25
26
  if( ++channel >= ADC_CHANNELS )
27
    channel = 0;
28
29
  ADCON = ADCON_SCH_ & (channel + 8 - ADC_CHANNELS) 
30
  | ADCON_ADEN_           // enable ADC
31
  | ADCON_PSIDLE_         // idle during conv.
32
  | ADCON_ADSST_;          // start conversion
33
34
  adc_input[channel] -= adc_input[channel] / SAMPLE_COUNT
35
         - (((uint)ADDH << 2) | (ADDL & 0x03));
36
}
37
38
39
void adc_interrupt( void ) interrupt INT_ADC
40
{
41
  ADCON &= ~(ADCON_PSIDLE_ | ADCON_ADEOC_ | ADCON_ADSST_);
42
            // clear  End of Conversion
43
}