0% found this document useful (0 votes)
162 views12 pages

Mplab Xc8 Getting Started Guide

This document provides steps to create a project in MPLAB X IDE using the MPLAB XC8 C compiler, including: 1. Setting the project type as a standalone project 2. Selecting the target device 3. Optionally selecting device headers and tools 4. Naming the project and path 5. Creating a main.c source file with skeleton code 6. Compiling and building the project code

Uploaded by

Tutorias Cardona
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
162 views12 pages

Mplab Xc8 Getting Started Guide

This document provides steps to create a project in MPLAB X IDE using the MPLAB XC8 C compiler, including: 1. Setting the project type as a standalone project 2. Selecting the target device 3. Optionally selecting device headers and tools 4. Naming the project and path 5. Creating a main.c source file with skeleton code 6. Compiling and building the project code

Uploaded by

Tutorias Cardona
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

MPLAB

Xc8
Getting Started
Guide

CREATION OF A PROJECT IN
MPLAB X IDE:
This section describes how to create a project in MPLAB X
IDE using the MPLAB XC8 C Compiler.
The process is explained in the following steps:
Step 1 sets the project type.
Step 2 selects the target device.
Step 3 selects the device header.
Step 4 selects the tool to run the project code.
Step 5 is only applicable to some debugger tool selections.
Step 6 selects the tool to compile the source code.
Step 7 specifies the project name and path.
Step 8 completes creation of the project.

Step 1 sets the project type:


From MPLAB X IDE, choose File>New Project.... In the window that
opens (as shown in Figure 1-1), select the Microchip Embedded
category, and a Standalone Project from the Projects field.

Step 2 selects the target device:


This selection must exactly match the device on your hardware. (If
you are using the simulator without hardware, you can choose any
device.)
To make selecting a device simpler, devices are organized into
families. MPLAB XC8 can compile for any device in the 8-bitmicrocontroller families. In Figure 1-2, a PIC18F87J11 has been
selected from the PIC18 family.

Step 3 selects the device header:


The use of debugging features is not required by this guide, so this
selection could be None, as shown in Figure 1-3.

Step 4 selects the tool to run the project code:


If you have a debugger and wish to use it with your hardware, select
it from the list; otherwise, select Simulator. Figure 1-4 shows MPLAB
REAL ICE selected as the programmer/debugger to run the generated
code.

Step 5 is only applicable to some debugger tool


selections:
Unless you must use a specific plugin board, select None (when/if the
dialog shown in Figure 1-5 appears)

Step 6 selects the tool to compile the source code:


As shown in the Select Compiler window in Figure 1-6, several
versions of the MPLAB XC8 compiler might be listed under the XC8
disclosure widget. Select the most recent version. You will be able to
change this selection during development.

Step 7 specifies the project name and path:


Type a name for the project in the Project Name field. Click Browse... if the
default project path is not suitable. In this case, the name quick_start_XC8
has been chosen for illustration purposes and can be seen in Figure 1-7.
5

To differentiate the current project in the IDE (when multiple projects exist)
as the main project, click Set as main project.

Step 8 completes creation of the project:


Click Finish, and the project is created. An icon representing the
project appears in the Projects window1, as shown in Figure 1-8. The
Projects window is shown at the top left in the figure. Below the
Projects window, the Dashboard provides more detailed project
information.

Foundation Code:
The code presented here is actually a small program that could be the
basis for all of your MPLAB XC8 projects. Although it could be
construed as trivial, the code is entirely valid, and compiles and
executes, as required
The code creation process is explained in the following linked steps:
Step 1 creates a new source file.
Step 2 enters a suitable name for the source file.
Step 3 adds skeleton code to the new file.
Step 4 saves your work.

Step 1 creates a new source file:


With MPLAB X IDE, there are several ways to create a source file. The
following method is the most basic, and moves through the all
aspects of the source creation process.
As shown in orange in Figure 1-9, right click on the project icon that
represents the new project you created, quick_start_XC8. Select
New>C Source File... on the resulting pop-up command and target
lists.

Step 2 enters a suitable name for the source file:


Ensure that the project name is correct.
As illustrated in Figure 1-10, the settings will create a file called main.c. After
clicking Finish, an icon representing the file should appear in the Project
window. The file is also opened in the text editor. At this time, the file is
empty.

Step 3 adds skeleton code to the new file:


Copy or type the following text into the new source file, main.c
#include int main(void) { return 0; }
8

This initial code can be used to start every project built with MPLAB
XC8. C. Every C program must have one, and only one, function called
main(); however, the exact prototype for this function can vary from
compiler to compiler. For all MPLAB XC compilers, you may use the
prototype shown above. Since main() returns an int, there must be a
return statement with a return value specified. The value 0 indicates
that main() returned successfully.
The inclusion of the header file, , allows code in this source file to
access compiler- or device-specific features. Since such access is
commonplace, you will need to include it into virtually all of your
source files.

Step 4 saves your work:


Select File>Save to ensure that your work is saved.
If you are not using MPLAB X IDE, you may enter the above program
into a file using any editor, as long as it is saved as plain text and the
file uses the .c extension.

COMPILATION:
As mentioned previously, the new program is a valid C program. This
means it can be compiled. This section explains how to build code.
MPLAB X IDE knows which compiler to execute when building your
source code, but options can be used to alter how the compiler
operates. The default options are acceptable for most projects. If you
do need to adjust the compiler options, you can do so from the Project
Properties dialog. Open this dialog using the top button on the left in
the project dashboard, as seen in Figure 1-11. From this dialog you
can also change other project attributes, such as the device or
compiler associated with your project.

There are several ways to execute the compiler. There are buttons on
the toolbar to enable quick access to the different build operations,
but you can also access these from the Run and Debug menus. Some
operations only build your code; others build and then execute your
code. Both the build and run steps can be made in either a release or
10

debug mode.
Debug mode operations enable the debug executive on your device.
This allows access to debugging features like breakpoints. For the
debug executive to be active, it must utilize some of the devices
memory that normally would be available for your code. Debug builds
ensure that this memory is reserved for the debug executive.
Release mode operations do not allow any debug features to be used,
but all the device memory is available for your project. This is the
build mode you would use to produce a production image suitable for
products you intend to release.

From left to right, the identified buttons perform the following


functions:
Build (release) any project source files modified since the last build,
then link
Build (release) all project source files, then link
Build (release) any project source files modified since the last build,
link, then download and run your code
Build (debug) any project source files modified since the last build,
link, then download and run your code with the debug executive
enabled.
For the purposes of this demonstration, click Build, or Clean and Build.
The compiler associated with your project will be invoked, and every
source file in your project (currently only one) will be built and linked
into one binary image. You will see a transcript of the build process in
the Output window, which will open if it is not already visible in the
workspace. This might look similar to that shown in Figure 1-13.

11

Note that there are several warnings relating to missing configuration


settings1, but these have not stopped the compilation process and
the BUILD SUCCESSFUL message at the lower part of the window
indicates that the code was built. The red error, at the bottom of the
Output window, indicates a mismatch in the code compiled and the
target device. The next section explains how to configure the device,
which will resolve these warnings and the error.
If you are building from a terminal, use the following command line
. xc8 --chip=18f87j11 main.c
Adjust the device and source file name, as appropriate. If the compiler
is not in the search path, you should use its full path, in addition to
the application name, xc8.

12

You might also like