2.4 Analog to Digital Converter (ADCHS)

  • 12-bit Analog to Digital Converter

  • 18 Msps with up to six ADC circuits (five dedicated and one shared)

  • Up to 45 analog input

  • Can operate during Sleep and Idle modes

  • Multiple trigger sources

  • Six Digital Comparators and six Digital Filters

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           (4095)

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

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

void ADC_ResultHandler(ADCHS_CHANNEL_NUM channel, uintptr_t context)
{
    /* Read the ADC result */
    adc_count = ADCHS_ChannelResultGet(ADCHS_CH3);    
    result_ready = true;
}


int main ( void )
{
    /* Initialize all modules */
    SYS_Initialize ( NULL );
    
    printf("\n\r---------------------------------------------------------");
    printf("\n\r                    ADC Interrupt Mode Demo                 ");
    printf("\n\r---------------------------------------------------------\n\r"); 
    
    ADCHS_CallbackRegister(ADCHS_CH3, ADC_ResultHandler, (uintptr_t)NULL);
    TMR3_Start();
    
    while ( true )
    {
        /* Maintain state machines of all polled MPLAB Harmony modules. */
        SYS_Tasks ( );
        
        /* 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           (4095)

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 Polled Mode Demo                 ");
    printf("\n\r---------------------------------------------------------\n\r"); 
    
    TMR3_Start();
    
    while ( true )
    {
        /* Maintain state machines of all polled MPLAB Harmony modules. */
        SYS_Tasks ( );
        
        /* Wait till ADC conversion result is available */
        while(!ADCHS_ChannelResultIsReady(ADCHS_CH3))
        {

        };

        /* Read the ADC result */
        adc_count = ADCHS_ChannelResultGet(ADCHS_CH3);
        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

12-bit Analog to Digital Converter peripheral library provides the following interfaces:

Functions

NameDescription
ADCHS_InitializeInitializes given instance of ADCHS peripheral
ADCHS_ModulesEnableEnables the ADC modules
ADCHS_ModulesDisableDisables the ADC modules
ADCHS_ChannelResultInterruptEnableEnables the ADC channel result data interrupt
ADCHS_ChannelResultInterruptDisableDisables the ADC channel result data interrupt
ADCHS_ChannelEarlyInterruptEnableEnables the ADC channel early result data interrupt
ADCHS_ChannelEarlyInterruptDisableDisables the ADC channel early result data interrupt
ADCHS_ChannelConversionStartStarts the ADC conversion of an analog channel
ADCHS_GlobalEdgeConversionStartStarts the ADC conversion of analog channels configured for global software edge trigger
ADCHS_GlobalLevelConversionStartStarts the ADC conversion of analog channels configured for global software level trigger
ADCHS_GlobalLevelConversionStopStops the global software level trigger
ADCHS_ChannelResultIsReadyReturns the status of the channel conversion
ADCHS_EOSStatusGetReturns the status of the end of scan interrupt flag
ADCHS_ChannelResultGetReads the conversion result of the channel
ADCHS_CallbackRegisterRegisters the function to be called from interrupt
ADCHS_EOSCallbackRegisterRegisters the function to be called from end of scan interrupt
ADCHS_DMASampleCountBaseAddrSetRegisters the user-defined RAM address at which the DMA engine will start saving the current count of output samples
ADCHS_DMAResultBaseAddrSetRegisters the user-defined RAM address at which the DMA engine will start saving the converted data
ADCHS_DMACallbackRegisterRegisters the application event handler which is called when the DMA transfer is complete
ADCHS_DMAStatusGetReturns the DMA transfer status
ADCHS_ComparatorxEnableEnables comparator
ADCHS_ComparatorxDisableDisables comparator
ADCHS_ComparatorxLimitSetSets lower and higher threshold for comparator
ADCHS_ComparatorxEventModeSetSets the condition under which the comparator output becomes true
ADCHS_ComparatorxAnalogInputIDGetIdentifies the analog input for which the comparator event is generated
ADCHS_ComparatorxCallbackRegisterRegisters the function to be called from the ADC comparator interrupt
ADCHS_ComparatorxStatusGetReturns the Comparator status
ADCHS_FilterxIsReadyReturns the status of ADC filter
ADCHS_FilterxDataGetReturns the ADC filter data
ADCHS_FilterxCallbackRegisterRegisters the function to be called from the ADC filter ready interrupt

Data types and constants

NameTypeDescription
ADCHS_MODULE_MASKEnumIdentifies dedicated and shared ADC module mask
ADCHS_CHANNEL_NUMEnumIdentifies ADCHS inputs as analog channel number
ADCHS_CMP_EVENT_MODEEnumIdentifies ADCHS comparator event modes
ADCHS_DMA_STATUSEnumIdentifies ADCHS DMA status
ADCHS_CALLBACKTypedefDefines the function pointer data type and function signature for the adchs peripheral callback function
ADCHS_DC_CALLBACKTypedefDefines the function pointer data type and function signature for the adchs digital comparator interrupt callback function
ADCHS_DF_CALLBACKTypedefDefines the function pointer data type and function signature for the adchs digital filter interrupt callback function
ADCHS_DMA_CALLBACKTypedefDefines the function pointer data type and function signature for the adchs DMA interrupt callback function
ADCHS_EOS_CALLBACKTypedefDefines the function pointer data type and function signature for the adchs end of scan interrupt callback function
ADCHS_CALLBACK_OBJECTStructADCHS Callback structure
ADCHS_EOS_CALLBACK_OBJECTStructADCHS Callback structure for End of Scan (EOS) interrupt
Note: Not all APIs maybe implemented. See the specific device family section for available APIs.