Hello World - Light an LED

 Objective

Hello World - MPLAB® Xpress Example

In this project, an LED (Light Emitting Diode) is turned on by thePIC16F18855 microcontroller using theMPLAB Xpress Evaluation Board. This is a great starting point for getting your Xpress board running.

figure0.png

 Materials

Hardware Tools

Tool About Purchase
Xpress-50px.png
MPLAB® Xpress
Development Board

Software Tools

Tool AboutInstallers
Installation
Instructions
Windows Linux Mac OSX
MPLAB® Xpress
Cloud Integrated Development Environment

Exercise Files

FileDownload
Installation
Instructions
Windows Linux Mac OSX
Project and Source Files

 Procedure

The PIC16F18855 GPIO pin RA0, connected to LED D2 on the Xpress board, is set up as an output to control the LED.

The project uses:

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 steps of this training module. You should see a screen similar to the one shown here to start.

figure1.png

1

Task 1

The MPLAB Xpress Development 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.

figure2.png

2

Task 2

Create a new project in MPLAB® Xpress for a PIC16F18855 using the MPLAB Xpress Development Board. Instructions are below if this is your first project.

1

Launch the new project wizard

Launch theNew Project Wizard using one of the following methods:

  • From the Toolbar: Click on theNew Project iconMain_New_Project.png, or
  • From the Menu: SelectFile > 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 bymodifying the project's properties.

2

Choose Project

a

Choose 'Microchip Embedded' from theCategories column (center).

b

Choose 'Standalone Project' from theProjects column (right).

c

Click theNext button.

create1.png
Click image to enlarge.

3

Select Device

a

Choose the device family from theFamily drop-down box.

If you have usedMPLAB® X IDE previously, choosing 'Recently Used' from theFamily 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 theDevice drop-down box. You may also type in the part number directly, and the list will adjust to the characters you enter.

create2.png
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!

create3.png
Click image to enlarge.

 How To Create A Project Video

This video shows how to 'Create a Project' inMPLAB Xpress.


3

Task 3

Open theMPLAB® Code Configurator (MCC) under theTools > Embedded > MPLAB Xpress Code Configurator menu of MPLAB Xpress.

figure4.png

A window will appear showing three steps. If you need the latest version ofJava you can click on step one, otherwise, click on the step two button to open theMPLAB® Xpress Code Configurator.

figure4_1.png

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.

figure4_4.png

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 amain.c file by clicking on theGenerate button near the center top of the screen. All these generated files will be included in your MPLAB Xpress project.

figure4_3.png

4

Task 4

There are three default Project Resources associated with any MPLAB Xpress project as shown here.
For this project, we only need to set up theSystem Module and thePin Manager. TheInterrupt Manager will not be used so they can be left in their default state.

Let's start with theSystem Module settings.

  • System Module
  • Interrupt Manager
  • Pin Manager

When theSystem Module selection is highlighted in the Project Resources area the System Module settings will appear in the center of the MCC screen.

figure3.png

For this project select the following:
Internal Oscillator

  • Oscillator Select: HFINTOSC (1MHz)
  • HF Internal Clock: 4MHz
  • Clock Divider: 4

WWDT

  • WDT Disabled, SWDTEN is ignored

5

Task 5

In the Pin Manager section select the pin RA0 Output by clicking on the blue unlock button, turning it to a green locked symbol.

  • System Module
  • Interrupt Manager
  • Pin Manager
figure9.JPG

The Xpress Board has RA0 connected to the LED D2. By making RA0 an output, the pin will control the LED.

Under the Easy Setup tab of the Pin Module section, you will see the output box already properly checked for the RA0 pin. It also allows you to change the name of the pin from IO_RA0 to a custom name such as "led".

At this step also check the box "Start High". This will drive the RA0 output pin high and light the LED.

figure10.png

6

Task 6

Click theGenerate button in MCC to create the appropriate header and source files for this configuration.

figure.png

Upon successful code generation, the “Generation Completed” notification should appear. SelectOK 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 newmain.c source file

figure11.png

7

Task 7

Click on themain.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 thewhile(1) loop.

figure19.png

This is highlighted to show you where you can modify the code to create a custom application. Fortunately, in this example, we don't need to add any code to control the LED because the MCC already set the pin high in the Pin Manager setup of Step 5. We had it initialize the RA0 pin to"Start High". This will keep it HIGH to light the LED. The initialization code generated by the MCC is in thepin_manager.c file. TheLATA = 0x01; line highlighted in the code block shows that the Least Significant Bit (LSB) of the LATCH A (LATA) register is set to a 1. This will drive the corresponding pin HIGH which is the RA0 pin connected to the LED D2. This is how the LED will continually be driven to an ON state.

8

Task 8

Compile and download the project .hex file by clicking on theMake and Program Device button at the top of the MPLAB Xpress IDE.

Main_Program_Target_Project.png

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.

figure13.png

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

The D2 LED will now be lit on the Xpress board.

figure14.png

 Analysis

The example shows how to control an I/O pin as an output to drive an LED on. The code shows how the MCC can generate initialization code so no additional code was required within themain.c file. Additional control of the LED can be performed by adding code tomain.c under thewhile(1) loop. There are many I/O control macros established in thepin_manager.h file to make it easier to create that code.

 Conclusions

This project can be the basis for building any project that requires an I/O pin to control a digital output signal. This can include various circuits such as transistor controlled relay or LED displays. This will form the basis of just about any microcontroller based project.