1.35.1 Analog-to-Digital Converter (ADC)

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

  • 500 ksps Conversion Rate

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

  • Selectable Single-Ended or Differential Input Voltage

  • Integrated Multiplexer Offering Up to 8 Independent Analog Inputs

  • Individual Enable and Disable of Each Channel

  • Hardware or Software Trigger

  • Two Sleep Modes

  • Channel Sequence Customization

  • 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 - PA17 - Connect to Vcc
 ADC CH1 - PA18 - connect to Vcc
 ADC_CH2 - PA19 - Connect to GND
 *****************************************************/

#define ADC_VREF               (3.3f)

uint16_t adc_ch0_count, adc_ch1_count, adc_ch2_count;

float adc_ch0_voltage, adc_ch1_voltage, adc_ch2_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_ch2_count = ADC_ChannelResultGet(ADC_CH2);
    adc_ch0_count = ADC_ChannelResultGet(ADC_CH0);
    adc_ch1_count = ADC_ChannelResultGet(ADC_CH1);
       
    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  CH1 Count  CH1 Voltage  CH2 Count  CH2 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_ch2_voltage = (float)adc_ch2_count * ADC_VREF/4095U;
            adc_ch0_voltage = (float)adc_ch0_count * ADC_VREF/4095U;
            adc_ch1_voltage = (float)adc_ch1_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_ch1_count, adc_ch1_voltage, adc_ch2_count, adc_ch2_voltage);
                           
                
            result_ready = false;
 
            SYSTICK_DelayMs(2000);

            /* 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 - PA17 - 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);    
        
        SYSTICK_DelayMs(1000);
    }
    
    /* 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_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