1.16.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

Name Description
ADCx_Initialize Initializes ADC peripheral
ADCx_Enable Enable (turn ON) ADC module
ADCx_Disable Disable ADC module
ADC_SamplingStart Starts the sampling
ADCx_ConversionStart Starts the conversion
ADC_InputSelect Selects input for ADC
ADC_InputScanSelect Selects input for scanning ADC channels
ADC_ResultIsReady Returns the status of the channel conversion
ADC_ResultGet Provides the ADC conversion result based on the buffer index
ADCx_CallbackRegister Registers the function to be called from interrupt

Data types and constants

Name Type Description
ADC_MUX Enum Identifies which ADC Mux is to be configured
ADC_RESULT_BUFFER Enum Identifies which ADC buffer has to be read
ADC_INPUT_POSITIVE Enum Identifies possible positive input for ADC
ADC_INPUT_NEGATIVE Enum Identifies possible negative input for ADC
ADC_INPUTS_SCAN Enum Identifies possible ADC inputs which can be scanned
ADC_CALLBACK Typedef Defines the data type and function signature for the ADC peripheral callback function