adc_test_12bit.c - Demonstrates reading the internal ADC in 12-bit mode and converting it to a voltageΒΆ

 
#include "pic24_all.h"
#include <stdio.h>

#define VREF 3.3  //assume Vref = 3.3 volts

int main (void) {
  uint16_t u16_adcVal;
  float f_adcVal;

  configBasic(HELLO_MSG);
  CONFIG_RA0_AS_ANALOG();
 

Configure A/D to sample AN0 for 31 Tad periods in 12-bit mode then perform a single conversion.

  configADC1_ManualCH0(RA0_AN, 31, 1);
  while (1) {
    u16_adcVal = convertADC1();   //get ADC value
    f_adcVal = u16_adcVal;
    f_adcVal = f_adcVal/4096.0 * VREF;  //convert to float in range 0.0 to VREF
    printf("ADC input: %4.2f V (0x%04x)\n", (double) f_adcVal, u16_adcVal);
    DELAY_MS(300);   //delay so that we do not flood the UART.
    doHeartbeat();
  } //end while(1)

}