Create a Unity Gain Amplifier Using PIC18F56Q71
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.
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.
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
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.
Procedure
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. Then, click Next.
Select your device. (This demonstration is using a PIC18F56Q71 device and the Curiosity Nano tool.)
Select a compiler.
Name your project and assign its location. Then, click Finish.
After the project is created, click the blue MCC shield at the top.
From the MCC interface, locate the Resource Management tab, and navigate to Project Resources.
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.)
Now we need to set up the test bench to display the output. From Device Resources, add UART.
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.
Under the ADC Easy View, change the 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 to Unity Gain Buffer.
Then, select Enable Internal Output.
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 the Generate button under Project Resources.
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:
{
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 . Under the device Curiosity Nano menu, click the serial COM port gear. Change the baud rate to 115200.
Add the COM port to the time plot ( ) and click Play.
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
You observed the application generating a unity output using the PIC18F56Q71 MCU!
Analysis
You have successfully demonstrated op amp unity gain functionality 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 amp labs (see the "Learn More" section).
Learn More
- 8-bit PIC® MCU Peripheral Summary
- All 8-bit Parts with Operational Amplifiers
- Operational Amplifier Module
- Introduction to Operational Amplifiers
- PIC18-Q71 Data Sheet
- PIC18F56Q71 Curiosity Nano Hardware User Guide
- Develop a Non-Inverting Amplifier Using PIC18F56Q71
- Multiplex Analog Inputs Into PIC18F56Q71 Operational Amplifier