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 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 should be 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 step-by-step instructions on how to do this.
2
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 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:
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 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 Non-Inverted Output.
- Leave the Enable Comparator Interrupt box unchecked.
The window should now look like the image below:
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 FVR_buffer1 Gain (to ADC) to off from the drop-down menu.
- Set FVR_buffer2 Gain (to other peripherals) to 2X from the drop-down menu.
The window should look like below after the setup:
6
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:
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 Weak Pull-up (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 the System Module name under Project Resources.
In this section, the oscillator settings and the core configuration settings are selected.
Oscillator
- Select HFINTOSC from the Oscillator Select drop-down menu.
- Select 4_MHz from the HF Internal Clock drop-down menu.
- 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.
11
main.c
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
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 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
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
|
| | | |