ADC Library

ADC (Analog to Digital Converter) module is available with a number of AVR MCUs. Several library routines are included to provide you comfortable work with the module in single-ended mode.

Library Routines

ADC_Init

Prototype

void ADC_Init();

// for XMEGA family of MCUs

void ADCx_Init();

Returns

Nothing.

Description

Initializes internal ADC module to work with XTAL frequency prescaled by 128. Clock determines the time period necessary for performing A/D conversion.

For XMEGA family of MCUs change the X in the routine prototype with A or B.

Requires
  • MCU with built-in ADC module.
Example
ADC_Init();   // Initialize ADC module with default settings
ADCA_Init();  // Initialize ADC module with default settings

ADCx_Init_Advanced

Prototype

void ADC_Init_Advanced(char Reference);

// for XMEGA family of MCUs

void ADCx_Init_Advanced(unsigned AdcMode, unsigned Reference);

Returns

Nothing.

Description
  • Initializes internal ADC module to work with user defined settings. For XMEGA family, change the X in the routine prototype with A or B.

Parameters AdcMode and Reference determine the work mode for ADC module and can have the following values:

    Description Predefined library const
    Resolution of ADC module - XMEGA MCUs :
    8 bit resolution _ADC_8bit
    12 bit resolution _ADC_12bit
    Voltage Reference - XMEGA MCUs :
    Internal 1V _ADC_INTERNAL_REF_1V
    Internal VCC/1.6V _ADC_INTERNAL_REF_VCC
    External Reference on PORTA _ADC_EXTERNAL_REF_A
    External Reference on PORTB _ADC_EXTERNAL_REF_B
    Voltage Reference - other MCUs :
    External reference - Aref _ADC_EXTERNAL_REF
    Internal reference - AVcc _ADC_INTERNAL_REF
    Internal 0.55V _ADC_INTERNAL_0_55
    Internal 1.1V _ADC_INTERNAL_1_1
    Internal 1.5V _ADC_INTERNAL_1_5
    Internal 2.2V _ADC_INTERNAL_2_2
    Internal 2.5V _ADC_INTERNAL_2_5
    Internal 2.56V _ADC_INTERNAL_2_56
    Internal 4V _ADC_INTERNAL_4
    Internal 4.3V _ADC_INTERNAL_4_3
Requires
  • MCU with built-in ADC module.
Example
ADCA_Init_Advanced(_ADC_12bit, _ADC_INTERNAL_REF_1V);  // Initialize ADC module with 12bit resolution and internal voltage reference of 1V

ADC_Get_Sample

Prototype

unsigned ADC_Get_Sample(unsigned short channel);

// for XMEGA family of MCUs

unsigned ADCx_Get_Sample(unsigned short channel);

Returns

10-bit or 12-bit unsigned value (MCU dependent) from the specified channel.

Description

Routine acquires analog value from the specified channel.

Parameter channel represents the channel from which the analog value is to be acquired. Refer to the appropriate datasheet for channel-to-pin mapping.

For XMEGA family of MCUs change the X in the routine prototype with A or B.

Requires
  • The MCU with built-in ADC module.
  • Prior to using this routine, ADC module needs to be initialized. See ADC_Init.
Example
unsigned adc_value;
...
ADC_Init();                       // Initialize ADC module with default settings
adc_value = ADC_Get_Sample(2);    // Acquire analog value from channel 2

ADC_Read

Prototype

unsigned ADC_Read(char channel);

// for XMEGA family of MCUs

unsigned ADCx_Read(char channel);

Returns

10-bit or 12-bit unsigned value (MCU dependent) from the specified channel.

Description

Routine initializes internal ADC module and acquires analog value from the specified channel. Clock determines the time period necessary for performing A/D conversion.

Parameter channel represents the channel from which the analog value is to be acquired. Refer to the appropriate datasheet for channel-to-pin mapping.

For XMEGA family of MCUs change the X in the routine prototype with A or B.

Requires

Nothing.

Example
unsigned adc_value;
...
adc_value = ADC_Read(2);  // initialize ADC module and acquire analog value from ADC module channel 2

ADC_Set_Active

Prototype

void ADC_Set_Active(unsigned (*adc_gs)(unsigned short));

Description

Sets active ADC module.

Parameters

Parameters :

Returns

Nothing.

Requires

Routine is available only for MCUs with multiple ADC modules.

Used ADC module must be initialized before using this routine. See ADC_Init and ADCx_Init_Advanced routines.

Example
// Activate ADC2 module
ADC_Set_Active(&ADC2_Get_Sample); 
Notes

None.

Library Example

This example code reads analog value from channel 2 and displays it on PORTB and PORTC.

Copy Code To ClipboardCopy Code To Clipboard
#include <built_in.h>
unsigned int adc_rd;

void main() {

  DDRB = 0xFF;               // Set PORTB as output
  DDRC = 0xFF;               // Set PORTC as output
  
  while (1) {
    adc_rd = ADC_Read(2);    // get ADC value from 2nd channel
    PORTB = adc_rd;          // display adc_rd[7..0]
    PORTC = Hi(adc_rd);      // display adc_rd[9..8]
  }
}

HW Connection

ADC HW connection

ADC HW connection

Copyright (c) 2002-2017 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code