[Menu]>[Guide to use the PIC]>[628 Hardware]

Comparator module of PIC16F628

There is analog voltage comparator function in PIC16F628. PIC16F628 has 2 circuits of this function.
It has a function to compare two in the voltage which is inputted to RA3 from RA0 and a function to compare with the reference voltage.

The analog voltage comparison function can be controlled by the contents of the CMCON register.

It is possible to make interruption occur when either comparator output is changed. In the case, it makes set the CMIE bit of the PIE1 register to "1". Also, it should set the PEIE bit and the GIE bit of the OPTION register to "1". By this, when the output of the comparator is changed, "1" is set to the CMIF bit of the PIR1 register and the interruption occurs. In the interruption processing, CMIF of the PIR1 register must be cleared. The interruption always occurs when the output of the comparator doesn't change when not doing this.



Output control

The output of the comparator is put to C1OUT and the C2OUT bit of the CMCON register. The output contents of C1OUT and C2OUT can be reversed by the setting of C1INV and C2INV bit. The example of CMP1 is shown below. CMP2 is same too.
Input conditionC1INVC1OUT
VIN+ > VIN-01
VIN+ > VIN-10
VIN+ < VIN-00
VIN+ < VIN-11




Input control

The input to the comparator can be controlled by CM2 from CM0 and CIS bit of the CMCON register.

In the condition immediately after the turning on, all CM2, 1, 0 and CIS are in the condition of "0". In this condition, the port of RA0-3 is set as the analog port of AN0-3. When not using a comparator, all CM2, 1, 0 must have been set to "1" by the initialization processing.

CIS is the initial of Comparator Input Switch. It is used when switching the analog port and comparing more than one analog signal. When CM2-0 is "010", a maximum of four kinds of analog signals can be compared with the reference voltage with two comparators by changing in CIS.
For the details, refer to the manual of PIC16F628.



The combination of each bit is as follows.

CM2CM1CM0CISConnection state
000X
0010
1
0100
1
011X
CM2CM1CM0CISConnection state
100X
101X
110X
111X

    ANx shows an analog input.
    RAx shows a digital state.



Reference voltage

PIC16F628 has the circuit which makes reference voltage. This function is an independent circuit with the comparator. However, when making CM2-0 "010", voltage which was made with reference voltage circuit is applied to the positive input terminal of each comparator.
The output of the reference voltage can be output at the RA2 port by the setting of VRCON register.

The reference voltage can be designated by 4 bits of VR3-0 of the VRCON register. The range which can be specified is 16 steps from 0 to 15. There are two ranges of low voltage range and high-voltage range. In case of the high voltage, the maximum is about 3.6 V. It is possible to do the setting of the voltage range by the VRR bit of the VRCON register.
Reference voltage is calculated by the following formula.
When VRR = 1 (Low Range)VREF = (VR<3:0>/ 24) * VDD
When VRR = 0 (High Range)VREF = 1/4 * VDD + (VR<3:0>/ 32) * VDD

Resistance and Voltage Relationship in case of VDD = 5V
VR3-0Low RangeHigh Range
00000.001.25
00010.211.41
00100.421.56
00110.631.72
01000.831.88
01011.042.03
01101.252.19
01111.462.34
VR3-0Low RabgeHigh Range
10001.672.50
10011.882.66
10102.082.81
10112.292.97
11002.503.13
11012.713.28
11102.923.44
11113.133.59