C:\> Rostislav Persion's Projects

.:: HI-TECH C Compiler ADC Example ::.
A library for HI-TECH C compiler for reading from ADC ports






Library for HI-TECH C Compiler for analog to digital (ADC) readings



   1:  static volatile unsigned char    ADRES    @ 0x1E;
   2:  static volatile unsigned char    ADCON0    @ 0x1F;
   3:  static bank1 unsigned char    ADCON1    @ 0x9F;
   4:  static volatile unsigned char    ADCON2    @ 0x9B;
   5:   
   6:  void ADCInit() { ADCON2=0b10001010; }
   7:   
   8:  unsigned int ADCRead(unsigned char ch)
   9:  {
  10:     if(ch>13) return 0;  //Invalid Channel
  11:     ADCON0=0x00;
  12:     ADCON0=(ch<<2);   //Select ADC Channel
  13:     ADON=1;  //switch on the adc module
  14:     ADCON0bits.GO_DONE=1;  //Start conversion
  15:     while(ADCON0bits.GO_DONE); //wait for the conversion to finish
  16:     ADON=0;  //switch off adc
  17:     return ADRES;
  18:  }