Você está na página 1de 3

ADC Library

Page 1 of 3

ADC Library
mikroC PRO for PIC Libraries > Hardware Libraries >

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 ADC_Get_Sample ADC_Read

ADC_Init
Prototype Returns Description void ADC_Init(); Nothing. This routine initializes PICs 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.


// Initialize ADC module with default settings

Example

ADC_Init();

ADC_Get_Sample
Prototype Returns Description unsigned ADC_Get_Sample(unsigned short channel); 10 or 12-bit unsigned value read from the specified channel (MCU dependent). 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.

mk:@MSITStore:C:\Program%20Files\Mikroelektronika\mikroC%20PRO%20for%20PIC... 10/2/2011

ADC Library

Page 2 of 3

Parameters

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

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 Returns Description unsigned ADC_Read(unsigned short channel); 10 or 12-bit unsigned value read from the specified channel (MCU dependent). Initializes PICs 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 Example Nothing.
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 Clipboard

unsigned int temp_res; void main() { ANSEL = 0x04; ANSELH = 0; C1ON_bit = 0;

// Configure AN2 pin as analog // Configure other AN pins as digital I/O // Disable comparators

mk:@MSITStore:C:\Program%20Files\Mikroelektronika\mikroC%20PRO%20for%20PIC... 10/2/2011

ADC Library

Page 3 of 3

C2ON_bit = 0; TRISA TRISC TRISB = 0xFF; = 0; = 0; // PORTA is input // PORTC is output // PORTB is output

do { temp_res = ADC_Read(2); PORTB = temp_res; PORTC = temp_res >> 8; } while(1); }

// Get 10-bit results of AD conversion // Send lower 8 bits to PORTB // Send 2 most significant bits to RC1, RC0

HW Connection

ADC HW connection

Copyright (c) 2002-2011 mikroElektronika. All rights reserved. What do you think about this topic ? Send us feedback!

mk:@MSITStore:C:\Program%20Files\Mikroelektronika\mikroC%20PRO%20for%20PIC... 10/2/2011

Você também pode gostar