How to work with inbuilt Analog Comparators of PIC18F4550

Analog comparator is an electronic device which compares the two voltage signals and provides TTL logic output to indicate the larger signal. The analog comparator is used in various applications where two inputs signals need to be compared. IR sensor is a very common example where analog comparator is used.

Analog Comparators

PIC18F4550 has two in-built comparators which can be used in eight different modes. These in-built comparators save the cost and connections for providing an extra IC (like LM324, LM339 etc) in the circuit. This article explains the configuration of the analog comparators of this PIC microcontroller.

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) 
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
C2OUT
C1OUT
C2INV
C1INV
CIS
CM2
CM1
CM0
 
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:
CM2:CM0
Mode
Description
000
Comparators Reset
The comparators remain reset and the output is read as zero
001
One Independent Comparator with Output
Comparator 1 is active with external output at RA4/C1OUT pin
010
Two Independent Comparators
Both comparators work separately with output changes at C1OUT and C2OUT bits respectively
011
Two Independent Comparators with Outputs
Both comparators work separately with external outputs at RA4/C1OUT and RA5/C2OUT pins respectively
100
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
101
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
110
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
111
Comparators Off
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.
 

About The Author

Ibrar Ayyub

I am an experienced technical writer with a Master's degree in computer science from BZU Multan University. I have written for various industries, mainly home automation, and engineering. I have a clear and simple writing style and am skilled in using infographics and diagrams. I am a great researcher and is able to present information in a well-organized and logical manner.