Microchip | The Embedded Control Solutions Company Register Login
Browse All
Microchip Technology
 369 Code Snippets

Reading Analog Signals using the MPLAB Xpress Development Board

Microchip Technology

Check out this project's MPLAB Xpress Webpage to view the complete code example along with a detailed step by step write-up and description, or visit the PIC16F18877 Device Product Page for more information.

About this Code Example

Learn how to combine CIP usage with basic programming to build an ADCC application:

Project Overview

 Once you understand how to work with the PIC16F18877, the next step is to be able to get it to interact with the world outside the chip itself. In this tutorial we will connect a potentiometer to the chip's Analog to Digital Converter with Conversion (ADCC). This is Microchip's updated ADC that has additional functionality to increase the accuracy of the readings, as well as being completely core independent. While this is not necessarily important using a basic potentiometer, the added functionality is invaluable in applications that require filtering or averaging of input signals. The ADCC enables these fine measurements to be performed without use of the CPU, and can be completely configured in MCC, saving you time on the front end programming and CPU resources in the application. This application takes the readings of the ADCC and creates a 10-bit value which is passed on to the PWM module via written code. Finally, the PWM module produces a value which changes the brightness of an LED according to the potentiometer value.

Tools

Steps

To follow along with these steps, MPLAB® Xpress should be open and the user should be logged in so that the MPLAB Code Configurator plug-in can be used. The setup is described in the Hello World tutorial in the PIC16F18877 microchip series of examples. You should see a screen similar to the one below to move on to step 1:

Step 1

We will attach an LED to the board via two jumper wires and a breadboard, through a 1K Ohm resistor. Connect a wire between the RA0 pin on the evaluation board and the resistor. Then, connect the positive (longer) leg of the LED to the resistor. Next, connect a wire between the negative (shorter) leg of the LED and the GND pin on the evaluation board. Finally, connect a potentiometer whose wiper pin (usually middle) goes to RA4 on the Xpress Board and an addtional pin goes to the common ground (GND). You can see this picture below:

Step 2

The MPLAB Xpress Evaluation Board should be connected to an available USB port on the host computer through a USB cable to the micro B connector on the board. Drivers should install successfully the first time the board is connected and may take a minute or so. The connection is shown in the picture below. No other components are required.

Step 3

Create a new "Stand-Alone" project in MPLAB® Xpress for a PIC16F18877 using the MPLAB Xpress Development Board.

Step 4

Open the MPLAB® Code Configurator using the blue shield link on the toolbar. 

A file will automatically download in your browser similar to the picture shown. Click on that file to open it. This is a java application that will launch the MCC.

The process can take several seconds depending on your internet connection speed. Several windows may appear asking if you want to run the program. When the process is complete you will see a new screen appear, separate from the MPLAB Xpress IDE, that is the MCC control screen. In this screen you can select peripherals for your project, setup the oscillator system and other configuration settings, and input and output selections for your device. When all this is completed you can generate project code including a main.c file. All these generated files will be included in your MPLAB Xpress project.

There are three default Project Resources in MCC associated with any MPLAB Xpress project: System Module, Pin Module and Interrupt Module (as shown in the figure below). For this project, the only thing we need to update is the Pin Module. Rename the ADCC input to "pot" for easier naming convention in the written code, as well as unchecking the Analog box for the PWM pin:

However, we need an ADC, a PWM (to send the brightness signal to the LED), and a TMR (to run the PWM). Double click these options from the menu below Device Resources:

Step 5

Analog-to-Digital Converter: The ADC is used to collect information from transducers, which gather information from the physical world, and turn it into electrical signals. Analog data, the type of data gained from the physical world, can have an almost infinite range of values. However, computers must work with only ones and zeros. Therefore, an ADC converts the physical world range of values into computer ones and zeros, with higher bit resolution providing higher accuracy.

For the purposes of this application, there are a few things to change in the ADCC block. Update the Clock Source to FRC. This sets the clock to a dedicated RC oscillator, keeping your ADCC from being adversely affected by the system clock.

Step 6

We also need to update TMR4 to change the Clock Source to FOSC/4. This change is necessary because we are using the Timer for PWM timing, and PWM timing only runs with FOSC/4. The Timer Period will automatically change to 1.024 ms, which is ok for this application.

Step 7

PWM: This project uses the ADCC to update the duty cycle value of a PWM signal, which in turn changes the brightness of the LED. Change the PWM block timer to TMR4. Also, notice that the period value found for the Timer is the same as the PWM period:

Step 8

Click the Generate button in MCC to create the appropriate header and source files for this configuration. Upon successful code generation the “Generation Completed” notification should appear. Select “OK” to close the window.

Step 9

Return to the project pane window in the MPLab Xpress online IDE:

This window shows many things including the files generated by MCC. In this window, you can find header and source files that contain functions to change or use the associated modules generated in MCC at runtime. Double clicking one of the header files will open a second tab in the project pane with comments on the proper use of each function. This is where we will get the function to read the ADCC and place that value into the PWM duty value.

Programming: This tutorial requires us to add some code to the main.c file in the Source Files in the Project. In order to learn to interface with the software in the code, it is always helpful to look in the Header Files to find the functions for the different blocks being used.

Under the adcc.h header file, we find the function:

adc_result_t ADCC_GetSingleConversion(adcc_channel_t channel);

which returns the current ADC value. The adc_result_t gives the type of variable that the function returns, and the adcc_channel_t channel gives the source of the input variable. Therefore, we want to replace adcc_channel_t channel with the pot variable (with which we replaced the name of RA4).

Second, we want to be able to control the PWM duty cycle with the ADC output. Therefore, we name the output variable of the above function PotVal and give this to the function:

void PWM6_LoadDutyValue(uint16_t dutyValue);

found in the pwm6.h header file.

This results in the code below:

while(1)
{adc_result_t dutyValue = ADCC_GetSingleConversion(Pot);
PWM6_LoadDutyValue(dutyValue);
}

When you are finished, it should look like this:

Step 10

Press the Make and Program icon  to generate and download the .hex file for the created project.

Step 11

Program the MPLAB Xpress evaluation board by dragging the project .hex file from the downloads section of the browser and dropping the file on to the XPRESS drive. 

Results

If everything is working properly, as you turn the potentiometer, the LED should change brightness. The ADCC reads the voltage output of the potentiometer wiper and converts this to a 10-bit digital value. This value is then given to a function that uses the number as the duty value of a PWM signal. Turning the potentiometer changes the value, and in turn changes the PWM signal sent to the LED. This changes the brightness of the LED.

If the LED never turns on, or never turns off, you may need to change the resistance values of the potentiometer or the 1k Ohm resistor in series with the LED.

Make sure you properly rename the pins in the MCC window, and that you have properly loaded all the MCC modules with their correct settings.

Categories

Release History

  • Dec 4, 2018

    Version: 1.0.0

    Initial Release

Download Contact the Developer

*Required Fields


  • Current Version:1.0.0
  • Created:Dec 4, 2018
  • Updated:Dec 4, 2018
  • Downloads:726
  • MPLAB Version:MPLAB X
  • C Compiler:1.4.1
  • Development Tools:MPLAB Xpress Development Board
  • Supported Devices:PIC16F18877

Corporate News

Resources

Stay Connected

Stay up to date with the latest news from Microchip Technology.