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 generates the code to blink an LED using a hardware Timer0 Interrupt to generate the delay between the on and off state.
The project uses:
- PIC16F18875
- HPC Curiosity Board
- MPLAB® X IDE
- MCC plugin
- MPLAB XC8 Compiler
Note: 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.
Note: 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.
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. Add CMP1 and FVR from the Device Resources pane. Click on the Timer category and then double click on the Timer 1 to add it to the project. Compare your results to the following image:
4
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 the CMP1.
- Check the Enable Comparator box.
- Select the FVR_buf2 as the Positive input from the drop-down menu.
- Set the CIN0- as the Negative input from the drop-down menu. This is connected to the potentiometer on the Curiosity HPC Board.
- Select the not inverted output.
- Leave the Enable Comparator Interrupt box unchecked.
The window will appear as follows after the setup:
5
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.
- Check the Enable FVR box.
- Set off as the FVR amount sent to ADC from the drop down menu.
- Select 2x as the FVR amount sent to Comparators, DAC, and CPS from the drop down menu.
The window will appear as follows after the setup:
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). Compare your results to the following:
7
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 WPU are not checked (click on them to uncheck them). Change the Custom Name to "D2_LED".
8
The System needs to be set up next. Click on System Module under Project Resources.
In this section, the oscillator settings and the configuration settings are selected.
Oscillator
- Select the HFINTOSC from the Oscillator Select drop-down menu.
- Select the 4_MHz selection from the HF Internal Clock drop-down menu.
- Select the Clock Divider value of 4.
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.
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();
}
}
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.
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.
Note: 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
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
|
| | | |