3.19.1 Analog-to-Digital Converter (ADC)

  • 10-bit 1 Msps rate with one Sample and Hold (S&H)

  • Up to 48 analog inputs

  • Can operate during Sleep mode

  • Flexible and independent ADC trigger sources

Using The Library

Interrupt mode:

#include <stddef.h>                     // Defines NULL
#include <stdbool.h>                    // Defines true
#include <stdlib.h>                     // Defines EXIT_FAILURE
#include "definitions.h"                // SYS function prototypes

#define ADC_VREF                (3.3f)
#define ADC_MAX_COUNT           (1023U)

uint16_t adc_count;
float input_voltage;
volatile bool result_ready = false;

// *****************************************************************************
// *****************************************************************************
// Section: Main Entry Point
// *****************************************************************************
// *****************************************************************************

void ADC_ResultHandler(uintptr_t context)
{
    /* Read the ADC result */
    adc_count = ADC_ResultGet(ADC_RESULT_BUFFER_0);   
    result_ready = true;
}

int main ( void )
{
    /* Initialize all modules */
    SYS_Initialize ( NULL );
    ADC_CallbackRegister(ADC_ResultHandler, (uintptr_t)NULL);
    
    printf("\n\r---------------------------------------------------------");
    printf("\n\r                    ADC Demo                 ");
    printf("\n\r---------------------------------------------------------\n\r");
    
    while (1)
    {
        /* Maintain state machines of all polled MPLAB Harmony modules. */
        SYS_Tasks ( );

        /* Auto sampling mode is used, so no code is needed to start sampling */
		
        /* Start ADC conversion in software */
        ADC_ConversionStart();

        /* Wait till ADC conversion result is available */
        if(result_ready == true)
        {
            result_ready = false;
            input_voltage = (float)adc_count * ADC_VREF / ADC_MAX_COUNT;
            printf("ADC Count = 0x%03x, ADC Input Voltage = %d.%02d V \r", adc_count, (int)input_voltage, (int)((input_voltage - (int)input_voltage)*100.0));
        }
    }

    /* Execution should not come here during normal operation */

    return ( EXIT_FAILURE );
}

Polling mode:

#include <stddef.h>                     // Defines NULL
#include <stdbool.h>                    // Defines true
#include <stdlib.h>                     // Defines EXIT_FAILURE
#include "definitions.h"                // SYS function prototypes

#define ADC_VREF                (3.3f)
#define ADC_MAX_COUNT           (1023U)

uint16_t adc_count;
float input_voltage;

// *****************************************************************************
// *****************************************************************************
// Section: Main Entry Point
// *****************************************************************************
// *****************************************************************************

int main ( void )
{
    /* Initialize all modules */
    SYS_Initialize ( NULL );
    
    printf("\n\r---------------------------------------------------------");
    printf("\n\r                    ADC Demo                 ");
    printf("\n\r---------------------------------------------------------\n\r");
    
    while (1)
    {
        /* Maintain state machines of all polled MPLAB Harmony modules. */
        SYS_Tasks ( );

        /* Auto sampling mode is used, so no code is needed to start sampling */
		
        /* Start ADC conversion in software */
        ADC_ConversionStart();

        /* Wait till ADC conversion result is available */
        while(!ADC_ResultIsReady())
        {

        };

        /* Read the ADC result */
        adc_count = ADC_ResultGet(ADC_RESULT_BUFFER_0);
        input_voltage = (float)adc_count * ADC_VREF / ADC_MAX_COUNT;

        printf("ADC Count = 0x%03x, ADC Input Voltage = %d.%02d V \r", adc_count, (int)input_voltage, (int)((input_voltage - (int)input_voltage)*100.0));
    }

    /* Execution should not come here during normal operation */

    return ( EXIT_FAILURE );
}

Library Interface

Peripheral library provides the following interfaces:

Functions

NameDescription
ADCx_InitializeInitializes ADC peripheral
ADCx_EnableEnable (turn ON) ADC module
ADCx_DisableDisable ADC module
ADC_SamplingStartStarts the sampling
ADCx_ConversionStartStarts the conversion
ADC_InputSelectSelects input for ADC
ADC_InputScanSelectSelects input for scanning ADC channels
ADC_ResultIsReadyReturns the status of the channel conversion
ADC_ResultGetProvides the ADC conversion result based on the buffer index
ADCx_CallbackRegisterRegisters the function to be called from interrupt

Data types and constants

NameTypeDescription
ADC_MUXEnumIdentifies which ADC Mux is to be configured
ADC_RESULT_BUFFEREnumIdentifies which ADC buffer has to be read
ADC_INPUT_POSITIVEEnumIdentifies possible positive input for ADC
ADC_INPUT_NEGATIVEEnumIdentifies possible negative input for ADC
ADC_INPUTS_SCANEnumIdentifies possible ADC inputs which can be scanned
ADC_CALLBACKTypedefDefines the data type and function signature for the ADC peripheral callback function