Multiplex Analog Inputs Into PIC18F56Q71 Operational Amplifier

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

Objective

This tutorial shows how to configure the PIC18F56Q71 operational amplifier (op amp) feature to switch between two analog inputs 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 how to set up the op amp as a simple unity gain buffer to read two incoming signals by the ADC, which are then displayed in MPLAB Data Visualizer using UART.

For this demonstration, we use MPLAB X Integrated Development Environment (IDE), MPLAB XC8 compiler, and the PIC18F56Q71 on a Curiosity Nano Development board. As a stand-in for an analog sensor, a waveform generator is used; however, this will work with any connected potentiometer or appropriate analog sensor.

The software is comprised of the main routine and an Interrupt Service Routine (ISR). After the system initializes, Timer 2 starts and the ADC is read, the UART Tx flag is checked, a UART write occurs, and the device loops back to the ADC read. Whenever Timer 2 triggers, the interrupt code switches the input pin. While the op amp pin is swapped, a register change is in the ADC peripheral, because writing to the ADPCH special function register automatically reconfigures the op amp to match when one of the op amp inputs in ADPCH is used.

Configuration 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 microcontroller (MCU) demonstration board to observe the expected behavior.

Lab Objectives

  • Create an MCC project for a PIC18F56Q71 MCU from scratch.
  • Use MPCC 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 block diagram of the peripheral configuration. Just like Lab 1 and Lab 2, the op amp buffers an incoming signal that is subsequently read by the ADC, then the CPU takes that value and writes it to the UART to be displayed on the TX pin RB4. The difference is using Timer 2 to trigger an interrupt at set intervals to reconfigure the op amp. This way the op amp is switching between input pins RA2 and RA4.block diagram of the peripheral configuration

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 ZIP file page

Back to Top

Procedure

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 your device. This demonstration uses a PIC18F56Q71 device and Curiosity Nano tool.
New Project window Select Device pane

Back to Top


 Select Application Project and click Next.
New Project window Choose Project 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 shield

Back to Top


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

Project Resources pane

Back to Top


Since the internal oscillator for the PIC18F56Q71 Curiosity Nano is being used, open Configuration Bits under Project Resources make the following selections:

  • External Oscillator Selection: Oscillator Not Enabled
  • Reset Oscillator Selection: The appropriate oscillator for your project. (This demonstration is using the 64 MHz 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

Back to Top


Then set the PLIB to UART2 on the window that pops up.
Easy View tab

Back to Top


Once UART2 appears, change the Requested Baudrate to 115200.
Easy View tab

Back to Top


Next, add the ADC from Devices Resources.
Device Resources pane

Back to Top


Under the ADC’s Easy View, change the result alignment to left justified to read just the high byte from the ADC result register.
Easy View tab

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

Back to Top


 This demonstration uses the Timer 2 peripheral to trigger an interrupt to reconfigure the op amp every 4 seconds, so next, add the Timer 2 peripheral under Device Resources.
Device Resources pane

Back to Top


Under Timer Clock, change the Clock Source to LFINTOSC.
Easy View tab

Then change the Prescaler to 1:128, and the Post Scaler to 1:4.
Timer 2 tab

Back to Top


Now adjust Timer 2’s period to seconds.
Timer 2 tab

Back to Top


 And lastly, enable the TMR Interrupt Enable.
Interrupt Settings
Now to set up the op amp peripheral. This demonstration uses a Unity Gain Configuration and will start with OPA1IN0+ as its first input.

Back to Top


Add OPA1 from under Device Resources.
Device Resources pane

Back to Top


Under the OPA1 Easy View, change the Op Amp Configuration from Direct Connection to Pins to Unity Gain Buffer.
Easy View tab

Make sure Enable Internal Output is selected.
Easy View tab

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 and the RX2 pin to RB5.

Pin Grid View

Information

Note: The UART RX pin 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.
Generate button

Once generated, switch from the Resource Manager tab on the left and open Projects, navigate to your project’s source files and open main.c. Enable global interrupts by uncommenting INTERRUPT_GlobalInterruptEnable(); and add TMR2_Start(): before the while loop and the following inside the while loop:

while(1)

{

ADC_ChannelSelectAndConvert(ADPCH);

while(! (UART2_IsTxReady(~)~)~);

UART2_Write(ADRESH);

}

Back to Top


After including the code for main.c, go back to the projects tab and under Source Files, open MCC Generated Files>timer>src>tmr2.c scroll down to TMR2_DefaultPeriodMatchCallback() and include this:Included code

This will function as a Timer 2 interrupt to swap the op amp input pins between both signals. The values used for the input channel are available in the ADPCH table in the ADC section of the device data sheet, and are visible under the ADC Register View.
CTX1_ADPCH: 0X84

CTX1_ADPCH: 0X82

Back to Top


Click Make and Program Device Main Project.
​ Make and Program Device Main Project button

Back to Top


Connect the first waveform generator output, potentiometer, or analog sensor to pin RA2 and the second to pin RA4.
Waveform generator output

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 (Play icon) and click Play.

The accompanying image shows the results in Data Visualizer.

Results in Data Visualizer

Here is the displayed ADC reading as the device switches between both signals from the waveform generator passing through the unity gain op amp. The first signal is a sine wave and the second is a triangle wave, every 4 seconds the microcontroller switches between each incoming signal providing readings from the corresponding inputs.

Back to Top

Results

You observed the application switching analog inputs 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