Objective
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.
Materials
Hardware Tools (Optional)
Tool | About | Purchase |
---|---|---|
| |
Software Tools
Tool | About | Installers |
Installation
Instructions |
||
---|---|---|---|---|---|
Windows | Linux | Mac OSX | |||
MPLAB® Xpress
Cloud Integrated Development Environment |
| |
Exercise Files
File | Download |
Installation
Instructions |
||
---|---|---|---|---|
Windows | Linux | Mac OSX | ||
Project and Source Files
|
| | | |
Procedure
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 (MCC) Plugin mplabxpress.microchip.com
- [www.microchip.com/pic16f18855 PIC16F18855 Microcontroller]
- [www.microchip.com/pic18f25k50 PIC18LF25K50 Microcontroller]
- MPLAB Xpress Development Board
- FREE Terminal Emulation Software
Windows users need to install the appropriate signed drivers in order to use the CDC configuration on the PIC18LF25K50 as detailed below. Linux® and MacOS® 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 MCC plugin 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.:
1
Task 1
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.
2
Task 2
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.
3
Task 3
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 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, set up the oscillator system and other configuration settings, and input and output selections for your device. When all this is completed you can generate project code including a main.c file. All these generated files will be included in your MPLAB Xpress project.
4
Task 4
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 set up 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 dropdown 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
- System Module
- Interrupt Module
- Pin Module
Peripherals
- EUSART
6
Task 6
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.
7
Task 7
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");
You may wish to add a newline (\n) and a carriage return (\r) commands as shown.
9
Task 9
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.
10
Task 10
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.
Results
Once communication is established, the terminal window should display the text enclosed in the printf statement added earlier in the project.
Analysis
The example shows how to perform one-way communication. The Xpress board sends the data as ASCII characters that are then displayed in the terminal window as text.
Conclusions
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
|