1.25.1 Analog-to-Digital Converter (ADC)

  • 12-bit Resolution with Enhanced Mode up to 14 bits

  • 1 Msps Conversion Rate

  • Digital Averaging Function providing Enhanced Resolution Mode up to 14 bits

  • Wide Range of Power Supply Operation

  • Selectable Single-Ended or Differential Input Voltage

  • Integrated Multiplexer Offering Up to 12 Independent Analog Inputs

  • Individual Enable and Disable of Each Channel

  • Hardware or Software Trigger

  • Drive of PWM Fault Input

  • DMA Support

  • Two Sleep Modes

  • Channel Sequence Customizing

  • Automatic Window Comparison of Converted Values

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

/*****************************************************
 ADC CH0 - PD19 - Connect to Vcc
 ADC CH5 - PD24 - connect to Vcc
 ADC_CH6 - PD25 - Connect to GND
 *****************************************************/

#define ADC_VREF               (3.3f)

uint16_t adc_ch0_count, adc_ch5_count, adc_ch6_count;

float adc_ch0_voltage, adc_ch5_voltage, adc_ch6_voltage;

volatile bool result_ready = false;

/* This function is called after conversion of last channel in the user sequence */
void ADC_EventHandler(uint32_t status, uintptr_t context)
{
    /* Read the result of 3 channels*/
    adc_ch5_count = ADC_ChannelResultGet(ADC_CH5);
    adc_ch6_count = ADC_ChannelResultGet(ADC_CH6);
    adc_ch0_count = ADC_ChannelResultGet(ADC_CH0);
       
    result_ready = true;

}

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

int main ( void )
{
    /* Initialize all modules */
    SYS_Initialize ( NULL );
    
    /* Register callback function for ADC end of conversion interrupt*/
    ADC_CallbackRegister(ADC_EventHandler, (uintptr_t)NULL);
    
    printf("\n\r---------------------------------------------------------");
    printf("\n\r                    ADC User Sequence Demo                 ");
    printf("\n\r---------------------------------------------------------\n\r");
    printf("CH0 Count  CH0 Voltage  CH5 Count  CH5 Voltage  CH6 Count  CH6 Voltage \n\r");           

    /* Start ADC conversion */
    ADC_ConversionStart();

    while ( true )
    {
        /* Check if result is ready to be transmitted to console */
        if (result_ready == true)
        {
            adc_ch5_voltage = (float)adc_ch5_count * ADC_VREF/4095U;
            adc_ch6_voltage = (float)adc_ch6_count * ADC_VREF/4095U;
            adc_ch0_voltage = (float)adc_ch0_count * ADC_VREF/4095U;
            printf("0x%03x      %0.2f V       0x%03x      %0.2f V       0x%03x      %0.2f V \t\r",
                    adc_ch0_count, adc_ch0_voltage, adc_ch5_count, adc_ch5_voltage, adc_ch6_count, adc_ch6_voltage);
                           
                
            result_ready = false;
 
            PIT_DelayMs(500);

            /* Start ADC conversion */
            ADC_ConversionStart();
        }
    }

    /* 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 <stdio.h>
#include "definitions.h"                // SYS function prototypes

/*****************************************************
 ADC CH0 - PD19 - Connect to Vcc or GND
 *****************************************************/

#define ADC_VREF               (3.3f)

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)
    {
        /* Start ADC conversion */
        ADC_ConversionStart();

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

        };

        /* Read the ADC result */
        adc_count = ADC_ChannelResultGet(ADC_CH0);
        input_voltage = (float)adc_count * ADC_VREF / 4095U;

        printf("ADC Count = 0x%03x, ADC Input Voltage = %0.2f V \r",adc_count, input_voltage);    
        
        PIT_DelayMs(500);
    }
    
    /* Execution should not come here during normal operation */

    return ( EXIT_FAILURE );
}

Library Interface

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

Functions

Name Description
ADCx_Initialize Initializes given instance of ADC peripheral
ADC_ChannelsEnable Enables the ADC channels
ADC_ChannelsDisable Disables the ADC channels
ADC_ChannelsInterruptEnable Enables the ADC interrupt sources
ADC_ChannelsInterruptDisable Disables the ADC interrupt sources
ADCx_ConversionStart Starts the ADC conversion of all the enabled channels with the software trigger
ADC_ChannelResultIsReady Returns the status of the channel conversion
ADC_ChannelResultGet Reads the conversion result of the channel
ADC_ConversionSequenceSet Sets the user sequence of the channel conversion
ADCx_ComparisonWindowSet Sets Low threshold and High threshold in comparison window
ADC_ComparisonEventResultIsReady Returns the status of the Comparison event
ADC_ComparisonRestart Restart the comparison function
ADC_SleepModeEnable This function enables ADC sleep mode
ADC_SleepModeDisable This function disables ADC sleep mode
ADC_FastWakeupEnable This function enables ADC Fast Wakeup
ADC_FastWakeupDisable This function disables ADC Fast Wakeup
ADCx_CallbackRegister Registers the function to be called from interrupt

Data types and constants

Name Type Description
ADC_CHANNEL_MASK Enum Identifies ADC channel mask
ADC_CHANNEL_NUM Enum Identifies ADC channel number
ADC_INTERRUPT_MASK Enum Identifies interrupt sources number
ADC_CALLBACK Typedef Defines the data type and function signature for the ADC peripheral callback function
ADC_CALLBACK_OBJECT Struct ADC Callback structure