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.
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
|
Fig. 2: Bit Configuration of Comparator Control Register in PIC18F4550
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
|
Fig. 3: Bit configurations and Modes for using analog compatator of PIC18F4550
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.

Fig. 4: Circuit Diagram Of PIC’s Analog Comparator
The circuit connection for a single IR module is as follows.

Fig. 5: Circuit Diagram of Single IR Module using PIC181F4550
The complete connection layout is shown in the circuit diagram tab.
Programming steps:
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.
Project Source Code
###
// Program to use inbuilt analog comparator of PIC18F4550 void main() { TRISA.RA0=1; // Configure as input pin for negative input of Comparator 1 TRISA.RA1=1; // Configure as input pin for negative input of Comparator 2 TRISA.RA2=1; // Configure as input pin for positive input of Comparator 1 TRISA.RA3=1; // Configure as input pin for positive input of Comparator 2 TRISA.RA4=0; // Configure as output pin for output of Comparator 1 TRISA.RA5=0; // Configure as output pin for output of Comparator 2 CMCON=0x05; // 'Two Common Reference Comparators with Outputs' Mode while(1); }
###
Circuit Diagrams
Project Components
Project Video
Filed Under: PIC Microcontroller
Filed Under: PIC Microcontroller
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.