Microchip Minutes Episode 6: Basic Comparator Setup Application Lab

Microchip Minutes Episode 6: Basic Comparator
Setup Application Lab
Contents
Microchip Minutes Episode 6: Basic Comparator Setup Application Lab ........................... 1
References ................................................................................................................................ 1
Prerequisites .............................................................................................................................. 1
Overview .................................................................................................................................... 2
Equipment Needed .................................................................................................................... 2
Software: ............................................................................................................................... 2
Hardware: .............................................................................................................................. 2
Procedure .................................................................................................................................. 3
Adding the Comparator........................................................................................................ 4
Configuring the Fixed Voltage Reference ........................................................................... 7
Result ........................................................................................................................................ 9
References

PIC16F1619 Datasheet: www.microchip.com/pic16f1619

MPLAB® X IDE User’s Guide: www.microchip.com/mplabx
Prerequisites
This lab assumes that the reader is familiar with introductory concepts covered in Microchip
Minutes: helloWorld Application Lab document and video including:

How to set up a basic project in MPLAB X IDE using the MPLAB Code Configurator

Familiarity with the MPLAB Code Configurator workspace
1
Overview
The purpose of this lab is to compare a voltage on a pin of the PIC16F1619 microcontroller
against an internal fixed voltage reference set to 2.048 V using one of the integrated high speed
comparators. When the pin voltage exceeds the 2.048 V the comparator will generate a 0
(LOW) on its output. When the pin voltage is less than the 2.048 V reference the comparator will
generate a 1 (HIGH) on its output. The output of the comparator will be configured internally to
connect to pin RC5 which is in turn connected to an LED on the Curiosity Development Board.
The potentiometer on the Curiosity board will be used to generate a variable voltage on the
comparator’s inverting input.
Equipment Needed
This lab was developed using the following tools:
Software:
-
MPLAB® X IDE : www.microchip.com/mplabx
MPLAB XC8 C Compiler: www.microchip.com/xc8
MPLAB Code Configurator (MCC) v2.25 was used in this lab. We recommend using the
latest revision: www.microchip.com/mcc
Hardware:
-
Curiosity Development Board: www.microchip.com/curiosity
Page includes user’s guide along with schematics and other helpful information.
2
-
Jumper wire to connect Potentiometer on pin RC0 to Comparator inverting input on RC1.
Procedure
1. Open MPLAB® X IDE and create a new project called basicComp
2. Open the MPLAB Code Configurator by selecting Tools>Embedded>MPLAB Code
Configurator.
3
3. In the Project Resources area, select the System resource to open the user interface
in the composer area. Leave the Clock settings at default and enable Low-voltage
programming in the CONFIG2>Low-Voltage Programming Enable [LVP ON] dropdown.
Adding the Comparator
4. In the Device Resources area, scroll down to locate the Comparator section and
double-click on CMP1::CMP to add the Comparator peripheral to the Project
Resources.
5. Select the CMP1::CMP peripheral in the Project Resources to open the user interface
in the Composer Area. Configure the Comparator as follows:
a. Positive Input: FVR_pin
4
 This selects the internal Fixed Voltage Reference as the positive intput
to the Comparator.
 The Fixed Voltage Reference (FVR) provides a stable reference in 1x, 2x
or 4x a base 1.024 V. This voltage source will not vary with fluctuations in
the Microcontroller’s voltage supply which is a common issue on other
devices.
 Note: Selecting the FVR_pin as the Positive Input: automatically
adds the FVR::FVR peripheral to the Project Resources pane.
b. Negative Input: CIN1 Automatically connects the negative input pin C1IN1- in the Pin Manager
to the RC1 pin.
5
c. Enable Comparator Hysteresis:
 Comparator hysteresis provides a separation voltage to reduce the
effects of input noise.
6. Connect the comparator output signal C1OUT to pin RC5 (connected to LED D7 on
Curiosity Development Board) in the Pin Manager.
6
Configuring the Fixed Voltage Reference
7. Finally, select the FVR::FVR peripheral in the Project Resources window to open the
Fixed Voltage Reference user interface in the Composer Area. Configure the FVR
amount sent to Comparators, DAC and CPS: to 2x using the associated drop-down
menu. This will send a 2 x 1.024 base voltage = 2.048 V to the Comparator 1 positive
input as configured in Step 5.a.
8. Generate the code for the project by clicking on the Generate Code button.
When prompted to generate a main.c click Yes.
9. Pending any issues successful code generation will be indicated in the MPLAB Code
Configurator Window.
10. Compile the project and download to the PIC16F1619 by clicking the Make and
Program Device Main Project button
in the MPLAB X IDE Workspace.
7
Note: If MPLAB® X IDE displays a window indicating the target device is not
recognized, low-voltage programming may need to be enabled in the project
properties.
Enable Low-Voltage Programming Mode in Project Properties
a) Open File>Project Properties (Lab1)
b) In the Project Properties window, highlight Starter Kit (PKOB) in the Categories
pane.
c) On the right side of the Project Properties window, select Program Options from
the Option categories: drop-down menu
d) Make sure that the checkbox next to Enable Low Voltage Programming is
selected. Even if the checkbox is selected click Apply and then OK to close the
window.
8
Result
Following download to the Curiosity Development Board LED D7 should light when the
potentiometer POT1 is rotated counterclockwise driving the RC1 pin voltage below the 2.048 V
Fixed Reference Voltage.
9