Develop a Non-inverting Amplifier Using PIC18F56Q71
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.
Lab Objectives
There are two ways to use this tutorial:
- Create the projects from scratch:
- Follow the step-by-step instructions to create the required software.
- 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
- PIC18F56Q71 Curiosity Nano Evaluation Kit (EV01G21A)
- USB Type-C® cable for programming/debugging
- A signal generator to generate input signal, but can also be a potentiometer or an analog sensor
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.
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.
To begin, first open MPLAB X IDE and connect the Curiosity Nano Development board.
Click the New Project button in the top left corner of the menu.
Select Application Project and click Next.
Select your device. This demonstration uses a PIC18F56Q71 device and Curiosity Nano tool.
Select a compiler.
Name your project, assign its location, and click Finish.
After the project is created, click the blue MCC shield at the top.
From the MCC interface, navigate to Project Resources under the Resource Management tab.
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.)
Now we need to set up the test bench to display the output. Start by adding UART under Device Resources.
Then set the PLIB to UART2 on the window that pops up.
Once UART2 appears, change the Requested Baudrate to 115200.
Next add the ADC from Device Resources.
Select the ADC tab and from its Easy View tab, change Result Alignment to left justified.
Under Context 1, change the Positive Channel Selection to OPA1IN0+.
Now let’s add the op amp. Navigate back to Device Resources, find the OPAMP module and add OPA1.
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.
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.
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.
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.
Click Generate under Project Resources.
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:
{
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.
Click Make and Program Device Main Project.
Connect the waveform generator, potentiometer, or analog sensor to pin RA2.
Open Data Visualizer and under the device Curiosity Nano, menu click the gear for the serial COM port and change the baud rate to 115200.
Add the COM port to the time plot () and click play.
The accompanying image shows the 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.
Results
You observed the application generating a non-inverting amplified output using the PIC18F56Q71 MCU!
Analysis
You have successfully demonstrated an op amp non-inverting amplifier using MCC Melody. Using MCC Melody provides many benefits, including:
- Supports MCC Builder, a structured relationship manager that offers a transparent visualization of component-related dependencies and context in your project.
- Enables seamless portability across MCUs via system drivers that abstract hardware-level dependencies.
- Enables content versioning at the driver level, offering increased flexibility and easy upgradability.
- Available in MPLAB X IDE and MPLAB Xpress, a cloud-based IDE.
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).
Learn More
- 8-bit PIC® MCU Peripheral Summary
- All 8-bit Parts with Op Amps
- Operational Amplifier Module
- Introduction to Operational Amplifiers
- PIC18F26/46/56Q71 Datasheet
- Create a Unity Gain Amplifier Using PIC18F56Q71
- Multiplex Analog Inputs Into PIC18F56Q71 Operational Amplifier