How to use analog comparator in PIC microcontroller

Microcontroller Topics
Post Reply
User avatar
Shehani
Lieutenant
Lieutenant
Posts: 61
Joined: Mon Aug 19, 2013 2:11 pm

How to use analog comparator in PIC microcontroller

Post by Shehani » Tue Aug 27, 2013 3:02 pm

Introduction to Comparator Module in PIC Microcontroller

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 of PIC Microcontroller
CMCON Register of PIC Microcontroller
1.png (6.56 KiB) Viewed 3076 times

CM0 – CM2

The Least Significant 3 bits, CM0 – CM2 is used to select one of the eight modes that the comparator module can work.
Mode Selection of Comparator Module in PIC Microcontroller
Mode Selection of Comparator Module in PIC Microcontroller
2.png (102.25 KiB) Viewed 3076 times
In normal cases we turn off the comparator module by setting CMCON = 0×07.

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.


Example Circuit and Program


For the demonstration of the working of Comparator Module in PIC Microcontroller, we use the mode ‘Two Common Reference Comparators with Output’.


Circuit Diagram

Using Analog Comparator in PIC Microcontroller Circuit Diagram
Using Analog Comparator in PIC Microcontroller Circuit Diagram
3.png (234.82 KiB) Viewed 3076 times
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

Code: Select all

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);
  }
}
Article courtesy of electrosome
Post Reply

Return to “Microcontrollers”