Hello, my name is Stephen Cowden, Applications Manager at Microchip Technology.
Today, we're going to use the MPLAB® Code Configurator to set up the ADC and EUSART.
We'll start the development from where we left off on the previous Overview video of the MPLAB Code Configurator
where we used the tool to set up a port pin high.
For this video, we'll continue to use the PICDEM 2 demo board with a PIC16F1937 installed,
but we're also going to use the MCP2200 USB to RS232 board.
This allows to see the EUSART communication on the PC.
For the dev tools, we'll be using the MPLAB X version 1.90, XC8 compiler version 1.21 and the MPLAB Real ICE in-circuit emulator.
OK, let's get started.
Let's start MPLAB X and open the MCCDEMO project we used for the overview video.
Once it's open, go to [Tools] menu
and then select the [MPLAB Code Configurator] under [Embedded].
Notice that when you open the Code Configurator
it successfully read the previous configuration and restored all your settings from the previous video.
OK, let's select the ADC from the Device Resources area first.
Once selected, it appears up in the Project Resources area.
The ADC user interface comes up in the Composer area, and it's added to the list of peripherals in the Table view of the Pin Manager.
Note there is also a warning in the output window, alerting the user that at least one I/O need to be configured for the ADC.
The PICDEM 2 board has a potentiometer on the RA0 pin.
So, let's select that pin for the ADC in the Pin Manager.
This can be done simply by clicking on the little blue open lock on the RA0 pin.
Notice that when you select the pin for the ADC the lock will lock and turn green,
and the other little blue lock in the GPIO is no longer available.
OK, after we select the ADC, let's move over to the ADC user interface in the Composer area and set up the ADC.
Enable the ADC via the [Enable] checkbox.
Right justify the ADC results and leave the positive and negative references at VDD and VSS.
We have to also customize the I/O name for the selected ADC channel.
Since we have a potentiometer on RA0, let's call the I/O "Pot."
Notice that when we changed the name of the I/O pin in the user interface it's also reflected in the Package View of the Pin Manager.
Well, that takes care of the ADC, let's set up the EUSART now.
Let's select the EUSART, Asynchronous mode, from the Device Resources area.
Notice Code Configurator automatically assigns the pins for the UART.
You can see this via the Package View and the Table View of the Pin Manager.
Since the pins are already set up for us, let's enable the UART,
and enable Transmit in the UART user interface in the Composer area.
So, if you want to see what's being transmitted out the UART on the PC,
let's also select the Redirect Standard I/O to EUSART check box.
Doing this will set up all the necessary code for using the standard I/O library
which will allow us to use the printf format string in our code.
And that's it for the UART. We are ready to generate the code.
OK, let's click on the "Generate Code" button and close the Code Configurator for now.
If you go to the project folder and expand the "header files and source files" folders,
you could see the files the Code Configurator generated.
We generated an additional ADC.h /.c and a EUSART.h/.c.
Let's open main.c and scroll down into the while loop
and we'll just ad one line of code to our project. The one line of code will be a printf statement
and it goes like this: printf("pot_value = %d\r\n", ADC_GetConversion (Pot));
Next, type ADC, and we use MPLAB auto-fill feature by pressing Ctrol+Space bar.
Let's select the ADC_GetConversion function from the list of functions,
and type in the word Pot in the parentheses, and a semicolon at the end of the line.
and that's it.
Let's make and program the device. Now, let's open a terminal program on the PC.
You should see the MCP2200 as a USB serial port.
After selecting it, you should see the string above with the measured ADC value from the potentiometer.
Simply turn the potentiometer on the RA0 pin,
you should see the measured values of the ADC change from rail to rail.
And there you have it. We've set both the ADCand EUSART using the MPLAB Code Configurator within a matter of minutes.
It's almost like cheating.
Well, thank you for your time and if for additional information on Microchip's wide range for development tools,
please go to www.microchip.com/MPLABX