ADC Library

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

Returns

Nothing.

Description

This routine initializes PIC’s internal ADC module to work with RC clock. Clock determines the time period necessary for performing AD conversion (min 12TAD).

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

ADC_Init_Advanced

Prototype

void ADC_Init_Advanced(char reference);

Returns

Nothing.

Description

This routine initializes PIC’s internal ADC module to work with user defined voltage reference.

Parameters :
  • reference: voltage reference settings. Valid values can be OR-ed to form user-defined value.
Value Description
_ADC_INTERNAL_REF Both voltage references set to internal VDD and VSS.
_ADC_EXTERNAL_REF Both voltage references set to external Vref+ and Vref- pins.
_ADC_EXTERNAL_VREFL Low voltage reference set to Vref- pin.
_ADC_EXTERNAL_VREFH High voltage reference set to Vref+ pin.
_ADC_INTERNAL_VREFL Low voltage reference set to internal VSS.
_ADC_INTERNAL_VREFH High voltage reference set to internal VDD.
_ADC_INTERNAL_FVRH1 High voltage reference set to 1.024V.
_ADC_INTERNAL_FVRH2 High voltage reference set to 2.048V.
_ADC_INTERNAL_FVRH4 High voltage reference set to 4.096V.
_ADC_INTERNAL_FVR Both voltage references set to internal 4.096V and 2.048V, respectively.
_ADC_INTERNAL_FVRL2 Low voltage reference set to internal 2.048V
_ADC_INTERNAL_FVRH3 High voltage reference set to internal 3.072V
Requires
  • MCU with built-in ADC module.
  • Not all MCUs support advanced initialization and all initialization settings. Please refer to the respective datasheet.
Example
//  low voltage reference Vref- pin, high voltage reference 4.096V
ADC_Init_Advanced(_ADC_EXTERNAL_VREFL | _ADC_INTERNAL_FVRH4);

ADC_Get_Sample

Prototype

unsigned ADC_Get_Sample(unsigned short channel);

Returns

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

Description

The function aquires 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.

  Note : This function doesn't work with the external voltage reference source, only with the internal voltage reference.
Requires
  • The MCU with built-in ADC module.
  • Prior to using this routine, ADC module needs to be initialized. See ADC_Init.
  • Before using the function, be sure to configure the appropriate TRISx bits to designate pins as inputs.
Example
unsigned adc_value;
...
adc_value = ADC_Get_Sample(2);    // read analog value from ADC module channel 2

ADC_Read

Prototype

unsigned ADC_Read(unsigned short channel);

Returns

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

Description

Initializes PIC’s internal ADC module to work with RC clock. Clock determines the time period necessary for performing AD conversion (min 12TAD).

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

  Note : This function doesn't work with the external voltage reference source, only with the internal voltage reference.
Requires
  • The MCU with built-in ADC module.
  • Before using the function, be sure to configure the appropriate TRISx bits to designate pins as inputs.
Example
unsigned tmp;
...
tmp = ADC_Read(2);  // Read analog value from channel 2

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
unsigned int temp_res;

void main() {
  ANSEL  = 0x04;              // Configure AN2 pin as analog
  ANSELH = 0;                 // Configure other AN pins as digital I/O
  C1ON_bit = 0;               // Disable comparators
  C2ON_bit = 0;
  
  TRISA  = 0xFF;              // PORTA is input
  TRISC  = 0;                 // PORTC is output
  TRISB  = 0;                 // PORTB is output

  do {
    temp_res = ADC_Read(2);   // Get 10-bit results of AD conversion
    PORTB = temp_res;         // Send lower 8 bits to PORTB
    PORTC = temp_res >> 8;    // Send 2 most significant bits to RC1, RC0
  } while(1);
}

HW Connection

ADC HW connection

ADC HW connection

Copyright (c) 2002-2019 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