1.32.2 Analog Digital Converter (ADC)

  • 8-bit, 10-bit or 12-bit resolution

  • Up to 1,000,000 samples per second (1MSPS)

  • Differential and single-ended inputs

  • Five Internal inputs

  • Single, continuous, and sequencing options

  • Windowing monitor with selectable channel

  • Conversion range: Vref = [1.0V to VDDANA]

  • Built-in internal reference and external reference options

  • Event-triggered conversion for accurate timing (one event input)

  • Optional DMA transfer of conversion settings or result

  • Hardware gain and offset compensation

  • Averaging and oversampling with decimation to support up to 16-bit result

  • Selectable sampling time

  • Flexible Power or Throughput rate management

Using The Library

ADC Sample:

#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

#define ADC_VREF                (3.3f)
#define DAC_COUNT_INCREMENT     (170U)  // Equivalent to 0.1V with the given DAC reference 
#define DAC_COUNT_MAX           (4095U)

uint16_t adc_count;
float input_voltage;
/* Initial value of dac count which is midpoint = 1.2 V*/
uint16_t dac_count = 2048;   

void switch_handler(uintptr_t context )
{
    /* Write next data sample */
    dac_count = dac_count + DAC_COUNT_INCREMENT;
    
    if (dac_count > DAC_COUNT_MAX)
            dac_count = 2048;    
    
    DAC_DataWrite(DAC_CHANNEL_0, dac_count);
}

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

int main ( void )
{
    /* Initialize all modules */
    SYS_Initialize ( NULL );
    
    printf("\n\r---------------------------------------------------------");
    printf("\n\r                    ADC Sample Demo                 ");
    printf("\n\r---------------------------------------------------------\n\r");
    
    ADC0_Enable();
    SYSTICK_TimerStart();
    EIC_CallbackRegister(EIC_PIN_15, switch_handler, (uintptr_t) NULL);
    DAC_DataWrite(DAC_CHANNEL_0, dac_count);
    while (1)
    {
        /* Start ADC conversion */
        ADC0_ConversionStart();

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

        };

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

        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));
        
        SYSTICK_DelayMs(500);
    }

    /* Execution should not come here during normal operation */

    return ( EXIT_FAILURE );
}

Library Interface

Analog Digital Converter peripheral library provides the following interfaces:

Functions

Name Description
ADCx_Initialize Initializes given instance of ADC peripheral
ADCx_Enable Enables given instance of ADC peripheral
ADCx_Disable Disables given instance of ADC peripheral
ADCx_ChannelSelect Selects ADC input channel
ADCx_ConversionStart Starts the ADC conversion with the software trigger
ADCx_ConversionStatusGet Returns the status of the conversion of the channel
ADCx_ConversionResultGet Returns the conversion result of the channel
ADCx_LastConversionResultGet Returns the result for the previous adc conversion of the channel
ADCx_WindowMonitorStatusGet Returns the status of the window monitor flag
ADCx_ComparisonWindowSet This function sets the window upper threshold and lower threshold values
ADCx_WindowModeSet This function configures the window comparison mode
ADCx_CallbackRegister Registers the function to be called from interrupt
ADCx_InterruptsClear This function clears the interrupt flags
ADCx_InterruptsEnable This function enables the interrupts
ADCx_InterruptsDisable This function disables the interrupts

Data types and constants

Name Type Description
ADC_STATUS Enum Identifies ADC interrupt flag status
ADC_POSINPUT Enum Identifies ADC positive input pin to select
ADC_NEGINPUT Enum Identifies ADC negative input pin to select
ADC_WINMODE Enum Identifies ADC window comparison modes
ADC_CALLBACK Typedef Defines the data type and function signature for the ADC peripheral callback function