1.14.1 1.15.1 Analog-to-Digital Converter (ADC)

The 12-bit ADC Converter with Threshold Detect includes the following features:

  • Successive Approximation Register (SAR) Conversion

  • Conversion Speeds of up to 300 ksps

  • User-Selectable Resolution of 10 or 12 Bits

  • Up to 24 Analog Inputs (internal and external)

  • External Voltage Reference Input Pins

  • Unipolar Differential Sample-and-Hold Amplifier (SHA)

  • Automated Threshold Scan and Compare Operation to Pre-Evaluate Conversion Results

  • Selectable Conversion Trigger Source

  • Fixed-Length Configurable Conversion Result Buffer

  • Eight Options for Result Alignment and Encoding

  • Configurable Interrupt Generation

  • Operation during CPU Sleep and Idle modes

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 );
}

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_RESULT_BUFFER Enum Identifies which ADC buffer has to be read
ADC_INPUT_POSITIVE Enum Identifies possible positive 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