Create a Unity Gain Amplifier Using PIC18F56Q71

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

Objective

This tutorial shows how to configure the PIC18F56Q71 operational amplifier (op amp) feature to create a unity gain amplifier using Microchip’s MPLAB® Code Configurator (MCC) Meldoy. 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 will demonstrate how to set up the op amp as a simple unity gain buffer for an incoming signal to be read by the ADC and displayed in Data Visualizer using UART. For this demonstration, we will use the MPLAB X Integrated Development Environment (IDE), MPLAB XC8 compiler, and PIC18F56Q71 on a Curiosity Nano Development board. As a stand-in for an analog sensor, a waveform generator will be used; 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.

main.c diagram

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.

Back to Top

Lab Objectives

  • Create an MCC project for a PIC18F56Q71 MCU from scratch.
  • Use MCC Melody to configure and generate Peripheral Libraries (PLIB) code for the following peripherals:
    • Op amp
    • ADC
    • UART
    • GPIO

Back to Top

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.

Connection Diagram

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 you double-check and use the same versions 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.

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.

Download ZIP

Back to Top

Procedure

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

Back to Top


Select Application Project. Then, click Next.

Application Project > Next

Back to Top


Select your device. (This demonstration is using a PIC18F56Q71 device and the Curiosity Nano tool.)

Select Device > Next

Back to Top


Select a compiler.
Select compiler

Back to Top


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

 Project name and location

Back to Top


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

Back to Top


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

MCC > Resource Management > Project Resources

Back to Top


Since the PIC18F56Q71 Curiosity Nano and an internal oscillator are being used, find the Configuration Bits under Project Resources and make the following selections:

  • External Oscillator Selection: Oscillator Not Enabled
  • Reset Oscillator Selection: The appropriate oscillator for your project. (This demonstration is using the 64MHz HFINTOSC.)

  External Oscillator Selection and Resert Oscillator Selection

Back to Top


 Now we need to set up the test bench to display the output. From Device Resources, add UART.

Device Resources

Then, set the PLIB to UART2 on the window that pops up.

UART2 selection

Back to Top


 Once UART2 appears, change the Requested Baudrate to 115200.

UART2 Easy View setting selections

Back to Top


 Next add the ADC from Device Resources.
Device Resources > ADC

Back to Top


 Under the ADC Easy View, change the result alignment to left justified

Left Justified

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

OPA1IN0+

Back to Top


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

add OPA1

Back to Top


 Under the OPA1 Easy View, change the Op Amp Configuration to Unity Gain Buffer.

Unity Gain Buffer

Then, select Enable Internal Output.

enable the Internal Output

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

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 the Generate button under Project Resources.

Generate Button

Back to Top


 Once generated, switch from the Resource Manager tab on the left to Projects. Navigate to your project’s source files and 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

Back to Top


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

Connected waveform generator, potentiometer, or analog sensor to pin RA2.

Back to Top


Open Data Visualizer Data Visualizer logo. Under the device Curiosity Nano menu, click the serial COM port gear. Change the baud rate to 115200.

Back to Top


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

Back to Top


Data Visualizer results are shown in the accompanying image.  The displayed ADC reading of the signal from the waveform generator passing through the op amp buffer can be seen. Notice how the signal is oscillating between points 0 and just below 120 on the y-axis.

Results in Data Visualizer

Back to Top


Results

You observed the application generating a unity output using the PIC18F56Q71 MCU!

Back to Top

Analysis

You have successfully demonstrated op amp unity gain functionality 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 amp labs (see the "Learn More" section).

Back to Top

Learn More

Back to Top