8-Bit Comparator and Fixed Voltage Reference

Signal voltages are not always digital, such as zero volts or five volts. Many times they are analog, which means they can vary in voltage level. Having a simple way to accurately measure a trip point or threshold can be very useful. This is where a comparator with an accurate Fixed Voltage Reference (FVR) can be used within a PIC® microcontroller. This project monitors a potentiometer-controlled voltage that lights up an LED when the threshold is crossed.


Step by Step Instructions

Using MPLAB® Code Configurator (MCC), this project will set up the peripherals and generate code for the PIC16F18875 on the Curiosity HPC board. The last step will program the board and make the project run.


The project uses:

  • PIC16F18875
  • HPC Curiosity Board
  • MPLAB Code Configurator (MCC) plug-in
  • MPLAB XC8 Compiler

To follow along with these steps, MPLAB X should be open and the HPC Curiosity Board should be connected to the computer through a USB cable.


Create a new standalone project in MPLAB X for a PIC16F18875.

If this is your first time creating an MPLAB X project, please visit the "Create a Standalone Project" page to follow step-by-step instructions on how to do this.


Open MCC under the Tools > Embedded menu of MPLAB X IDE.



Select the peripherals for your project.

In this project, you need to select these peripherals:

  • System Module
  • Interrupt Module
  • Pin Module
  • CMP1
  • FVR

The System Module, Interrupt Module, and the Pin Module will all be automatically included when you launch MCC. CMP1 and FVR need to be added from the Device Resources area. To add it to the project, click on the Comparator Section and then double click on CMP1. Expand the FVR section and double click on FVR to add it to the project. The result should look like the image below:



The CMP1 needs to be set up. Click on the CMP1 label in the Project Resources menu to make the CMP1 setup screen appear.


The comparator will require two inputs selected for operation. Follow the steps below to set up CMP1:

  1. Check the Enable Comparator box.
  2. Select the FVR_buf2 as the Positive input from the drop-down menu.
  3. Set the CIN0- as the Negative input from the drop-down menu. This is connected to the potentiometer on the Curiosity HPC Board.
  4. Select the Non-Inverted Output.
  5. Leave the Enable Comparator Interrupt box unchecked.

The window should now look like the image below:



The FVR needs to be set up. Click on the FVR label in the Project Resources menu to make the FVR setup screen appear.


The FVR base voltage is 1.024 V. This project needs the comparator threshold to be at 2.048 V, so the FVR must be set to two times the base voltage. This is done in the MCC setup screen.

  1. Check the Enable FVR box.
  2. Set FVR_buffer1 Gain (to ADC) to off from the drop-down menu.
  3. Set FVR_buffer2 Gain (to other peripherals) to 2X from the drop-down menu.

The window should look like below after the setup:



Open the Pin Manager and then click on the PORTA bit 4 (RA4) output blue lock symbol. The lock symbol will turn green. This adds this RA4 I/O pin connected to the LED2 on the Curiosity Board to the project. Next, verify the C1INx- pin lock is green. When the comparator was set up, the C1INx- input was selected, which is at PortA bit 0 (RA0). It should look like the image below when completed:



Close the Pin Manager and then click on the Pin Module selection in the Project Resources section.


The center screen should show RA4 listed on the I/O chart. Click on the Output box to make the pin an output (if not checked) and make sure Analog and Weak Pull-up (WPU) are not checked (click on them to uncheck them). Change the Custom Name to "D2_LED".



The System needs to be set up next. Click on the System Module name under Project Resources.


In this section, the oscillator settings and the core configuration settings are selected.


  1. Select HFINTOSC from the Oscillator Select drop-down menu.
  2. Select 4_MHz from the HF Internal Clock drop-down menu.
  3. Select 4 from the Clock Divider drop-down menu.

This will enable the internal 1 MHz internal oscillator as the system clock.


Also, make sure the Low-voltage Programming Enable mode is selected at the bottom of the System Module screen.



Click on the Generate button to have MCC create the software libraries for this project.



The project will now have both generated Header Files and Source Files. It should also have a generated main.c file.

Note: MCC may offer to generate a main.c file. Click Yes to allow it to complete that task.


Double click on main.c to open it in the editor window.




The main loop requires you to add a small section of code to control the LED. Within main.c is a while(1) loop with the comment // Add your application code . An if statement is added to test the state of the CMP1_GetOutputStatus() function. That function is defined in the cmp1.h and cmp1.c files generated by MCC. If it returns a one, then the D2_LED_SetLow() function is implemented. If it is clear then the D2_LED_SetHigh() function is implemented. These functions are actually macros created by MCC and placed in the pin_manager.h file. Based on the output of the comparator, this main.c code will either light the LED or turn it off.

while (1)
        // Add your application code


Click on the Build Project (hammer icon) to compile the code and you should see a BUILD SUCCESSFUL message in the Output window of MPLAB X.



Make sure your project has the Curiosity Board selected and the USB cable is connected to the board.

Click on Make and Program Device Main Project. This will build the project again and launch the programmer. In the Output window, you should see a series of messages, and if successful, it will end with a Programming/Verify complete message.

Connecting to Starter Kit on Board...

Currently loaded firmware on Starter Kit on Board
Firmware Suite Version.....01.54.00
Firmware type..............Enhanced Midrange

Target voltage detected
Target device PIC16F18875 found.
Device Revision ID = 2002

Device Erased...


The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x7ff
configuration memory
**Programming/Verify complete**

The MPLAB XC8 Compiler made setting up the Comparator and the FVR easy, allowing the main.c code to use the potentiometer position to control the LED. When the potentiometer is turned more than half-way, the LED will light. When it is turned less than half-way, the LED will be off.

If it is the first time the programmer is connected to the board, the programming tool may need to download the proper operating firmware for the exact device. You may see a series of processes if this occurs. This should only happen once.

Downloading Firmware…
Downloading bootloader
Bootloader download complete
Programming download…
Downloading RS…
RS download complete
Programming download…
Downloading AP…
AP download complete
Programming download…
Firmware Suite Version…..01.34.11
Firmware type…………..Enhanced Midrange


The project can be closed in MPLAB X. The project is saved automatically when it is built but any changes to files or configuration may ask to be saved before the project is closed. The project can be closed under the File > Close Project.



If you have any problems with your project, the completed MPLAB X project file can be downloaded from the link below:

File Download
Windows Linux Mac OSX
Project 8 Files
© 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.