Multiplex Analog Inputs Into PIC18F56Q71 Operational Amplifier
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.
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 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
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 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.
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
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 your device. This demonstration uses a PIC18F56Q71 device and Curiosity Nano tool.
Select Application Project and click Next.
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 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.)
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 Devices Resources.
Under the ADC’s Easy View, change the result alignment to left justified to read just the high byte from the ADC result register.
And under Context 1, change the Positive Channel Selection to OPA1IN0+.
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.
Under Timer Clock, change the Clock Source to LFINTOSC.
Then change the Prescaler to 1:128, and the Post Scaler to 1:4.
Now adjust Timer 2’s period to 4 seconds.
And lastly, enable the TMR Interrupt Enable.
Now to set up the op amp peripheral. This demonstration uses a Unity Gain Configuration and will start with OPA1IN0+ as its first input.
Add OPA1 from under Device Resources.
Under the OPA1 Easy View, change the Op Amp Configuration from Direct Connection to Pins to Unity Gain Buffer.
Make sure Enable Internal Output is selected.
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.
Click Generate under Project Resources.
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:
{
ADC_ChannelSelectAndConvert(ADPCH);
while(! (UART2_IsTxReady(~)~)~);
UART2_Write(ADRESH);
}
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:
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.
Click Make and Program Device Main Project.
Connect the first waveform generator output, potentiometer, or analog sensor to pin RA2 and the second to pin RA4.
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 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.
Results
You observed the application switching analog inputs 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).