Jump to main content

##

# 3.9.2 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

Name | Description
--- | ---
ADCHS_Initialize | Initializes given instance of ADCHS peripheral
ADCHS_ModulesEnable | Enables the ADC modules
ADCHS_ModulesDisable | Disables the ADC modules
ADCHS_ChannelResultInterruptEnable | Enables the ADC channel result data interrupt
ADCHS_ChannelResultInterruptDisable | Disables the ADC channel result data interrupt
ADCHS_ChannelEarlyInterruptEnable | Enables the ADC channel early result data interrupt
ADCHS_ChannelEarlyInterruptDisable | Disables the ADC channel early result data interrupt
ADCHS_ChannelConversionStart | Starts the ADC conversion of an analog channel
ADCHS_GlobalEdgeConversionStart | Starts the ADC conversion of analog channels configured for global software edge trigger
ADCHS_GlobalLevelConversionStart | Starts the ADC conversion of analog channels configured for global software level trigger
ADCHS_GlobalLevelConversionStop | Stops the global software level trigger
ADCHS_ChannelResultIsReady | Returns the status of the channel conversion
ADCHS_EOSStatusGet | Returns the status of the end of scan interrupt flag
ADCHS_ChannelResultGet | Reads the conversion result of the channel
ADCHS_CallbackRegister | Registers the function to be called from interrupt
ADCHS_EOSCallbackRegister | Registers the function to be called from end of scan interrupt
ADCHS_DMASampleCountBaseAddrSet | Registers the user-defined RAM address at which the DMA engine will start saving the current count of output samples
ADCHS_DMAResultBaseAddrSet | Registers the user-defined RAM address at which the DMA engine will start saving the converted data
ADCHS_DMACallbackRegister | Registers the application event handler which is called when the DMA transfer is complete
ADCHS_DMAStatusGet | Returns the DMA transfer status
ADCHS_ComparatorxEnable | Enables comparator
ADCHS_ComparatorxDisable | Disables comparator
ADCHS_ComparatorxLimitSet | Sets lower and higher threshold for comparator
ADCHS_ComparatorxEventModeSet | Sets the condition under which the comparator output becomes true
ADCHS_ComparatorxAnalogInputIDGet | Identifies the analog input for which the comparator event is generated
ADCHS_ComparatorxCallbackRegister | Registers the function to be called from the ADC comparator interrupt
ADCHS_ComparatorxStatusGet | Returns the Comparator status
ADCHS_FilterxIsReady | Returns the status of ADC filter
ADCHS_FilterxDataGet | Returns the ADC filter data
ADCHS_FilterxCallbackRegister | Registers the function to be called from the ADC filter ready interrupt

Data types and constants

Name | Type | Description
--- | --- | ---
ADCHS_MODULE_MASK | Enum | Identifies dedicated and shared ADC module mask
ADCHS_CHANNEL_NUM | Enum | Identifies ADCHS inputs as analog channel number
ADCHS_CMP_EVENT_MODE | Enum | Identifies ADCHS comparator event modes
ADCHS_DMA_STATUS | Enum | Identifies ADCHS DMA status
ADCHS_CALLBACK | Typedef | Defines the function pointer data type and function signature for the adchs peripheral callback function
ADCHS_DC_CALLBACK | Typedef | Defines the function pointer data type and function signature for the adchs digital comparator interrupt callback function
ADCHS_DF_CALLBACK | Typedef | Defines the function pointer data type and function signature for the adchs digital filter interrupt callback function
ADCHS_DMA_CALLBACK | Typedef | Defines the function pointer data type and function signature for the adchs DMA interrupt callback function
ADCHS_EOS_CALLBACK | Typedef | Defines the function pointer data type and function signature for the adchs end of scan interrupt callback function
ADCHS_CALLBACK_OBJECT | Struct | ADCHS Callback structure
ADCHS_EOS_CALLBACK_OBJECT | Struct | ADCHS Callback structure for End of Scan (EOS) interrupt