ADC Library

ADC (Analog to Digital Converter) module is available with a number of PIC MCUs. Library function ADC_Read is included to provide you comfortable work with the module.

Library Routines

ADC_Init

Prototype

procedure ADC_Init();

Returns

Nothing.

Description

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

ADC_Get_Sample

Prototype

function ADC_Get_Sample(channel : byte) : word

Returns

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
var adc_value : word
...
adc_value := ADC_Get_Sample(1);    // read analog value from ADC module channel 1

ADC_Read

Prototype

function ADC_Read(channel : byte) : word;

Returns

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 and acquires analog value from the specified channel. 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
var tmp : word;
...
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
program ADC_on_LEDs;
var temp_res : word;

  begin
    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
    TRISB  := 0;                          // PORTB is output
    TRISC  := 0;                          // PORTC is output

    while (TRUE) do
      begin
        temp_res := ADC_Read(2);          // Get 10-bit results of AD conversion
        PORTB := temp_res;                // Send lower 8 bits to PORTB
        PORTC := word(temp_res shr 8);    // Send 2 most significant bits to RC1, RC0
      end;
  end.

HW Connection

ADC HW connection

ADC HW connection

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