2004 Microchip Technology Inc. DS51297C

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

 2004 Microchip Technology Inc.

DS51297C
MPLAB® C18
C COMPILER
LIBRARIES

2.1 A/D CONVERTER FUNCTIONS


The A/D peripheral is supported with the following functions:

TABLE 2-1: A/D CONVERTER FUNCTIONS


Function Description
BusyADC Is A/D converter currently performing a conversion?
CloseADC Disable the A/D converter.
ConvertADC Start an A/D conversion.
OpenADC Configure the A/D convertor.
ReadADC Read the results of an A/D conversion.
SetChanADC Select A/D channel to be used.

 2004 Microchip Technology Inc. DS51297C


Hardware Peripheral Functions
2.2.1 Function Descriptions

BusyADC
Function: Is the A/D converter currently performing a conversion?
Include: adc.h
Prototype: char BusyADC( void );
Remarks: This function indicates if the A/D peripheral is in the process of
converting a value.
Return Value: 1 if the A/D peripheral is performing a conversion.
0 if the A/D peripheral isn’t performing a conversion.
File Name: adcbusy.c

CloseADC
Function: Disable the A/D converter.
Include: adc.h
Prototype: void CloseADC( void );
Remarks: This function disables the A/D convertor and A/D interrupt mechanism.
File Name: adcclose.c

ConvertADC
Function: Starts the A/D conversion process.
Include: adc.h
Prototype: void ConvertADC( void );
Remarks: This function starts an A/D conversion. The BusyADC() function
may be used to detect completion of the conversion.
File Name: adcconv.c

OpenADC
PIC18CXX2, PIC18FXX2, PIC18FXX8, PIC18FXX39
Function: Configure the A/D convertor.
Include: adc.h
Prototype: void OpenADC( unsigned char config,
unsigned char config2 );
Arguments: config
A bitmask that is created by performing a bitwise AND operation (‘&’)
with a value from each of the categories listed below. These values are
defined in the file adc.h.
A/D clock source:
ADC_FOSC_2 FOSC / 2
ADC_FOSC_4 FOSC / 4
ADC_FOSC_8 FOSC / 8
ADC_FOSC_16 FOSC / 16
ADC_FOSC_32 FOSC / 32
ADC_FOSC_64 FOSC / 64
ADC_FOSC_RC Internal RC Oscillator
A/D result justification:
ADC_RIGHT_JUST Result in Least Significant bits
ADC_LEFT_JUST Result in Most Significant bits

 2004 Microchip Technology Inc. DS51297C-page


12
Hardware Peripheral Functions

OpenADC
PIC18CXX2, PIC18FXX2, PIC18FXX8, PIC18FXX39 (Continued)
A/D voltage reference source:
ADC_8ANA_0REF VREF+=VDD, VREF-=VSS,
All analog channels
ADC_7ANA_1REF AN3=VREF+, All analog
channels except AN3
ADC_6ANA_2REF AN3=VREF+, AN2=VREF
ADC_6ANA_0REF VREF+=VDD, VREF-=VSS
ADC_5ANA_1REF AN3=VREF+, VREF-=VSS
ADC_5ANA_0REF VREF+=VDD, VREF-=VSS
ADC_4ANA_2REF AN3=VREF+, AN2=VREF-
ADC_4ANA_1REF AN3=VREF+
ADC_3ANA_2REF AN3=VREF+, AN2=VREF-
ADC_3ANA_0REF VREF+=VDD, VREF-=VSS
ADC_2ANA_2REF AN3=VREF+, AN2=VREF-
ADC_2ANA_1REF AN3=VREF+
ADC_1ANA_2REF AN3=VREF+, AN2=VREF-,
AN0=A
ADC_1ANA_0REF AN0 is analog input
ADC_0ANA_0REF All digital I/O

config2
A bitmask that is created by performing a bitwise AND operation (‘&’)
with a value from each of the categories listed below. These values are
defined in the file adc.h.
Channel:
ADC_CH0 Channel 0
ADC_CH1 Channel 1
ADC_CH2 Channel 2
ADC_CH3 Channel 3
ADC_CH4 Channel 4
ADC_CH5 Channel 5
ADC_CH6 Channel 6
ADC_CH7 Channel 7
A/D Interrupts:
ADC_INT_ON Interrupts enabled
ADC_INT_OFF Interrupts disabled
Remarks: This function resets the A/D peripheral to the POR state and
configures the A/D-related Special Function Registers (SFRs)
according to the options specified.
File Name: adcopen.c
Code Example: OpenADC( ADC_FOSC_32 &
ADC_RIGHT_JUST &
ADC_1ANA_0REF,
ADC_CH0 &
ADC_INT_OFF );

 2004 Microchip Technology Inc. DS51297C-page


13
Hardware Peripheral Functions

OpenADC
PIC18C658/858, PIC18C601/801,
PIC18F6X20, PIC18F8X20
Function: Configure the A/D convertor.
Include: adc.h
Prototype: void OpenADC( unsigned char config,
unsigned char config2 );
Arguments: config
A bitmask that is created by performing a bitwise AND operation (‘&’)
with a value from each of the categories listed below. These values are
defined in the file adc.h.
A/D clock source:
ADC_FOSC_2 FOSC / 2
ADC_FOSC_4 FOSC / 4
ADC_FOSC_8 FOSC / 8
ADC_FOSC_16 FOSC / 16
ADC_FOSC_32 FOSC / 32
ADC_FOSC_64 FOSC / 64
ADC_FOSC_RC Internal RC Oscillator
A/D result justification:
ADC_RIGHT_JUST Result in Least Significant bits
ADC_LEFT_JUST Result in Most Significant bits
A/D port configuration:
ADC_0ANA All digital
ADC_1ANA analog:AN0 digital:AN1-AN15
ADC_2ANA analog:AN0-AN1 digital:AN2-AN15
ADC_3ANA analog:AN0-AN2 digital:AN3-AN15
ADC_4ANA analog:AN0-AN3 digital:AN4-AN15
ADC_5ANA analog:AN0-AN4 digital:AN5-AN15
ADC_6ANA analog:AN0-AN5 digital:AN6-AN15
ADC_7ANA analog:AN0-AN6 digital:AN7-AN15
ADC_8ANA analog:AN0-AN7 digital:AN8-AN15
ADC_9ANA analog:AN0-AN8 digital:AN9-AN15
ADC_10ANA analog:AN0-AN9 digital:AN10-AN15
ADC_11ANA analog:AN0-AN10 digital:AN11-AN15
ADC_12ANA analog:AN0-AN11 digital:AN12-AN15
ADC_13ANA analog:AN0-AN12 digital:AN13-AN15
ADC_14ANA analog:AN0-AN13 digital:AN14-AN15
ADC_15ANA All analog

config2
A bitmask that is created by performing a bitwise AND operation (‘&’)
with a value from each of the categories listed below. These values are
defined in the file adc.h.

 2004 Microchip Technology Inc. DS51297C-page


14
Hardware Peripheral Functions

OpenADC
PIC18C658/858, PIC18C601/801,
PIC18F6X20, PIC18F8X20 (Continued)
Channel:
ADC_CH0 Channel 0
ADC_CH1 Channel 1
ADC_CH2 Channel 2
ADC_CH3 Channel 3
ADC_CH4 Channel 4
ADC_CH5 Channel 5
ADC_CH6 Channel 6
ADC_CH7 Channel 7
ADC_CH8 Channel 8
ADC_CH9 Channel 9
ADC_CH10 Channel 10
ADC_CH11 Channel 11
ADC_CH12 Channel 12
ADC_CH13 Channel 13
ADC_CH14 Channel 14
ADC_CH15 Channel 15
A/D Interrupts:
ADC_INT_ON Interrupts enabled
ADC_INT_OFF Interrupts disabled
A/D voltage configuration:
ADC_VREFPLUS_VDD VREF+ = AVDD
ADC_VREFPLUS_EXT VREF+ = external
ADC_VREFMINUS_VDD VREF- = AVDD
ADC_VREFMINUS_EXT VREF- = external
Remarks: This function resets the A/D-related registers to the POR state and
then configures the clock, result format, voltage reference, port and
channel.
File Name: adcopen.c
Code Example: OpenADC( ADC_FOSC_32 &
ADC_RIGHT_JUST &
ADC_14ANA,
ADC_CH0 &
ADC_INT_OFF );

 2004 Microchip Technology Inc. DS51297C-page


15
Hardware Peripheral Functions

OpenADC
All Other Processors
Function: Configure the A/D convertor.
Include: adc.h
Prototype: void OpenADC(unsigned char config,
unsigned char config2 ,
unsigned char portconfig);
Arguments: config
A bitmask that is created by performing a bitwise AND operation (‘&’)
with a value from each of the categories listed below. These values are
defined in the file adc.h.
A/D clock source:
ADC_FOSC_2 FOSC / 2
ADC_FOSC_4 FOSC / 4
ADC_FOSC_8 FOSC / 8
ADC_FOSC_16 FOSC / 16
ADC_FOSC_32 FOSC / 32
ADC_FOSC_64 FOSC / 64
ADC_FOSC_RC Internal RC Oscillator
A/D result justification:
ADC_RIGHT_JUST Result in Least Significant bits
ADC_LEFT_JUST Result in Most Significant bits
A/D acquisition time select:
ADC_0_TAD 0 Tad
ADC_2_TAD 2 Tad
ADC_4_TAD 4 Tad
ADC_6_TAD 6 Tad
ADC_8_TAD 8 Tad
ADC_12_TAD 12 Tad
ADC_16_TAD 16 Tad
ADC_20_TAD 20 Tad
config2
A bitmask that is created by performing a bitwise AND operation (‘&’)
with a value from each of the categories listed below. These values are
defined in the file adc.h.
Channel:
ADC_CH0 Channel 0
ADC_CH1 Channel 1
ADC_CH2 Channel 2
ADC_CH3 Channel 3
ADC_CH4 Channel 4
ADC_CH5 Channel 5
ADC_CH6 Channel 6
ADC_CH7 Channel 7
ADC_CH8 Channel 8
ADC_CH9 Channel 9
ADC_CH10 Channel 10
ADC_CH11 Channel 11
ADC_CH12 Channel 12
ADC_CH13 Channel 13
ADC_CH14 Channel 14
ADC_CH15 Channel 15

 2004 Microchip Technology Inc. DS51297C-page


16
Hardware Peripheral Functions

OpenADC
All Other Processors (Continued)
A/D Interrupts:
ADC_INT_ON Interrupts enabled
ADC_INT_OFF Interrupts disabled
A/D voltage configuration:
ADC_VREFPLUS_VDD VREF+ = AVDD
ADC_VREFPLUS_EXT VREF+ = external
ADC_VREFMINUS_VDD VREF- = AVDD
ADC_VREFMINUS_EXT VREF- = external

portconfig
The value of portconfig is any value from 0 to 127 for the
PIC18F1220/1320 and 0 to 15 for the PIC18F2220/2320/4220/4320,
inclusive. This is the value of bits 0 through 6 or bits 0 through 3 of the
ADCON1 register, which are the port configuration bits.
Remarks: This function resets the A/D-related registers to the POR state and
then configures the clock, result format, voltage reference, port and
channel.
File Name: adcopen.c
Code Example: OpenADC( ADC_FOSC_32 &
ADC_RIGHT_JUST &
ADC_12_TAD,
ADC_CH0 &
ADC_INT_OFF, 15 );

ReadADC
Function: Read the result of an A/D conversion.
Include: adc.h
Prototype: int ReadADC( void );
Remarks: This function reads the 16-bit result of an A/D conversion.
Return Value: This function returns the 16-bit signed result of the A/D conversion.
Based on the configuration of the A/D converter (e.g., using the
OpenADC() function), the result will be contained in the Least
Significant or Most Significant bits of the 16-bit result.
File Name: adcread.c

 2004 Microchip Technology Inc. DS51297C-page


17
Hardware Peripheral Functions

SetChanADC
Function: Select the channel used as input to the A/D converter.
Include: adc.h
Prototype: void SetChanADC( unsigned char channel );
Arguments: channel
One of the following values (defined in
adc.h): ADC_CH0 Channel 0
ADC_CH1 Channel 1
ADC_CH2 Channel 2
ADC_CH3 Channel 3
ADC_CH4 Channel 4
ADC_CH5 Channel 5
ADC_CH6 Channel 6
ADC_CH7 Channel 7
ADC_CH8 Channel 8
ADC_CH9 Channel 9
ADC_CH10 Channel 10
ADC_CH11 Channel 11
Remarks: Selects the pin that will be used as input to the A/D converter.
File Name: adcsetch.c
Code Example: SetChanADC( ADC_CH0 );

2.2.2 Example Use of the A/D Converter Routines


#include
<p18C452.h>
#include
<adc.h>
#include
<stdlib.h>
#include
<delays.h>

int result;

void main( void )


{
// configure A/D convertor
OpenADC( ADC_FOSC_32 & ADC_RIGHT_JUST &
ADC_8ANA_0REF, ADC_CH0 & ADC_INT_OFF );

Delay10TCYx( 5 ); // Delay for 50TCY


ConvertADC(); // Start
conversion while( BusyADC() ); // Wait
for completion result = ReadADC();
// Read result
CloseADC(); // Disable A/D
converter
}

 2004 Microchip Technology Inc. DS51297C-page


18

You might also like