ADC Library

ADC (Analog to Digital Converter) module is available with a number of 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

ADC_Init

Prototype

void ADC_Init();

Description

This routines configures ADC module.

The internal ADC module is set to:

  • single conversion mode,
  • internal voltage reference.

Parameters

None.

Returns

Nothing.

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

None.

ADC_Init_Advanced

Prototype

void ADC_Init_Advanced(unsigned short mode);

Description

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

Parameters
  • mode: mode used in ADC process :
    Value Description
    _ADC_MODE_SINGLE Single conversion mode.
    _ADC_MODE_CONTINUOUS Continuos conversion mode.
Returns

Nothing.

Requires
  • The MCU with built-in ADC module.
Example
ADC_Init_Advanced(_ADC_MODE_CONTINUOUS);  // set continuous conversion mode
Notes

None.

ADC_Set_Input_Channel

Prototype

void ADC_Set_Input_Channel(unsigned 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_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
  • Refer to the appropriate Datasheet for the number of available channels.

ADC_Get_Sample

Prototype

unsigned int ADC_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_ALL All ADC channels.
Returns

ADC 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 ADC_Init and ADC_Init_Advanced.
Example
unsigned adc_value;
...
ADC_Set_Input_Channel(_ADC_CHANNEL_1); // Set ADC channel 1 as an analog input
ADC_Init();                             // Initialize ADC module
adc_value = ADC_Get_Sample(_ADC_CHANNEL_1);         // read analog value from ADC module channel 1
Notes
  • Refer to the appropriate Datasheet for the number of available channels.

ADC_Read

Prototype

unsigned int ADC_Read(unsigned char channel);

Description

The function sets appropriate ADC channel as an analog input, enables ADC module, initializes ADC module and reads from 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_ALL All ADC channels.
Returns

ADC value from the specified channel.

Requires
  • The MCU with built-in ADC module.
Example
unsigned adc_value;
...
adc_value = ADC_Read(_ADC_CHANNEL_1);    // read analog value from ADC module channel 1
Notes
  • Refer to the appropriate Datasheet for the number of available channels.
  • Since this routine enables, initializes and reads value from ADC module, its execution will be much slower than the ADC_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 ADC_Read routine.
Copyright (c) 2002-2015 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