Contents
Comparator Module in PIC
Analog Comparator is a device which compares two voltage signals and switches its output to indicate which one is larger. The analog comparator is used in many applications such as battery charger, analog to digital converters, IR sensors where we need to compare two signals. Most of the PIC Microcontrollers have built in Comparator Module which makes ease of design and saves cost. For demonstration, we use PIC16F877A.
PIC16F877A consists of two analog comparators which can be used in eight different modes. The input pins of comparator are multiplexed with I/O port pins RA0 through RA3 while output pins are multiplexed with RA4 and RA5. CMCON register is the Comparator Module control register.
CMCON Register
CM0 – CM2
The Least Significant 3 bits, CM0 – CM2 is used to select one of the eight modes that the comparator module can work.
In normal cases we turn off the comparator module by setting CMCON = 0x07.
C2OUT : Comparator 2 Output bit. It is a read only bit.
When C2INV = 0 :
1 = C2Vin+ > C2Vin-
0 = C2Vin+ < C2Vin-
When C2INV = 1:
1 = C2Vin+ < C2Vin-
0 = C2Vin+ > C2Vin-
C1OUT : Comparator 1 Output bit. It is a read only bit.
When C1INV = 0:
1 = C1Vin+ > C1Vin-
0 = C1Vin+ < C1Vin-
When C1INV = 1:
1 = C1Vin+ < C1Vin-
0 = C1Vin+ > C1Vin-
C2INV : Comparator 2 Output Inversion Bit. When this bit is set the Comparator 2 output will be inverted.
C1INV : Comparator 1 Output Inversion Bit. When this bit is set the Comparator 1 output will be inverted.
For the demonstration of the working of Comparator Module in PIC Microcontroller, we use the mode ‘Two Common Reference Comparators with Output’.
Circuit Diagram
Note: VDD and VSS of the pic microcontroller is not shown in the circuit diagram. VDD should be connected to +5V and VSS to GND.
In this example we are comparing voltages across two LDRs (Light Dependent Resistors) with reference voltage set by the variable resistor RV1. LED will glow if the voltage across LDR is less than the reference voltage. LEDs D1 and D2 are supplied directly from the output of comparator. Note that RA4 is an open collector I/O pin, so when used as an output, a pull-up resistor is required. LEDs D3 and D4 are supplied from PORTC, which is powered through program.
MikroC Program
void main() { TRISC = 0; //Configure PORTC as ouput 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) { PORTC.F0 = CMCON.C2OUT; // Assigning output of comparator 2 to RC0 PORTC.F1 = CMCON.C1OUT; // Assigning output of comparator 2 to RC1 Delay_ms(100); } }
Download Here
You can download the hex file, MikroC source code, Proteus files etc here…