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 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 the MPLAB® Code Configurator (MCC), this project creates a comparator input to read a potentiometer and control an LED from a digital output. A Fixed Voltage Reference is used as a reference for the comparator. When the potentiometer is turned to produce a voltage beyond the threshold, the LED will light up. When the potentiometer is turned to a voltage below the threshold the LED will be off.


The project uses:

PICDEM Lab II Connections:

  • Connect 5 V and Gnd to the power bus of the protoboard.
  • Connect 5 V to the Vdd pin of the PIC16F1508
  • Connect Gnd to the Vss pin of the PIC16F1508
  • Connect a wire from the RA2 pin (pin 17) to the breadboard Row 23
  • Connect a wire from the ground bus to breadboard Row 27
  • Connect the LED Anode to breadboard Row 23
  • Connect the LED Cathode to breadboard Row 24
  • Connect a 330 ohm resistor between Row 24 and Row 27
  • Connect a wire from the RC3 pin (pin 7) to the breadboard Row 8
  • Connect the potentiometers three pins to row 7,8 and 9
  • Connect a wire between Row 9 and the breadboard ground bus
  • Connect a wire between Row7 and the breadboard power bus

To follow along with these steps, MPLAB X should be open and the Programmer/Debugger connected to both the computer and the Development Board. The setup was described in the Setup and Installation section of this training module. You should see a screen similar to the one below to move on to step 1.



Create a new "Stand-Alone" project in MPLAB X for a PIC16F1508. Instructions are included below if this is your first project.


Open the MPLAB Code Configurator 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 the MCC. CMP1 and FVR needs to be added from the Device Resources area. Just click on the Timer category and then double click on the Timer 1 to add it to the project. The result should look like the picture below:



The CMP1 needs to be setup. 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. The steps below will setup the CMP1.

  1. Check the Enable Comparator box.
  2. Select the FVR_pin as the Positive input from the drop down menu.
  3. Set the CIN3- as the Negative input from the drop down menu.
  4. Select the Non-Inverted output.
  5. Leave the Enable Comparator Interrupt box unchecked.

The window should look like below after the setup:



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


The Fixed Voltage Reference 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. Select 2x as the FVR amount sent to Comparators, DAC and CPS from the drop down menu.
  3. Set off as the FVR amount sent to ADC from the drop down menu.

The window should look like below after the setup:



Open the Pin Manager and then click on the PORTA pin 2 (RA2) Output blue lock symbol. It should turn green. This adds this RA2 I/O pin to the project. Also make sure the Reset pin has the blue lock shown. If it's green then click on the green lock to make it blue. This will make the Reset internal. Finally click on the RC3 pin for the C1INx- pin. It should look like the picture here when completed:



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


The center screen should show RA2 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 WPU are not checked (click on them to uncheck them).



The System needs to be setup next. Click on the System name in the Project Resources list.


The System section will appear. In this section the oscillator settings and the configuration settings are selected.


  1. Select the INTOSC from the drop down menu for the System Clock Select.
  2. Select the 1MHz_HF selection from the Internal Clock drop down menu.

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



Each configuration setting can be changed under the Register tab of the System window. Match the selections shown here.



Click on the Generate Code button to have the 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 up in the editor window.




The main loop requires a small section of code added 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 the MCC. If it returns a one, then the IO_RA2_SetLow() function is implemented. If it is clear then the IO_RA2_SetHigh() function is implemented. These functions are actually macros created by the 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 Icon (the Hammer) to compile the code and you should see a BUILD SUCCESSFUL message in the output window of MPLAB® X.

BUILD SUCCESSFUL (total time: 5s)


Make sure your project has the programming tool selected (Part of Step 1 above) and connect power to your development board.

The PICkit 3 has limited power capability so we recommended powering the board separately.
The ICD 3 can power a development board, but for simplicity we recommend powering the board separately.
The RealICE cannot power the development board so powering the board separately is required.

Click on the Make and Program Device icon. 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 and Verify Successful message.


Output Window:

Connecting to MPLAB PICkit 3...

Currently loaded firmware on PICkit 3
Firmware Suite Version.....01.41.06
Firmware type..............Enhanced Midrange

Target voltage detected
Target device PIC16F1508 found.
Device ID Revision = 2

The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x5f
configuration memory

Device Erased...

Programming/Verify complete

The RA2 LED should light up when the RA4 potentiometer is turned clockwise more than the half-way point. The picture below shows the potentiometer beyond the half-way point so the LED is lit.

If it's 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 make 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 Menu>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.