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.
MPLAB® Xpress IDE Edition - Episode 8 - Using the ADCC Threshold Comparison Features
Materials
Hardware Tools (Optional)
Tool | About | Purchase |
---|---|---|
| |
Software Tools
Tool | About | Installers |
Installation
Instructions |
||
---|---|---|---|---|---|
Windows | Linux | Mac OSX | |||
MPLAB® Xpress
Cloud Integrated Development Environment |
| | |||
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.
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
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.
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.
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:
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:
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.
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.
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.
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.
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
Table of Contents
|