A/D Converter module of PIC16F873
The function of A/D converter is to change the measurement result of the voltage of the analog input signal into the digital information.
PIC16F873 changes into the 10-bit digital information. So, the measurement result can be displayed in the precision of 1/1024 in the maximum range. It is possible to measure in the about 5mV precision (5/1024=0.004883), supposing that the maximum measuring range is 5V(From 0V to +5V).

Input port
In PIC16F873, a maximum of 5 ports can be specified as the analog input port. Because the A/D conversion module is one, so, it can do only the measurement of 1 port at the same time. If measuring each port in the order, more than one port can be measured. Because the measuring time is an about 40-u seconds, the changing analog voltage can be measured. It isn't possible to do the measurement of the high frequency.
PCFG
3210
Port Remarks
AN4AN3AN2AN1AN0
0000AAAAA
0001AVREF+AAA
0010AAAAASame as 0000
0011AVREF+AAASame as 0001
0101DVREF+DAA
0110DDDDD
0111DDDDDSame as 0110
1000AVREF+VREF-AA
1001AAAAASame as 0000
1010AVREF+AAASame as 0001
1011AVREF+VREF-AASame as 1000
1100AVREF+VREF-AASame as 1000
1101DVREF+VREF-AA
1110DDDDA
1111DVREF+VREF-DA
The input analog port is designated by PCFG3-0 bits of ADCON1 register. The patterns which can be designated by these 4 bits are beforehand decided.
The port which connects with the A/D conversion module is designated by CHS0, CHS1 and CHS2 of the ADCON0 register. The analog input port should be set as input mode with setting the corresponding bit of TRISA register.

There are the same patterns more than one. It is because the pattern is shared in the PIC16F87x series.

A: Analog port
D: Digital port
VREF+: High reference voltage
VREF-: Low reference voltage

Measurement range and measurement precision

As for the measurement range, the range of VDD(+5V) from VSS(0V) is basic range. Besides, it is possible to set in VREF-(Low reference voltage) and VREF+(High reference voltage). But, VREF- and VREF+ must be the inside of the range of VDD from VSS.
For example, if it makes the side of the low voltage to VSS(0V) and the side of the high voltage to 3V by VREF+, then the measuring range becomes 3V. Because this range is measured in the precision of 1/1024, the measurement precision in this case is 3/1024=0.002929 (about 3mV).
 The use of VREF- or VREF+ can be designated by the pattern of PCFG0-3 of the ADCON1 register. When VREF- and VREF+ are specified, the difference voltage which is inputted on AN2 and AN3 is a measuring range. When only VREF+ is specified, the low voltage is VSS. When VREF- and VREF+ aren't specified, the low voltage is VSS and the high voltage is VDD.

Result of A/D conversion

 The ADRESH and ADRESL register pair is the location where the 10 bits A/D result is loaded at the completion of the A/D conversion. This register pair is 16 bits wide. The A/D module gives the flexibility to left or right justify the 10 bits result in the 10 bits result register. The A/D Format Select bit (ADFM) of ADCON1 register controls this justification. The extra bits are loaded with 0.

Mechanism of the measurement of the analog voltage

The sample/hold capacitor (CHOLD) in A/D converter module is charged by the voltage applied from analog input port. The voltage of CHOLD is converted to the digital value with A/D converter. So, it is necessary to wait until the CHOLD is charged fully after selecting the input analog channel by setting CHS0-3 of ADCON0 register. This time isn't automatically secured. The start timing of A/D converter after selecting the input channel must be controlled by software. It needs about 20u seconds to start A/D converter after setting input channel.
The shortest A/D conversion time is an about 20u seconds. It depends on the conversion clock. During this time, the CHOLD is disconnected from analog input.
A/D conversion starts when GO bit of ADCON0 register is set. When the A/D conversion is complete, GO bit will be cleared by hardware. There are two way to detect the completion of A/D conversion. One is the way of polling for the GO bit to be cleared. One more way is the way which use interruption. In case of the interruption, ADIE bit of PIE register and GIE bit of INTCON register should be set. When the conversion is complete, ADIF bit of PIR1 register is set and interruption occurs.

The circuit block which CHOLD charges by the analog input voltage is shown in the left figure. CHOLD is charged when sample switch (SS) is closed. The diode is for the protection when voltage over VDD or voltage under the grounding is applied to the input.

 CHOLD: Sample/Hold capacitance ( 120pF ) RSS: Sampling switch resistance( about 7K-ohm at VDD=5V ) SS: Sampling switch RIC: Interconnect resistance ( =<1K-ohm ) CPIN: Input capacitance ( 5pF ) ANx: Input pin RS: Analog source impedance ( =<10K-ohm ) VA: Analog source voltage

A/D conversion clock

Less 1.25MHzFosc/200(1/1.25)*2=1.6 uS
Less 5MHzFosc/801(1/5)*8 =1.6 uS
Less 20MHzFosc/3210(1/20)*32=1.6 uS

RC is used when doing A/D conversion under SLEEP mode.
A/D conversion is executed based on the A/D conversion clock. For correct A/D conversions, the A/D conversion clock(TAD) must be selected to ensure a minimum TAD time of 1.6u seconds.
One conversion clock from four kinds of clock patterns can be selected by ADCS0 and ADCS1 bits of ADCON0 register.
The resultant TAD times deriver from the device operating frequencies and the A/D clock source selected is shown below.

In PIC16F873, the A/D conversion takes 12-bit time. Actually, the conversion is 10 bits but more 2 bits (pre-processing time and post-processing time) is necessary. When supposing that the conversion time of 1 bit is 1.6 uS, the total conversion time is 1.6 x 12 = 19.2u seconds.
In case of the 10MHz clock, Fosc/32 is chosen. In this case, the conversion time of 1 bit is (1/10) x 32=3.2 uS and it takes 38.4u seconds for 12 bits.

Software processing of the A/D converter

When not using interruption processing