1.30.2 1.31.2 1.33.2 1.37.2 Analog Digital Converter (ADC)

  • 8-, 10- or 12-bit resolution

  • Up to 350,000 samples per second (350ksps)

  • Differential and single-ended inputs

  • Five internal inputs

  • 1/2x to 16x gain

  • Single, continuous and pin-scan conversion options

  • Windowing monitor with selectable channel

  • Built-in internal reference and external reference options

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

  • Optional DMA transfer of conversion result

  • Hardware gain and offset compensation

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

  • Selectable sampling time

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                (1650)   //1650 mV (1.65V)
#define DAC_COUNT_INCREMENT     (31U)    // equivalent to 0.1V (0.1 / (3.3 / ((2^10) - 1))) 
#define DAC_COUNT_MAX           (511)

uint16_t adc_count;
uint32_t input_voltage;
/* Initial value of dac count which is midpoint = 1.65 V*/
uint16_t dac_count = 0x100;   

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=0;    
    
    DAC_DataWrite(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");
    
    ADC_Enable();
    SYSTICK_TimerStart();
    EIC_CallbackRegister(EIC_PIN_15, switch_handler, (uintptr_t) NULL);
    DAC_DataWrite(dac_count);
    while (1)
    {
        /* Start ADC conversion */
        ADC_ConversionStart();

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

        };

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

        printf("ADC Count = 0x%x, ADC Input Voltage = %d.%03d V \r", adc_count, (int)(input_voltage/1000), (int)(input_voltage%1000));
        
        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_ComparisonWindowSet Returns the status of automatic sequence conversion
ADCx_WindowMonitorStatusGet Returns the status of the window monitor flag
ADCx_CallbackRegister Registers the function to be called from interrupt

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_CALLBACK Typedef Defines the data type and function signature for the ADC peripheral callback function