Analog comparators, including inbuilt Analog Comparators, are electronic devices which compare the two voltage signals and provide TTL logic output to indicate the larger signal. The analog comparator is used in various applications where two input signals need to be compared. IR sensor is a very common example where analog comparator is used.
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.
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
|
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
|
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
Source: How to work with inbuilt Analog Comparators of PIC18F4550- (Part 12/25)