2016年7月27日 星期三

pic18f4620 adc 研究

Ref : 18f4620 spec

Ref : http://www.jgorasia.com/Files/UBWdemos/Documentation/periph-lib/AD%20Converter.htm



REGISTER 19-1: ADCON0: A/D CONTROL REGISTER 0
U-0 U-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0
— — CHS3 CHS2 CHS1 CHS0 GO/DONE ADON
bit 7 bit 0
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
bit 7-6 Unimplemented: Read as ‘0’
bit 5-2 CHS3:CHS0: Analog Channel Select bits
0000 = Channel 0 (AN0)
0001 = Channel 1 (AN1)
0010 = Channel 2 (AN2)
0011 = Channel 3 (AN3)
0100 = Channel 4 (AN4)
0101 = Channel 5 (AN5)(1,2)
0110 = Channel 6 (AN6)(1,2)
0111 = Channel 7 (AN7)(1,2)
1000 = Channel 8 (AN8)
1001 = Channel 9 (AN9)
1010 = Channel 10 (AN10)
1011 = Channel 11 (AN11)
1100 = Channel 12 (AN12)
1101 = Unimplemented)(2)
1110 = Unimplemented)(2)
1111 =



bit 1 GO/DONE: A/D Conversion Status bit
When ADON = 1:
1 = A/D conversion in progress
0 = A/D Idle
bit 0 ADON: A/D On bit
1 = A/D Converter module is enabled
0 = A/D Converter module is disabled


============================================================

REGISTER 19-2: ADCON1: A/D CONTROL REGISTER 1
U-0 U-0 R/W-0 R/W-0 R/W-0 R/W-q(1) R/W-q(1) R/W-q(1)
— — VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0
bit 7 bit 0


bit 5 VCFG1: Voltage Reference Configuration bit (VREF- source)
1 = VREF- (AN2)
0 = VSS
bit 4 VCFG0: Voltage Reference Configuration bit (VREF+ source)
1 = VREF+ (AN3)
0 = VDD







============================================================

ADCON2: A/D CONTROL REGISTER 2
R/W-0 U-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0
ADFM — ACQT2 ACQT1 ACQT0 ADCS2 ADCS1 ADCS0
bit 7 bit 0



bit 7 ADFM: A/D Result Format Select bit
1 = Right justified
0 = Left justified
bit 6 Unimplemented: Read as ‘0’
bit 5-3 ACQT2:ACQT0: A/D Acquisition Time Select bits
111 = 20 TAD
110 = 16 TAD
101 = 12 TAD
100 = 8 TAD
011 = 6 TAD
010 = 4 TAD
001 = 2 TAD
000 = 0 TAD(1)
bit 2-0 ADCS2:ADCS0: A/D Conversion Clock Select bits
111 = FRC (clock derived from A/D RC oscillator)(1)
110 = FOSC/64
101 = FOSC/16
100 = FOSC/4
011 = FRC (clock derived from A/D RC oscillator)(1)
010 = FOSC/32
001 = FOSC/8
000 = FOSC/2






The following steps should be followed to perform an A/D
conversion:

1. Configure the A/D module:
• Configure analog pins, voltage reference and
digital I/O (ADCON1)
• Select A/D input channel (ADCON0)
• Select A/D acquisition time (ADCON2)
• Select A/D conversion clock (ADCON2)
• Turn on A/D module (ADCON0)
2. Configure A/D interrupt (if desired):
• Clear ADIF bit
• Set ADIE bit
• Set GIE bit
3. Wait the required acquisition time (if required).
4. Start conversion:
• Set GO/DONE bit (ADCON0 register)

5. Wait for A/D conversion to complete, by either:
• Polling for the GO/DONE bit to be cleared
OR
• Waiting for the A/D interrupt
6. Read A/D Result registers (ADRESH:ADRESL);
clear bit ADIF, if required.
7. For next conversion, go to step 1 or step 2, as
required. The A/D conversion time per bit is
defined as TAD. A minimum wait of 2 TAD is
required before the next acquisition starts.


=======================================================


TAD vs. DEVICE OPERATING FREQUENCIES
AD Clock Source (TAD) Maximum Device Frequency
Operation ADCS2:ADCS0 PIC18F2X20/4X20 PIC18LF2X20/4X20(4)
2 TOSC 000 2.86 MHz 1.43 kHz
4 TOSC 100 5.71 MHz 2.86 MHz
8 TOSC 001 11.43 MHz 5.72 MHz
16 TOSC 101 22.86 MHz 11.43 MHz
32 TOSC 010 40.0 MHz 22.86 MHz
64 TOSC 110 40.0 MHz 22.86 MHz
RC(3) x11 1.00 MHz(1) 1.00 MHz(2)



=======================================================


 Introduction

The A/D peripheral is supported with the following functions:
TABLE:             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 converter.
ReadADC
Read the results of an A/D conversion.
SetChanADC
Select A/D channel to be used.
SelChanConvADC
Select A/D channel to be used and start an A/D conversion.





Based on the different control registers, configuration bits and their positions in the control register, all PIC18 devices are divided into the following different versions. Wherever required, separate functions have been designed to support these versions, so before calling the LIB functions care has to be taken to know the version of the configured device and to call the appropriate function with correct number of arguments. For ADC_V11 user has to configure the I/O registers.
Below is the table to find the ADC version for the configured device:

TABLE: VERSION vs. DEVICES
Version name
Device number
ADC_V1
18C242, 18C252, 18C442, 18C452, 18F242, 18F252,
18F442, 18F452, 18F248 18F258, 18F448, 18F458,
18F2439, 18F2539, 18F4439, 18F4539
ADC_V2
18C601, 18C801, 18C658, 18C858, 18F6620, 18F6720,
18F8620, 18F8720, 18F6520, 18F8520
ADC_V3
18F1220, 18F1320
ADC_V4
18F1230, 18F1330
ADC_V5
18F2220, 18F2320, 18F4220, 18F4320, 18F2420,
18F2520, 18F4420, 18F4520, 18F2423, 18F2523,
18F4423, 18F4523, 18F2455, 18F2550, 18F4455,
18F4550, 18F2410, 18F2510, 18F2515, 18F2610,
18F4410, 18F4510, 18F4515, 18F4610, 18F2525,
18F2620, 18F4525, 18F4620, 18F6310, 18F6410
18F8310, 18F8410, 18F6390, 18F6490, 18F8390,
18F8490, 18F6527, 18F6622, 18F6627, 18F6722,
18F8527, 18F8622, 18F8627, 18F8722, 18F6585,
18F6680, 18F8585, 18F8680, 18F6525, 18F6621,
18F8525, 18F8621, 18F2450, 18F4450, 18F2480, 18F2580, 18F4480, 18F4580, 18F2585, 18F2680, 18F4585, 18F4680, 18F2682, 18F2685, 18F4682, 18F4685, 18F2221, 18F2321, 18F4221, 18F4321



   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

2.2         CloseADC

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

2.3         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 or A/D interrupt may be used to detect completion of the conversion.
File Name:
adcconv.c

2.4         OpenADC


2.4.3        OpenADC (ADC_V3, 4, 5, 6)

For ADC_V3, ADC_V4, ADC_V5 and ADC_V6
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 either bitwise AND operation (‘&’) or bitwise OR operation (‘|’), configurable either way as shown in the example at the end of this file, 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 either bitwise AND operation (‘&’) or bitwise OR operation (‘|’), configurable either way as shown in the example at the end of this file, with a value from each of the categories listed below. These values are defined in the file adc.h.

Channel:
For ADC_V3:
            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
For ADC_V4:
            ADC_CH0          Channel 0
            ADC_CH1          Channel 1
            ADC_CH2          Channel 2
            ADC_CH3          Channel 3
For ADC_V5 and ADC_V6:
            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 Vref+ and Vref- configuration:
    ADC_REF_VDD_VREFMINUS       VREF+ = VDD & VREF- = Ext.
    ADC_REF_VREFPLUS_VREFMINUS  VREF+ = Ext. & VREF- = Ext. 
    ADC_REF_VREFPLUS_VSS        VREF+ = Ext. & VREF- = VSS
    ADC_REF_VDD_VSS             VREF+ = VDD & VREF- = VSS

Portconfig
For ADC_V3:
The value of portconfig can be any value from 0 to 127, few are defined
below
            ADC_0ANA        All digital
            ADC_1ANA        analog:AN0                   
            ADC_2ANA        analog:AN0-AN1            
            ADC_3ANA        analog:AN0-AN2            
            ADC_4ANA        analog:AN0-AN3            
            ADC_5ANA        analog:AN0-AN4            
            ADC_6ANA        analog:AN0-AN5            
            ADC_7ANA        analog:AN0-AN6            
For ADC_V4:
The value of portconfig can be any value from 0 to 15, few are defined below
            ADC_0ANA        All digital
            ADC_1ANA        analog:AN0                   
            ADC_2ANA        analog:AN0-AN1            
            ADC_3ANA        analog:AN0-AN2            
            ADC_4ANA        analog:AN0-AN3            
For ADC_V5 and ADC_V6:
      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
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:
With AND mask:
OpenADC( ADC_FOSC_32      &
         ADC_RIGHT_JUST   &
         ADC_12_TAD,
         ADC_CH0          &
                   ADC_REF_VDD_VSS  &
         ADC_INT_OFF, 12  );
With OR mask:
OpenADC( ADC_FOSC_32      |
         ADC_RIGHT_JUST   |
         ADC_12_TAD,
         ADC_CH0          |
         ADC_REF_VDD_VSS  |
         ADC_INT_OFF, 12  );



  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




SetChanADC

This function not supports ADC_V7 and ADC_V7_1.

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
            ADC_CH12        Channel 12
            ADC_CH13        Channel 13
            ADC_CH14        Channel 14
            ADC_CH15        Channel 15
            ADC_CH_CTMU          Channel 13
            ADC_CH_VDDCORE  Channel 14
            ADC_CH_VBG            Channel 15
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.7         SelChanConvADC

Function:
Select the channel used as input to the A/D converter  and starts the A/D conversion process.
Include:
adc.h
Prototype:
void SelChanConvADC( 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
            ADC_CH12        Channel 12
      ADC_CH13        Channel 13
            ADC_CH14        Channel 14
            ADC_CH15        Channel 15
            ADC_CH_CTMU          Channel CTMU
            ADC_CH_VDDCORE  Channel VDDCore
            ADC_CH_VBG            Channel VBG
Remarks:
Selects the pin that will be used as input to the A/D converter. And starts an A/D conversion. The BusyADC() function or A/D interrupt may be used to detect completion of the conversion.
File Name:
adcselchcov.c
Code Example:
SelChanConvADC( ADC_CH0 );

2.8         Macro definitions

Macros
Description
ADC_INT_ENABLE()
Enables A/D interrupt i.e. sets PEIE and ADIE bits.
ADC_INT_DISABLE()
Disables the A/D interrupt.
ADC_SEVT_ENABLE()
Enables the Special Event Trigger bit, this macro is available for ADC_V4 only.
ADC_SEVT_DISABLE()
Disables the Special Event Trigger bit, this macro is available for ADC_V4 only.
ADC_CALIB()
Enables the A/D calibration bit, this macro is available forADC_V6 , ADC_V9, ADC_V11 and ADC_V12 only.
ADC_NO_CALIB()
Disables the A/D calibration bit, this macro is available forADC_V6 , ADC_V9, ADC_V11 and ADC_V12 only.
ADC_CH_GRA_AN0()
Selects the adc channel, this macro is available for ADC_V7 and ADC_V7_1 only.
ADC_CH_GRA_AN4()
Selects the adc channel, this macro is available for ADC_V7 and ADC_V7_1 only.
ADC_CH_GRB_AN1()
Selects the adc channel, this macro is available for ADC_V7 and ADC_V7_1 only.
ADC_CH_GRC_AN2()
Selects the adc channel, this macro is available for ADC_V7 and ADC_V7_1 only.
ADC_CH_GRD_AN3()
Selects the adc channel, this macro is available for ADC_V7 and ADC_V7_1 only.
ADC_CH_GRB_AN5()
Selects the adc channel, this macro is available for ADC_V7only.
ADC_CH_GRC_AN6()
Selects the adc channel, this macro is available for ADC_V7only.
ADC_CH_GRD_AN7()
Selects the adc channel, this macro is available for ADC_V7only.
ADC_CH_GRA_AN8()
Selects the adc channel, this macro is available for ADC_V7only.
ALL_CH_DIGITAL()
Selects the adc channel, this macro is available for ADC_V7only.

Example Use of the A/D Converter Routines with AND mask:
#include <p18cxxx.h>
#include <adc.h>
#include <delays.h>

int result;

void main( void )
{
  // configure A/D convertor
  OpenADC( ADC_FOSC_32      &
           ADC_RIGHT_JUST   &
           ADC_12_TAD,
           ADC_CH0          &
                     ADC_REF_VDD_VSS  &
           ADC_INT_OFF, ADC_CH10 );

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

Example Use of the A/D Converter Routines with OR mask:
#include <p18cxxx.h>
#define  USE_OR_MASKS
#include <adc.h>
#include <delays.h>

int result;

void main( void )
{
  // configure A/D convertor
  OpenADC( ADC_FOSC_32      |
         ADC_RIGHT_JUST   |
         ADC_12_TAD,
         ADC_CH0          |
                   ADC_REF_VDD_VSS  |
         ADC_INT_OFF, ADC_CH10 );
  Delay10TCYx( 5 );     // Delay for 50TCY
  ConvertADC();         // Start conversion
  while( BusyADC() );   // Wait for completion
  result = ReadADC();   // Read result
  CloseADC();           // Disable A/D converter
}


=======================================================



COMPARATOR MODULE
The analog comparator module contains two
comparators that can be configured in a variety of
ways. The inputs can be selected from the analog
inputs multiplexed with pins RA0 through RA5, as well
as the on-chip voltage reference (see Section 21.0
“Comparator Voltage Reference Module”). The digital
outputs (normal or inverted) are available at the pin
level and can also be read through the control register.
The CMCON register (Register 20-1) selects the
comparator input and output configuration. Block
diagrams of the various comparator configurations are
shown in Figure 20-1.
REGISTER 20-1: CMCON: COMPARATOR CONTROL REGISTER





沒有留言:

張貼留言