ADC Library

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

ADC1_Init

Prototype

procedure ADC1_Init();

Description

This routines configures ADC module to work with default settings.

The internal ADC module is set to:

  • single channel conversion
  • 10-bit conversion resolution
  • unsigned integer data format
  • auto-convert
  • VRef+ : AVdd, VRef- : AVss
  • instruction cycle clock
  • conversion clock : 32*Tcy
  • auto-sample time : 31TAD

Parameters

None.

Returns

Nothing.

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

None.

ADC1_Init_Advanced

Prototype

procedure ADC1_Init_Advanced(Reference : word);

Description

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

Parameters
  • Reference: voltage reference used in ADC process.
    Description Predefined library const
    Voltage reference:
    Internal voltage reference _ADC_INTERNAL_REF
    External voltage reference _ADC_EXTERNAL_REF
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.

ADC1_Get_Sample

Prototype

function ADC1_Get_Sample(channel : word) : word;

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.
Returns

10-bit unsigned value from the specified channel.

Requires
  • The MCU with built-in ADC module.
  • Prior to using this routine, ADC module needs to be initialized. See ADC1_Init and ADC1_Init_Advanced.
  • Before using the function, be sure to configure the appropriate TRISx bits to designate pins as inputs.
Example
var adc_value : word;
...
adc_value = ADC1_Get_Sample(10);    // read analog value from ADC module channel 10
Notes
  • The function sets the appropriate bit in the AD1PCFG registers to enable analog function of the chosen pin.
  • Refer to the appropriate Datasheet for channel-to-pin mapping.

ADC1_Read

Prototype

function ADC1_Read(channel : word) : word;

Description

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

Parameters
  • channel represents the channel from which the analog value is to be acquired.
Returns

10-bit unsigned value from the specified channel.

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 adc_value : word;
...
adc_value = ADC1_Read(10);    // read analog value from ADC module channel 10
Notes
  • This is a standalone routine, so there is no need for a previous initialization of ADC module.
  • The function sets the appropriate bit in the ADPCFG registers to enable analog function of the chosen pin.
  • Refer to the appropriate Datasheet for channel-to-pin mapping.

Library Example

This code snippet reads analog value from the channel 1 and sends readings as a text over UART1.

Copy Code To ClipboardCopy Code To Clipboard
program Temperature_Sensor;

// LCD module connections
var LCD_RS : sbit  at LATB2_bit;
var LCD_EN : sbit  at LATB3_bit;
var LCD_D4 : sbit  at LATB4_bit;
var LCD_D5 : sbit  at LATB5_bit;
var LCD_D6 : sbit  at LATB6_bit;
var LCD_D7 : sbit  at LATB7_bit;

var LCD_RS_Direction : sbit at TRISB2_bit;
var LCD_EN_Direction : sbit at TRISB3_bit;
var LCD_D4_Direction : sbit at TRISB4_bit;
var LCD_D5_Direction : sbit at TRISB5_bit;
var LCD_D6_Direction : sbit at TRISB6_bit;
var LCD_D7_Direction : sbit at TRISB7_bit;
// End LCD module connections

var temp : real;
    txt : array[20] of char;

// Convert ADC value to Celsius degrees format
function ADC_to_degC() : real;
  begin
    result := ADC1_Get_Sample(8);         // Read ADC value from AN8 pin
    result := (((3.25/1024) * result - 0.5) * 100);
  end;

begin
  CHECON := 0x32;
  AD1PCFG := 0xFFF7;                      // Configure AN8 pin as analog I/O
  ADC1_Init();                            // Initialize ADC
  Delay_100ms();
  Lcd_Init();                             // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);                    // Clear LCD
  Lcd_Cmd(_LCD_CURSOR_OFF);               // Turn cursor off
  Lcd_Out(1, 1, ' Temperature:   ');

  while(TRUE) do
  begin
    temp := ADC_to_degC();                // Convert ADC value to Celsius degrees format
    FloatToStr(temp, txt);
    Lcd_Chr(2,13,223);                    // Print degree character, 'C' for Centigrades
                                          // Different LCD displays have different char code for degree
    Lcd_Chr(2,14,'C');                    // If you see greek alpha letter try typing 178 instead of 223
    Lcd_Out(2, 5, txt);                   // Display value on the LCD
    Delay_1sec();                         // 1 second delay
  end;
end.

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