Internal Comparator Setup on 8-Bit Devices

The comparator peripheral inside many 8-bit PIC® microcontrollers (MCUs), including the PIC16F18855 on theMPLAB® 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:

figure1.JPG

Setting up the Comparator Module

The comparator module can be easily set up usingMPLAB® Code Configurator (MCC). The comparator is located underDevice Resources in MCC.

figure2(1).JPG

Double click onCMP1 orCMP2 to open the comparator setup window:

figure3.JPG

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 aTimer1 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 ConditionOutput PolarityCMPx Output
V+ > V-Not Inverted1
V+ < V-Not Inverted0
V+ > V-Inverted0
V+ < V-Inverted1

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.

figure4(1).JPG

Under thePin Manager in MCC, the three rows labeledCMPx (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 themain.c file

After you press the MCCGenerate 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:

figure5(1).JPG
Software Function (found incmp1.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 undercmp1.c seen below:
figure6.JPG

Example Code

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

<code>#defineLED_OnLATAbits.LATA0=1//Set the Pin as 1 if calling LED_ON#defineLED_OffLATAbits.LATA0=0//Set the Pin as 0 if calling LED_OFFCMP1_Initialize()//Initialize CMP1while(1){if(CMP1_GetOutputStatus())//Check status of the comparator{LED_On;//Turn LED On}else{LED_Off;//Turn LED Off}}    </code>*/

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

proj8bd.jpg