adc2pots1.c - Demonstrates 2-channel manual sampling with the ADCΒΆ

Performs a basic config of the ADC and samples two channels manually and sequentially with 12-bit results. Conversion results are printed to screen as both HEX values and voltages.

 
#include "pic24_all.h"
#include "stdio.h"
 

uncomment the next line to setup this project for a 12-bit ADC

#define USE_12BIT_ADC

#ifdef  USE_12BIT_ADC
#define   ADC_LEN           12
#define   ADC_NSTEPS        4096
#define   ADC_12BIT_FLAG    1
#else
#define   ADC_LEN           10
#define   ADC_NSTEPS        1024
#define   ADC_12BIT_FLAG    0
#endif

int main (void) {
  uint16_t  u16_pot1, u16_pot2;
  float   f_pot1, f_pot2;

  configBasic(HELLO_MSG);
 

make RA0/AN0/VREF+ a digital input to kill the pullup and set the TRISA bit, then make it ANALOG so the ADC will work

  CONFIG_RA0_AS_ANALOG();
  CONFIG_RA1_AS_ANALOG();

  while (1) {
    configADC1_ManualCH0(RA0_AN, 31, ADC_12BIT_FLAG );
    u16_pot1 = convertADC1();
    configADC1_ManualCH0(RA1_AN, 31, ADC_12BIT_FLAG );
    u16_pot2 = convertADC1();

    f_pot1 = 3.30 / ADC_NSTEPS * u16_pot1;
    f_pot2 = 3.30 / ADC_NSTEPS * u16_pot2;
    printf("AN0 is 0x%0X or %1.4fV. |  AN1 is 0x%0X or %1.4fV.\n",    \
           u16_pot1, (double) f_pot1, u16_pot2, (double) f_pot2 );
    DELAY_MS(1500);

  } //endof while()
} // endof main()