ADC Library

ADC (Analog to Digital Converter) module is available with a number of ARM MCU modules. ADC is an electronic circuit that converts continuous signals to discrete digital numbers. ADC Library provides you a comfortable work with the module.

Library Routines

ADCx_Init

Prototype

void ADCx_Init();

Description

This routines configures ADC module.

The internal ADC module is set to:

  • single channel conversion
  • 10-bit conversion resolution for Stellaris devices, 12-bit resolution for ST devices.
  • unsigned integer data format
  • VRef+ : AVdd, VRef- : AVss

Parameters

None.

Returns

Nothing.

Requires
  • MCU with built-in ADC module.
Example
ADC1_Init();  // Initialize ADC module with default settings
Notes
  • ADC library routines require you to specify the module you want to use. To select the desired ADC module, simply change the letter x in the routine prototype for a number from 0 to 1.
  • Number of ADC modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

ADCx_Init_Advanced

Prototype

void ADCx_Init_Advanced(unsigned short _external_reference);

// for MCUs with selectable ADC resolution

void ADCx_Init_Advanced(unsigned short _external_reference, unsigned short _adc_resolution);

Description

This routine configures the internal ADC module to work with user defined settings.

Parameters
  • _external_reference: voltage reference used in ADC process :
    Value Description
    _ADC_EXTERNAL_REF External voltage reference.
    _ADC_EXTERNAL_REF_3V 3V external voltage reference.
    _ADC_EXTERNAL_REF_1V 1V external voltage reference.
    _ADC_INTERNAL_REF Internal voltage reference.
  • _external_reference: voltage reference used in ADC process :
    Value Description
    _ADC_RESOLUTION_10BIT 10-bit ADC resolution.
    _ADC_RESOLUTION_12BIT 12-bit ADC resolution..
Returns

Nothing.

Requires
  • The MCU with built-in ADC module.
Example
ADC1_Init_Advanced(_ADC_INTERNAL_REF);  // set internal reference used
Notes
  • Not all MCUs support advanced configuration. Please, read the appropriate datasheet before utilizing this library.
  • ADC library routines require you to specify the module you want to use. To select the desired ADC module, simply change the letter x in the routine prototype for a number from 0 to 1.
  • Number of ADC modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

ADC_Set_Input_Channel

Prototype // for Stellaris Cortex M3 devices and ST devices with 16 ADC channels or less :

void ADC_Set_Input_Channel(unsigned input_mask);

// for Stellaris Cortex M4 devices and ST devices with more than 16 ADC channels :

void ADC_Set_Input_Channel(unsigned long input_mask);

Description

The function sets appropriate ADC channel as an analog input.

Parameters
  • channel: represents the channel which will be set as an analog input:
    Value Description
    _ADC_CHANNEL_0 ADC channel 0.
    _ADC_CHANNEL_1 ADC channel 1.
    _ADC_CHANNEL_2 ADC channel 2.
    _ADC_CHANNEL_3 ADC channel 3.
    _ADC_CHANNEL_4 ADC channel 4.
    _ADC_CHANNEL_5 ADC channel 5.
    _ADC_CHANNEL_6 ADC channel 6.
    _ADC_CHANNEL_7 ADC channel 7.
    _ADC_CHANNEL_8 ADC channel 8.
    _ADC_CHANNEL_9 ADC channel 9.
    _ADC_CHANNEL_10 ADC channel 10.
    _ADC_CHANNEL_11 ADC channel 11.
    _ADC_CHANNEL_12 ADC channel 12.
    _ADC_CHANNEL_13 ADC channel 13.
    _ADC_CHANNEL_14 ADC channel 14.
    _ADC_CHANNEL_15 ADC channel 15.
    _ADC_CHANNEL_16 ADC channel 16.
    _ADC_CHANNEL_17 ADC channel 17.
    _ADC_CHANNEL_18 ADC channel 18.
    _ADC_CHANNEL_19 ADC channel 19.
    _ADC_CHANNEL_20 ADC channel 20.
    _ADC_CHANNEL_21 ADC channel 21.
    _ADC_CHANNEL_22 ADC channel 22.
    _ADC_CHANNEL_23 ADC channel 23.
    _ADC_CHANNEL_ALL All ADC channels.
Returns

Nothing.

Requires
  • The MCU with built-in ADC module.
Example
// sets ADC channels 0 and 1 as analog inputs
ADC_Set_Input_Channel(_ADC_CHANNEL_0 | _ADC_CHANNEL_1);
Notes
  • This routine is available only for MCUs with ADC module located on GPIO pins.
  • Refer to the appropriate Datasheet for the number of available channels.

ADCx_Get_Sample

Prototype

unsigned ADCx_Get_Sample(unsigned channel);

Description

The function enables ADC module and reads the specified analog channel input.

Parameters
  • channel: represents the channel from which the analog value is to be acquired :
    Value Description
    _ADC_CHANNEL_0 ADC channel 0.
    _ADC_CHANNEL_1 ADC channel 1.
    _ADC_CHANNEL_2 ADC channel 2.
    _ADC_CHANNEL_3 ADC channel 3.
    _ADC_CHANNEL_4 ADC channel 4.
    _ADC_CHANNEL_5 ADC channel 5.
    _ADC_CHANNEL_6 ADC channel 6.
    _ADC_CHANNEL_7 ADC channel 7.
    _ADC_CHANNEL_8 ADC channel 8.
    _ADC_CHANNEL_9 ADC channel 9.
    _ADC_CHANNEL_10 ADC channel 10.
    _ADC_CHANNEL_11 ADC channel 11.
    _ADC_CHANNEL_12 ADC channel 12.
    _ADC_CHANNEL_13 ADC channel 13.
    _ADC_CHANNEL_14 ADC channel 14.
    _ADC_CHANNEL_15 ADC channel 15.
    _ADC_CHANNEL_16 ADC channel 16.
    _ADC_CHANNEL_17 ADC channel 17.
    _ADC_CHANNEL_18 ADC channel 18.
    _ADC_CHANNEL_19 ADC channel 19.
    _ADC_CHANNEL_20 ADC channel 20.
    _ADC_CHANNEL_21 ADC channel 21.
    _ADC_CHANNEL_22 ADC channel 22.
    _ADC_CHANNEL_23 ADC channel 23.
    _ADC_CHANNEL_ALL All ADC channels.
Returns

10-bit unsigned value from the specified channel.

Requires
  • The MCU with built-in ADC module.
  • Prior to using this routine, appopriate ADC channels must be set as an analog input. Please see ADC_Set_Input_Channel.
  • Also, ADC module needs to be previously initialized. See ADCx_Init and ADCx_Init_Advanced.
Example
unsigned adc_value;
...
ADC_Set_Input_Channel(_ADC_CHANNEL_10); // Set ADC channel 10 as an analog input
ADC1_Init();                            // Initialize ADC module
adc_value = ADC1_Get_Sample(10);        // read analog value from ADC module channel 10
Notes
  • Refer to the appropriate Datasheet for the number of available channels.
  • ADC library routines require you to specify the module you want to use. To select the desired ADC module, simply change the letter x in the routine prototype for a number from 0 to 1.
  • Number of ADC modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

ADCx_Read

Prototype

unsigned ADCx_Read(unsigned channel, unsigned short _external_reference);

Description

The function sets appropriate ADC channel as an analog input, enables ADC module, initializes ADC module and reads the specified analog channel input.

Parameters
  • channel: represents the channel from which the analog value is to be acquired :
    Value Description
    _ADC_CHANNEL_0 ADC channel 0.
    _ADC_CHANNEL_1 ADC channel 1.
    _ADC_CHANNEL_2 ADC channel 2.
    _ADC_CHANNEL_3 ADC channel 3.
    _ADC_CHANNEL_4 ADC channel 4.
    _ADC_CHANNEL_5 ADC channel 5.
    _ADC_CHANNEL_6 ADC channel 6.
    _ADC_CHANNEL_7 ADC channel 7.
    _ADC_CHANNEL_8 ADC channel 8.
    _ADC_CHANNEL_9 ADC channel 9.
    _ADC_CHANNEL_10 ADC channel 10.
    _ADC_CHANNEL_11 ADC channel 11.
    _ADC_CHANNEL_12 ADC channel 12.
    _ADC_CHANNEL_13 ADC channel 13.
    _ADC_CHANNEL_14 ADC channel 14.
    _ADC_CHANNEL_15 ADC channel 15.
    _ADC_CHANNEL_16 ADC channel 16.
    _ADC_CHANNEL_17 ADC channel 17.
    _ADC_CHANNEL_18 ADC channel 18.
    _ADC_CHANNEL_19 ADC channel 19.
    _ADC_CHANNEL_20 ADC channel 20.
    _ADC_CHANNEL_21 ADC channel 21.
    _ADC_CHANNEL_22 ADC channel 22.
    _ADC_CHANNEL_23 ADC channel 23.
    _ADC_CHANNEL_ALL All ADC channels.
  • _external_reference: voltage reference used in ADC process :
    Value Description
    _ADC_EXTERNAL_REF External voltage reference.
    _ADC_INTERNAL_REF Internal voltage reference.
Returns

10-bit unsigned value from the specified channel.

Requires
  • The MCU with built-in ADC module.
Example
unsigned adc_value;
...
adc_value = ADC1_Read(10);    // read analog value from ADC1 module channel 10
Notes
  • Not all MCUs support external reference configuration. Please, read the appropriate datasheet before utilizing this library.
  • Refer to the appropriate Datasheet for the number of available channels.
  • ADC library routines require you to specify the module you want to use. To select the desired ADC module, simply change the letter x in the routine prototype for a number from 0 to 1.
  • Number of ADC modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • Since this routine enables, initializes and reads value from ADC module, its execution will be much slower than the ADCx_Get_Sample which only samples analog value.
    If the speed is a key factor, it is advisable to initialize the ADC module first, and then sample analog value, rather than using ADCx_Read routine.
Copyright (c) 2002-2012 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