Internal Comparator Setup on 8-Bit Devices

The comparator peripheral inside many 8-bit PIC® microcontrollers (MCUs), including the PIC16F18855 on the MPLAB® Xpress Evaluation Board is a useful module to connect the analog world to the digital world by comparing two analog signals and sending a digital result. Specifically, it compares the amplitudes or the voltage level of the analog signals and then outputs a digital one or zero based on the difference between the two. This module is particularly useful because it can operate independently of the software execution of the microcontroller. Figure 18-1 below, from the PIC16F18855 data sheet, gives a visual example of this:


Setting up the Comparator Module

The comparator module can be easily set up using MPLAB® Code Configurator (MCC). The comparator is located under Device Resources in MCC.


Double click on CMP1 or CMP2 to open the comparator setup window:


Configurability Options

Enable Comparator

Checking this box triggers the register responsible for enabling the comparator for operation. This allows you to choose whether or not to run the comparator on start-up. Disabling on start-up may be useful for power savings depending upon your project’s necessity.

Enable Synchronous Mode

This mode can synchronize the output of the comparator with Timer1, specifically connecting on the falling edge of the Timer1 source clock. If you want to time an analog event, this is useful for using with a Timer1 Gate.

Enable Comparator Hysteresis

Hysteresis alternately adjusts the trip point to prevent repeated switching when hovering around a fixed trip point. This can help reduce noise due to small changes in the input signal.

Positive Input

The non-inverting input can be connected to one of four sources in the drop-down menu. CIN0+ is the analog input pin that would allow you to connect the comparator to an external analog input, such as a potentiometer or temperature sensor. DACOUT connects to the output of the digital to analog converter of the microcontroller. FVR_buf2 connects the V+ input to a Fixed Voltage Reference of the microcontroller. VSS connects the V+ input to ground.

Negative Input

The inverting input can be connected to one of three sources in the drop-down menu. CxIN- will connect to one of the given analog input pins. FVR will connect the inverting input to the Fixed Voltage Reference. Vss will connect the inverting input to analog ground.

Some of the inputs to the inverting input may be shared with the operational amplifier output, in which case the output of the opamp will be connected to the inverting input of the comparator.

Output Polarity

This will determine whether V+ > V- or V- > V+ will trigger a 1 or 0 output from the comparator. This is functionally the same as switching the inputs to V+ and V-.

Input Condition Output Polarity CMPx Output
V+ > V- Not Inverted 1
V+ < V- Not Inverted 0
V+ > V- Inverted 0
V+ < V- Inverted 1

Enable Comparator Interrupt

When the relationship between VIN+ and VIN- changes, this option allows an interrupt to be triggered. The two options, Rising Edge and Falling Edge, determine when the interrupt is triggered.

Configuring the Pins in MCC

To choose exterior inputs into the comparator, set the CIN0+ and CxIN- pins. Set the CxOUT to choose where the comparator will output its digital signal.


Under the Pin Manager in MCC, the three rows labeled CMPx (CMP1 in this case) provide the configurability for these pins. As you can see, there is only one option for the CIN0+ input, RA2. For the CxIN- pin, there are four options, and many for the CxOUT which outputs the digital signal.

Using Basic Software Functionality in the main.c file

After you press the MCC Generate button, MCC will create a set of driver files with custom functions that can be useful for comparator applications. To find the functions for the comparator, look in the cmpx.h (cmp1.h in this case) and cmpx.c (cmp1.c) files:

Software Function (found in cmp1.h) What It Does
void CMP1_Intialize(void); Loads all the preset conditions in MCC Xpress Code Configurator. Not necessary to call unless CMP un-enabled at start-up.
bool CMP1_GetOutputStatus(void); Returns a logic 1 or 0 depending on the current conditions set in the comparator. These are the conditions referred to under the Output Polarity section.
If interrupts are enabled, the following function is added:
void CMP1_ISR(void); When originally generated, the function only resets the flag bit triggered by the interrupt. However, to change what happens when this function is called, you can edit the function under cmp1.c seen below:

Example Code

This simple code sample controls an LED based on the signal inputs to the comparator.

#define LED_On  LATAbits.LATA0=1    //Set the Pin as 1 if calling LED_ON
#define LED_Off LATAbits.LATA0=0    //Set the Pin as 0 if calling LED_OFF
CMP1_Initialize()                    //Initialize CMP1
        if(CMP1_GetOutputStatus())    //Check status of the comparator
            LED_On;                    //Turn LED On
             LED_Off;                    //Turn LED Off

For a more detailed example of using the comparator, check out this project using a PIC16F1508.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.