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.

proj8.png

Step by Step Instructions

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

proj6bd.jpg

The project uses:

  • PIC16F18875
  • HPC Curiosity Board
  • MPLAB X IDE
  • 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 connected to the computer through a USB cable.

1

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 a step-by-step instruction on how to do this.

2

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

mcclaunch.png

3

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 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:

proj8per2.png

4

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

cmp12.png

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_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 look like below after the setup:

project8compset.png

5

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

fvr2.png

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

The window should look like below after the setup:

fvrset2.png

6

Open the Pin Manager and then click on the PORTA pin 4 (RA4) Output blue lock symbol. It should 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 Pin 0 (RA0). It should look like the picture here when completed:

project8pinmanager.png

7

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

pin.png

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 WPU are not checked (click on them to uncheck them). Change the Custom Name to D2_LED.

project8pin.png

8

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

project7menusystem.png

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

Oscillator

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

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

project7osc.png

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

lvp.png

9

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

generate.png

10

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.

proj8files2.png

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

mainfile.png

11

main.c

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 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
      if(CMP1_GetOutputStatus())
         {
            D2_LED_SetHigh();
         }
         else
         {
            D2_LED_SetLow();
         }
     }

12

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.

Main_Build_Project.png
BUILD SUCCESSFUL

13

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.

Main_Program_Target_Project.png
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...

Programming...

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 control the LED-based on the potentiometer position. 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.
proj6finished.png

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

14

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.

closeproject.png

Download

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

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