PIC18F4550 consists of two analog comparators and these comparators can be used in eight different modes. The analog comparators’ I/O pins are multiplexed with PortA pins (RA0 - RA5) pins of the controller. The register CMCON is configured to set the mode of the comparator in a PIC microcontroller. The bits of CMCON register are explained below.
CMCON (Comparator Control Register)
CM2:CM0: These bits are used to set one of the comparator modes (of 8 different modes). The comparator can be configured in following modes:
The comparators remain reset and the output is read as zero
One Independent Comparator with Output
Comparator 1 is active with external output at RA4/C1OUT pin
Two Independent Comparators
Both comparators work separately with output changes at C1OUT and C2OUT bits respectively
Two Independent Comparators with Outputs
Both comparators work separately with external outputs at RA4/C1OUT and RA5/C2OUT pins respectively
Two Common Reference Comparators
The comparators works separately having common reference voltage on positive reference pins of the comparators with output changes at C1OUT and C2OUT bits respectively
Two Common Reference Comparators with Outputs
The comparators work separately having common reference voltage on positive reference pins of the comparators with external outputs at RA4/C1OUT and RA5/C2OUT pins respectively
Four Inputs Multiplexed to Two Comparators
Both comparators have multiplexed input at negative reference pin of the comparator. The common reference voltage at positive reference voltage pin comes from internal voltage reference module with output changes at C1OUT and C2OUT bits respectively
Both comparators remain off
CIS: This bit has to be configured when CM0:CM2 bit is set as 110.
1 = C1 VIN- connects to RA3/AN3/VREF+
C2 VIN- connects to RA2/AN2/VREF-/CVREF
0 = C1 VIN- connects to RA0/AN0
C2 VIN- connects to RA1/AN1
C1INV: This bit is used to invert the output bit of the comparator 1.
1 = C1 output inverted
0 = C1 output not inverted
C2INV: This bit is used to invert the output bit of the comparator 2.
1 = C2 output inverted
0 = C2 output not inverted
C1OUT: The output of the comparator 1 is stored here.
When C1INV = 0:
1 = C1 VIN+ > C1 VIN-
0 = C1 VIN+ < C1 VIN-
When C1INV = 1:
1 = C1 VIN+ < C1 VIN-
0 = C1 VIN+ > C1 VIN-
C2OUT: The output of the comparator 2 is stored here.
When C2INV = 0:
1 = C2 VIN+ > C2 VIN-
0 = C2 VIN+ < C2 VIN-
When C2INV = 1:
1 = C2 VIN+ < C2 VIN-
0 = C2 VIN+ > C2 VIN-
Using PIC's Analog Comparator
Objective: To design proximity sensors using IR LEDs (IR sensors) using the in-built comparators of PIC microcontroller.
The two IR sensors can be designed by using PIC18F4550 microcontroller. The Two Common Reference Comparators with Outputs (CM2:CM0 = 101) mode is selected to design the sensors. The input pins for sensor inputs are RA0/AN0 and RA1/AN1 (pins 2 & 3) and reference voltage pin is RA3/AN3 (pin 5). The outputs of the comparators 1 and 2 are obtained at RA4/C1OUT and RA5/C2OUT pins (pins 6 & 7).
The diagram for both the comparators is given below.
The circuit connection for a single IR module is as follows.
The complete connection layout is shown in the circuit diagram tab.
1. Set RA0 – RA3 pins as input by data direction register.
2. Set RA4 - RA5 pins as output pins.
3. Select the relevant comparator mode from CMCON register.