8-Bit Analog Input Project

Signal voltages are not always digital such as zero volts or five volts. Many times they are analog, in that they can vary in voltage level, and that is where an analog-to-digital converter is used to convert the input signal from an analog voltage level to a digital value that the microcontroller can process. This project monitors a potentiometer controlled voltage that lights up an LED when the threshold is crossed.

proj3.png

Step by Step Instructions

Using the MPLAB® Code Configurator (MCC), this project creates an analog input to read a potentiometer and control an LED from a digital output. When the potentiometer is turned right of the center position, the LED will light up. When the potentiometer is turned left of the center position, the LED will be off.

proj3bd.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 IDE 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, these peripherals will need to be selected:

  • System Module
  • Interrupt Module
  • Pin Module
  • ADCC (Foundation Services …)

The System Module, Interrupt Module, and Pin Module will be automatically included, but the ADC must be selected for the project. The list of peripherals will show up under the Device Resources list. Double-click on the ADC to have it added to the project. The result should look like the picture below.

proj3per.png

4

Open the Pin Manager and then click on the PORTA pin 4 (RA4) blue lock symbol in the Output row to make it locked and green. This adds this RA4 I/O pin to the project to drive the LED.

Click on the PORTA pin 0 blue lock in the ANx row to make it green. This adds the RA0/AN0 input to the project. Vref+ and Vref- should be left to their default values.

proj3io2.png

5

Close the Pin Manager and then click on the Pin Module in Project Resources to open the center section. It will show RA4 and RA0 listed on the I/O chart.

gpio.png
  1. The RA4 pin will be made an output and control the LED. Click on the Output box to make the pin an output and uncheck the WPU and Analog boxes.
  2. Change the RA4 pin name to "D2_LED".
  3. The RA0 pin is the potentiometer input, so check the Analog box and uncheck the Output and WPU boxes if checked.
  4. Change the RA0 pin name to "Pot_A0".
proj3ioset.png

6

The System Module needs to be set up next. Click on System Module under Project Resources.

system.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.

oscillator.png

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

lvp.png

7

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

adc.png
  1. Check the ADC Enable box and select the FOSC/ADCLK Clock Source from the drop-down menu and the FOSC/2 Clock.
  2. Select right for Result Alignment. This will produce a 10-bit ADC result.
  3. Leave Vref+ as VDD and Vref- as VSS.

After proper set up, your window should look like the setup below.

adcsetup.png

8

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

generate.png

9

The project will now have both generated Header Files and Source Files. It should also have a generated a main.c file.

projfiles3.png

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

mainfile.png

10

The ADC result will produce a value between 0 and 1023 depending on the position of the potentiometer. A value of 0 is the position all the way counterclockwise. A value of 1023 is all the way clockwise. The half-way point is a value of 512. We shall test the position by comparing the value to 512. If it is larger than 512, then the LED will be lit. If it is smaller than or equal to 512, then the LED will be off.

Add the following code to the end of the main.c file:

while (1) {
        ADCC_Initialize();

        if (ADCC_GetConversion(Pot_A0) < 512) {
            D2_LED_SetHigh();
        } else {
            D2_LED_SetLow();
        }
    }

The ADCC functions in main.c are generated by MCC and defined in the adcc.h file.

This macro initializes the ADCC peripheral:
ADCC_Initialize()
This macro is used to select desired channel for conversion:
ADCC_GetConversion(Pot_A0)

The D2_LED_SetHigh() and D2_LED_SetLow() functions are macros that were generated by MCC, and their definitions are located in the pin_manager.h file.

#define D2_LED_SetHigh()            do { LATAbits.LATA4 = 1; } while(0)
#define D2_LED_SetLow()             do { LATAbits.LATA4 = 0; } while(0)

11

Click on the Build Project icon (the hammer) to compile the code. You should see a "BUILD SUCCESSFUL" message in the output window.

Main_Build_Project.png
BUILD SUCCESSFUL

12

Make sure your project has the Curiosity Board selected and USB cable is connected to the board.

Click on Make and Program Device. 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 and the D2 LED connected to RA4 will be lit when the potentiometer is turned to the right of center on the HPC Curiosity Board.

Main_Program_Target_Project.png

Output Window:

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 D2 (RA4) LED will light up when the 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.
proj3final.jpg

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

13

The project can be closed in MPLAB X IDE. 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.

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 3 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.