Analog Read Serial Write Using the ADCC Peripheral
Objective
In this project, the voltage across the potentiometer on an MPLAB® Xpress Development board is converted to a 10-bit value using the Analog-to-Digital Converter with Computation peripheral. The value is then sent to the EUSART peripheral on the PIC16F18855 to be transmitted to the other PIC® MCU on the Xpress Board to convert and then transmitted over the USB connection to a computer to display in a terminal emulator program.
Launch the New Project Wizard using one of the following methods:
From the Toolbar: Click on the New Project icon , or
From the Menu: Select File > New Project, or
The project wizard will walk you through the process and prompt you for all the required information. Your selections may be changed after the project has been created by modifying the project's properties.
2
Choose Project
a
Choose 'Microchip Embedded' from the Categories column (center).
b
Choose 'Standalone Project' from the Projects column (right).
c
Click the Next button.
Click image to enlarge.
3
Select Device
a
Choose the device family from the Family drop-down box.
If you have used MPLAB® X IDE previously, choosing 'Recently Used' from the Family drop-down box will filter the 'Device' list to the 3 devices you have used most recently.
b
Choose your device's part number from the Device drop-down box. You may also type in the part number directly, and the list will adjust to the characters you enter.
Click image to enlarge.
4
Select Project Name and Folder
a
Input a project name. This will be your project's name that the IDE will create.
Note: The name cannot have any empty spaces!
Click image to enlarge.
How To Create A Project Video
This video shows how to 'Create a Project' in MPLAB Xpress.
2
Task 2
Open the MPLAB Code Configurator (MCC).
Instructions are below if this is your first project.
Open the MPLAB® Code Configurator (MCC) under the Tools > Embedded > MPLAB Xpress Code Configurator menu of MPLAB Xpress.
A window will appear showing three steps. If you need the latest version of Java™ you can click on step one, otherwise, click on the step two button to open the MPLAB® Xpress Code Configurator.
A file will automatically download in your browser similar to the picture shown. Click on that file to open it. This is a Java application that will launch the MCC.
The process can take several seconds depending on your internet connection speed. Several windows may appear asking if you want to run the program. When the process is complete you will see a new screen appear, separate from the MPLAB Xpress IDE, that is the MCC control screen. In this screen, you can select peripherals for your project, system setup for the oscillator and other configuration settings, input and output selections for your device. When all of these are completed you can generate project code including a main.c file by clicking on the Generate button near the center top of the screen. All these generated files will be included in your MPLAB Xpress 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.
4
Task 4
Select the ADCC peripheral under Project Resources>Peripherals to open the configuration window and configure as follows:
Ensure that Basic_Mode is selected in the Operating drop-down menu.
Select Right from the Result Alignment drop-down menu.
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 following image.
5
Task 5
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.
6
Task 6
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".
7
Task 7
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.
8
Task 8
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 following image.
9
Task 9
In the Pin Manager, connect the EUSART signals as follows:
TX: RC0
RX: RC1
10
Task 10
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.
Upon successful code generation the “Generation Completed” notification should appear. Select OK to close the window.
11
Task 11
Return to the MPLAB Xpress IDE and open the adcc.h header file under the Project>analogReadSerialWrite>Header Files>MCC Generated Files folder tree.
12
Task 12
Scroll down through the adcc.h header file to locate ADCC_GetSingleConversion(). This function will trigger a single ADCC conversion and is the same as the legacy ADC function call ADC_GetConversion() readers who’ve used the ADC peripheral on other devices may recognize.
Highlight and copy the function.
13
Task 13
In the Project pane, highlight the main.c source file to open.
14
Task 14
Scroll down through main.c to locate the while(1) loop inside of main(). Add the following printf statement pasting copied function from the previous step inside the statement as shown, changing the argument inside the brackets to the custom name POT:
printf(“\n\rThe ADC Value is: %i “,ADCC_GetSingleConversion(POT));
\n: Creates a new line.
\r: Returns the cursor.
%i: Indicates that an integer value will be inserted at this point in the output.
POT: The custom name given to the pin RA4 connected to the potentiometer on the Xpress Board.
15
Task 15
The MPLAB Xpress Development 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.
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.
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.
18
Task 18
Open a terminal program on the host computer select Connect. In this example the free program called CoolTerm is used.
Results
Once communication is established with the MPLAB Xpress Board, the Terminal screen should display a value between 0 and 1023 depending on the rotation of the potentiometer.
Analysis
The ADCC value output can be used to calculate the voltage across the potentiometer as per the following:
(1)
\begin{align} Voltage = {\frac{V_{REF}}{ADC Resolution}}\times\text{ADCC Value} \end{align}
Example:
If the ADC Value is 368, this equates to:
(2)
\begin{align} Voltage = {\frac{3.3V}{2^{10}-1}}\times\text{ADCC Value}={\frac{3.3V}{1023}}\times368= 1.19V \end{align}
Where: VREF is the Positive Reference selected when the ADCC was configured. This was left at the default of VDD (PIC16F18855 supply voltage) or 3.3 V.