ADCC Threshold Comparison Example

 Objective

The Analog to Digital Converter with Computation (ADCC) can be useful for a number of applications. One is monitoring a sensor and then measuring the value against a set of thresholds. In this project, the voltage on the MCU pin connected to the potentiometer on an MPLAB® Xpress Evaluation Board is converted to a 10-bit value using the Burst Averaging Mode feature of the Analog-to-Digital Converter with Computation peripheral. This 10-bit Filtered Value is then compared against an upper and a lower Threshold value. An interrupt is generated when the Filtered Value is higher than the Upper Threshold value or less than the Lower Threshold value. A printf statement is sent serially using the on-chip Enhanced/Addressable Universal Asynchronous Receiver Transceiver (EUSART) to the PIC® PIC16F18555 on the MPLAB Xpress board for conversion to the USB protocol. The message is then displayed in a terminal program running on a host computer to confirm that an interrupt has occurred.

overviewGraphic.jpg

 MPLAB® Xpress IDE Edition - Episode 8 - Using the ADCC Threshold Comparison Features

 Materials

Hardware Tools (Optional)

Tool About Purchase
Xpress-50px.png
MPLAB® Xpress
Development Board

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
MPLAB® Xpress
Cloud Integrated Development Environment
swtool-28px.png
CoolTerm FREE serial port terminal
http://freeware.the-meiers.org/

Exercise Files

File Download
Installation
Instructions
Windows Linux Mac OSX
Project and Source Files
Board Schematic

Additional Files

Files

 Procedure

To follow along with these steps, MPLAB Xpress should be open and you should be logged in so that the MPLAB Code Configurator (MCC) plug-in can be used.

1

Task 1

Create a new project in MPLAB Xpress for a PIC16F18555 using the MPLAB Xpress Development Board called adccThreshold.

Instructions are below if this is your first project.

2

Task 2

Open the MPLAB Code Configurator (MCC).

Instructions are below if this is your first project.

3

Task 3

Inside of the MCC, expand the ADCC heading in the Device Resources and double-click on the ADCC peripheral to add to the Project Resources.

ADCC_DeviceResources.jpg

4

Task 4

Select the ADCC peripheral under Project Resources > Peripherals to open the configuration window and configure as follows:

  • Ensure that Burst_average_mode is selected in the Operating drop-down menu.
  • Select FOSC/ADCLK from the Clock Source drop-down menu.
  • Select FOSC/2 under Clock to obtain an ADCC Clock period (TAD) of 2 µs.
  • Select Right from the Result Alignment drop-down menu
  • Enable Continuous Operation. This feature will continuously sample the input pin once the ADCC is triggered until the threshold conditions set up below are met.
  • Enable Stop on Interrupt. This feature will stop the conversion of the selected input pin when an interrupt has occurred. Note that the user code will need to re-trigger the ADCC with every interrupt.
  • Set Acquisition Time to 1. This will delay for 1 x TAD = 1 x 2.0 µs before initiating the conversion process when the ADCC receives a trigger. This is a newer feature of the ADCC replacing traditional delays that tied-up the processor.

The ADCC Easy Setup Hardware Settings should now resemble the image below

ADCCSetup2.JPG

5

Task 5

Expand the Computation Features section of the Easy Setup and configure as follows:

  • Select Filtered value vs setpoint from the Error Calculation drop-down menu. This will be the average result (Filtered Value) after the Burst Averaging has completed minus the Setpoint Value.
  • Leave the Setpoint at 0. Therefore, the calculated error value will simply be the Filtered Value.
  • Define the threshold limits as the following values:
    • Lower Threshold = 100
    • Upper Threshold = 500
  • Set the Repeat to 32 consecutive potentiometer pin reads every time the ADCC is triggered.
  • Set the Acc Right Shift value to 5. This will shift the sum of all 32 consecutive potentiometer pin conversions to the right by 5 bit positions effectively dividing the accumulated result by 25 = 32.
  • Enable ADC Threshold Interrupts and define the Threshold Interrupt condition by selecting ADERR < ADLTH and ADERR > ADUTH. This means that an interrupt will occur if the calculated error value (Filtered Value - Setpoint) following the Burst Averaging and right shift of 5-bit positions is either greater than the Upper Threshold value of 500 or less than the Lower Threshold value of 100.

The Computation Feature section should now resemble the image below.

computationFeatures.JPG

6

Task 6

In the Pin Manager pane, tie the RA4 pin that is connected to the potentiometer on the MPLAB Xpress Development Board to the ADCC input channel by clicking on the appropriate cell in the ANx row.

ADCCpinManage.jpg

7

Task 7

Highlight the Pin Module in Project Resources to open the Easy Setup pane. Edit the Custom Name field to rename the ADCC input channel to something more intuitive like POT.

pinModule.JPG

8

Task 8

In the Device Resources pane, scroll down to locate the EUSART heading, expand and double-click to add the EUSART peripheral to the Project Resources > Peripherals section.

EUSART_DeviceResources.jpg

9

Task 9

Select the EUSART peripheral in the Project Resources to open the Easy Setup Hardware Settings and configure as follows:

  • Enable Transmit
  • Redirect STDIO to USART

The Easy Setup Hardware Settings should now resemble the image below.

EUSARTsetup.JPG

10

Task 10

In the Pin Manager, connect the EUSART signals as follows:

  • TX: RC0
  • RX: RC1
EUSARTpinModule.JPG

11

Task 11

Click the Generate button in MCC to create the appropriate header and source files for this configuration. A main.c file will also be generated for the project.

figure8.png

Upon successful code generation the “Generation Completed” notification should appear. Select “OK” to close the window.

figure9.png

12

Task 12

Return to the MPLAB Xpress IDE and open the adcc.h header file found in the Project\adccThreshold\Header Files\MCC Generated Files folder tree.

Scroll down through adcc.h to locate the function ADCC_StartConversion(). This function triggers the ADCC conversion. Remember that the ADCC will continuously re-trigger after each Burst Averaging has completed to obtain a new Filtered Value and will only stop when an interrupt has occurred.

Highlight and copy the function.

startConversionFunction.JPG

Next, open main.c and scroll down to just before the while(1) loop in the main() function, paste the copied ADCC_StartConversion() and change the function arguments to the renamed potentiometer pin POT as shown below:

pasteADCCStartConversion.JPG

13

Task 13

Remember than an interrupt is generated whenever the 10-bit Filtered Value exceeds the Upper Threshold, or is less than the Lower Threshold defined earlier. Uncomment the INTERRUPT_GlobalInterruptEnable() and INTERRUPT_PeripheralInterruptEnable() macros towards the top of main() as shown below:

enableInterrupts.JPG

14

Task 14

Highlight the adcc.c source file for the ADCC peripheral under the adccThreshold\Source Files folder in the Project pane of MPLAB Xpress IDE.

Scroll through adcc.c to locate the function ADCC_ThresholdISR() at the bottom of the file. Add a printf statement to transmit the Filtered Value over the EUSART to confirm that an interrupt has occurred.

Immediately following the printf statement, add another ADCC_StartConversion() function call on the POT pin to start another round of ADCC input samples.

interruptServiceRoutine.JPG
    printf("\n\rADCC Value: %u        ", ADCC_GetFilterValue());
    ADCC_StartConversion(POT);

15

Task 15

The MPLAB Xpress Evaluation Board should be connected to an available USB port on the host computer through a USB cable to the micro B connector on the board. Drivers should install successfully the first time the board is connected and may take a minute or so. The connection is shown in the picture. No other components are required.

figure3.png

16

Task 16

Compile and download the project .hex file by clicking on the Make and Program Device button at the top of the MPLAB Xpress IDE.

figure13.png

17

Task 17

Program the MPLAB Xpress board by dragging the project .hex file from the downloads section of the browser and dropping the file on to the XPRESS drive.

figure14.png

The Programmer LED on the Xpress board should quickly flash from green to red and then back to green indicating that the .hex file was successfully programmed to the PIC16F18855.

18

Task 18

Open a terminal program on the host computer select Connect. In this example, we are using the free program CoolTerm.

coolTermConnect.JPG

 Results

Once communication is established with the MPLAB Xpress Board, the Terminal screen should only display an output value when:

  • The potentiometer is rotated clockwise until the POT pin voltage results in a 10-bit ADCC result that exceeds the Upper Threshold value of 500
  • The potentiometer is rotated counter-clockwise until the POT pin voltage results in a 10-bit ADCC result that is less than the Lower Threshold value of 100
burstAveraging.jpg
© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.