Develop a Non-inverting Amplifier Using PIC18F56Q71

Last modified by Microchip on 2025/05/13 12:12

Objective

This tutorial shows how to configure the PIC18F56Q71 operational amplifier (op amp) feature to create a non-inverting amplifier using Microchip’s MPLAB® Code Configurator (MCC) Melody. MCC Melody is used to quickly configure peripheral drivers such as op amps, Universal Asynchronous Receiver/Transmitter (UART), Analog-to-Digital Converter (ADC), and General-Purpose Inputs/Outputs (GPIOs) using a graphical user interface.

Overview

This lab demonstrates of how to set the op amp up as a non-inverting amplifier for an incoming signal to be read by the ADC and displayed in Data Visualizer using UART. We use the MPLAB X Integrated Development Environment (IDE), MPLAB XC8 compiler and PIC18F56Q71 on a Curiosity Nano Development board for this demonstration. A waveform generator is used as a stand-in for an analog sensor; however, this will work with any connected potentiometer or appropriate analog sensor.

The software is straightforward. After the system initializes, the ADC read occurs, the UART Tx flag is checked, a UART write occurs and the device loops back to the ADC read.

workflow

Lab Objectives

There are two ways to use this tutorial:

  1. Create the projects from scratch:
    • Follow the step-by-step instructions to create the required software.
  2. Use the solution projects as an example:
    • Build the solution projects and program them into the appropriate MCU demonstration board to observe the expected behavior.

Materials

Information

Note: The Curiosity series evaluation boards include an onboard, embedded debugger. No external tools are necessary to program or debug the PIC18F56Q71 Curiosity Nano boards. For programming and debugging, the debugger connects to the host PC through the USB Type-C connector on the Curiosity Nano boards.

Back to Top

Connection Diagram

Here is a basic block diagram of this demo’s unity gain configuration. The incoming signal is picked up by the op amp on pin RA2, which is then internally connected to the ADC peripheral. The CPU is running a simple conversion command and then sends it to the UART peripheral with the TX output routed to pin RB4.

unity gain configuration block diagram

Information

Note: This project has been verified to work with the following versions of software tools:

Because we regularly update our tools, occasionally you may discover an issue while using the newer versions. If you suspect that to be the case, we recommend that you double-check and use the same versions that the project was tested with.

Back to Top

Lab Solutions

This ZIP file contains the completed solution projects for this lab. The contents of this ZIP file can be placed in a folder of your choice. Both files are stored in a single GitHub repository. You will need to create a free account to download the files.

Information

Note: Because MCC generates source and header files and libraries under the project folder, the contents of this ZIP file can be placed in any folder of your choice. 

Download the lab.

GitHub repository

Back to Top


 To begin, first open MPLAB X IDE and connect the Curiosity Nano Development board.

Back to Top


Click the New Project button in the top left corner of the menu.

New Project button

Back to Top


Select Application Project and click Next.

New Project window Choose Project pane

Back to Top


Select your device. This demonstration uses a PIC18F56Q71 device and Curiosity Nano tool.New Project window Select Device pane

Back to Top


Select a compiler.
New Project window Select Compiler pane

Back to Top


Name your project, assign its location, and click Finish.

 New Project window Select Project Name and Folder pane

Back to Top


After the project is created, click the blue MCC shield at the top.
MCC icon

Back to Top


From the MCC interface, navigate to Project Resources under the Resource Management tab.

Project Resources pane

Back to Top


Since the PIC18F56Q71 Curiosity Nano and an internal oscillator are being used, Configuration Bits under Project Resources should be opened.  Make the the following sections:

  • External Oscillator Selection: Oscillator not enabled 
  • Reset Oscillator Selection: The appropriate oscillator for your project. (This demonstration uses the 64MHz HFINTOSC.)

Configuration Bits tab

Back to Top


Now we need to set up the test bench to display the output. Start by adding UART under Device Resources.
Device Resources pane
Then set the PLIB to UART2 on the window that pops up.

UART2 Easy View

Back to Top


Once UART2 appears, change the Requested Baudrate to 115200

UART2 Easy View

Back to Top


Next add the ADC from Device Resources.
Device Resources pane

Back to Top


Select the ADC tab and from its Easy View tab, change Result Alignment to left justified

Easy View tab

Under Context 1, change the Positive Channel Selection to OPA1IN0+.Context 1 tab

Back to Top


Now let’s add the op amp. Navigate back to Device Resources, find the OPAMP module and add OPA1.

Device Resources pane

Back to Top


Under the OPA1 Easy View change the Op Amp Configuration from Direct Connection to Pins to Non-Inverting Programmable Gain Amplifier, then select Enable Internal Output.
Hardware Settings pane

Back to Top


Scroll down to Programmable Gain and Feedback Selection and use Internal Resistor Ladder Selection to select the appropriate gain. This example will use a R2/R1 of 1.

Programmable Gain and Feedback Selection pane

Back to Top


Check for the desired gain. The gain of a non-inverting configuration will be (1+R2/R1), meaning this example will have a gain of 2.

Back to Top


Next, navigate down to the Pin Grid View to assign the RX and TX pins for the UART2 peripheral. Assign the UART2 TX2 pin to RB4.

Pin Grid View

Information

Note: The UART RX pin on this board is RB5; it has been assigned for the sake of completing the UART set up, but is not required in this demo.

Back to Top


Click Generate under Project Resources.
Project Resources Generate button

Once generated, switch from the Resource Manager tab on the left to Projects and navigate to your project’s source files. Open main.c and add the following code to the while loop:

while(1)

{

ADC_ChannelSelectAndConvert(ADPCH);

while(! (UART2_IsTxReady()));

UART2_Write(ADRESH);

}

There are many ways to take an ADC reading and send it to UART including using the DMA peripheral to eliminate the CPU from the process, however, ADC_ChannelSelectAndConvert() is the easiest for the purposes of this demonstration.

Back to Top


Click Make and Program Device Main Project.

Make and Program Device Main Project icon

Back to Top


Connect the waveform generator, potentiometer, or analog sensor to pin RA2.waveform generator connection

Back to Top


Open Data Visualizer  Data Visualizer icon and under the device Curiosity Nano, menu click the gear for the serial COM port and change the baud rate to 115200.

Back to Top


Add the COM port to the time plot (time plot icon) and click play.

The accompanying image shows the results in Data Visualizer.

Results in Data Visualizer

Here is the displayed ADC reading of the signal from the waveform generator passing through the op amp in a programmable gain amplifier configuration. The incoming signal from the waveform generator is the same, but the new reading is double the previous signal and maxes out at a reading of approximately 240 on the y-axis.

Back to Top

Results

You observed the application generating a non-inverting amplified output using the PIC18F56Q71 MCU!

Back to Top

Analysis

You have successfully demonstrated an op amp non-inverting amplifier using MCC Melody. Using MCC Melody provides many benefits, including:

  1. Supports MCC Builder, a structured relationship manager that offers a transparent visualization of component-related dependencies and context in your project.
  2. Enables seamless portability across MCUs via system drivers that abstract hardware-level dependencies.
  3. Enables content versioning at the driver level, offering increased flexibility and easy upgradability.
  4. Available in MPLAB X IDE and MPLAB Xpress, a cloud-based IDE.

Back to Top

Conclusion

This tutorial provided you with training for configuring the PIC18F56Q71 MCU. As a next step, you can complete the other two associated op amps (see the "Learn More" section).

Back to Top

Learn More

Back to Top