Serial Communication - MPLAB® Xpress Example
In this project, a serial communications link is established with the PIC16F18855 MCU populating the MPLAB Xpress Development Board and a host PC. This communication is enabled by a PIC18LF25K50 MCU which is also populated on the Xpress board and is configured for two different USB classes to perform the following tasks:
- Configured using the USB Mass Storage Device (MSD) Class, receives .hex files from the host PC and then programs the PIC16F18855 using Microchip Technology’s In-Circuit Serial Programming (ICSP™) technology
- Translates EUSART data transmissions from the PIC16F18855 and transmits over the USB connection to the host PC using the USB Communications Device Class (USB CDC)
Using the latter CDC configuration enables the PIC16F18855 to send and receive information through the PIC18LF25K50 to an available USB port on a host computer. Terminal emulation software running on the host computer connects to the associated COM Port and can then be used to display information from the PIC16F18855 or allow the user to send commands back to the Microcontroller.
Hardware Tools (Optional)
|Tool|| About|| Purchase|
| Windows|| Linux|| Mac OSX|
Cloud Integrated Development Environment
| Windows|| Linux|| Mac OSX|
Project and Source Files
This video shows the step by step creation of this project. The detailed written description is further below on this page under the Procedure heading.
The PIC16F18855 EUSART peripheral connects on pins RC1 (TX) and RC0 (RX) to a PIC18LF25K50 configured to operate as a CDC USB device to translate the EUSART message for transmission over the USB protocol to a host computer COM Port.
The project uses:
- MPLAB Xpress Cloud-Based Integrated Development Environment with MPLAB Code Configurator Plug-in: mplabxpress.microchip.com
- PIC16F18855 Microcontroller: www.microchip.com/pic16f18855
- PIC18LF25K50 Microcontroller: www.microchip.com/pic18f25k50
- MPLAB Xpress Development Board
- FREE Terminal Emulation Software:
Windows users will need to install the appropriate signed drivers in order to use the CDC configuration on the PIC18LF25K50 as detailed below. Linux and MAC OS users may skip over these two steps.
- The Windows signed drivers can be downloaded directly here.
- Once the inf – Composite Devices.zip has been downloaded, extract the files to the C:\DRIVERS\WIN directory.
To follow along with these steps, MPLAB® Xpress should be open and the user should be logged in so that the MPLAB Code Configurator plug-in can be used. The setup is described in the Setup and Installation section of this training module. You should see a screen similar to the one shown here.:
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.
Create a new project in MPLAB® Xpress for a PIC16F1855 using the MPLAB Xpress Development Board. 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 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, setup the oscillator system and other configuration settings, and input and output selections for your device. When all this is competed you can generate project code including a main.c file. All these generated files will be included in your MPLAB Xpress project.
In the open MCC window the default parameters for the System resource can be used but you will need to add the EUSART peripheral to this project and setup the pin connections for the communication.
In the Device Resources area scroll down to locate the EUSART peripheral and expand. Double-click on the EUSART to add the peripheral to the Project Resources.
The Enable EUSART box should be checked. Enable Transmit should also be checked. The Baud Rate drop down should be set to 19200. Finally the Redirect STDIO to USART should be checked since this project will be using PRINTF statements to send data from the EUSART peripheral.
The setup in MCC should look like this:
* System Module
* Interrupt Module
* Pin Module
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.
New MCC Generated header and source files should now be present in the Project window of the MPLAB Xpress IDE including a new main.c source file.
Click on the main.c source file in the Project pane to open the file and scroll through the code to locate the // Add your application code comment inside of the while(1) loop inside of the main().
At this point add a simple printf statement that will be used to execute a transmission over the EUSART and eventually display on the host computer. A simple statement enclosed in double quotes is all that is needed.
printf("Hello from MPLAB Xpress! \n \r");
The user may wish to add newline (\n) and carriage return (\r) commands as shown.
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.
Open a terminal emulator program on the host computer and select the COM port associated with the MPLAB Xpress board. In this example a free program called TeraTerm is used. Note that the COM port number will probably be different from that shown below.
Configure the terminal emulation software to communicate at the 19200 baud rate that was configured earlier in the project when configuring the EUSART in MCC.
Once communication is established, the terminal window should display the text enclosed in the printf statement added earlier in the project.
The example shows how to perform one way communication. The Xpress board sends the data as ASCII characters that is then displayed in the terminal window as text.
This project can be the basis for building a two-way communication project where data entered in the terminal window is sent back to the Xpress board and the data can be used in another algorithm.
Table of Contents