Universiti Malaysia Perlis: School of Electrical Systems Engineering
Universiti Malaysia Perlis: School of Electrical Systems Engineering
SCHOOL OF
ELECTRICAL SYSTEMS ENGINEERING
EET 203
MICROCONTROLLER SYSTEMS DESIGN
Module 1b
Introduction to MPLABX IDE & xC8 Compiler
EET 203 [MICROCONTROLLER SYSTEMS DESIGN] MODULE 1b
OBJECTIVES
OBJECTIVES
· To introduced MPLABX IDE & xC8 Compiler as a tool to develop applications for Microchip
microcontrollers
EQUIPMENT / COMPONENTS
OBJECTIVES
· MPLAB X IDE
· xC8 Compiler
· Computer Unit
INTRODUCTIONS
OBJECTIVES
MPLAB X IDE is a software program that runs on a PC (Windows®, Mac OS®, Linux®) to develop
applications for Microchip microcontrollers and digital signal controllers. It is called an Integrated
Development Environment (IDE), because it provides a single integrated "environment" to develop code
for embedded microcontrollers. MPLAB X Integrated Development Environment brings many changes to
the PIC® microcontroller development tool chain. Unlike previous versions of the MPLAB IDE which were
developed completely in-house, MPLAB X IDE is based on the open source NetBeans IDE from Oracle.
Taking this path has allowed us to add many frequently requested features very quickly and easily, while
also providing us with a much more extensible architecture to bring you even more new features in the
future.
Microchip’s line of award-winning MPLAB® XC C Compilers provides a comprehensive solution for your
project’s software development and is offered in free, unrestricted-use downloads. Finding the right
compiler to support your device is simple:
• MPLAB XC8 supports all 8-bit PIC MCUs
• MPLAB XC16 supports all 16-bit PIC MCUs and dsPICs
• MPLAB XC32/32++ supports all 32-bit PIC MCUs
When combined with Microchip’s award-winning, free integrated development environment, MPLAB® X
IDE, the full graphical frontend provides:
• Editing errors and breakpoints that match corresponding lines in the source code
• Single stepping through C and C++ source code to inspect variables and structures at critical
points
• Data structures with defined data types, including floating point, display in watch windows
PROCEDURE
OBJECTIVES
PART A: USING MPLAB IDE
MPLAB X IDE is the latest integrated development environment from Microchip Technology. It is the
upgrade from MPLAB IDE v8.xx and previous versions. To start MPLAB X IDE, double click on the icon
installed on the desktop after installation and a screen will display the MPLAB X IDE logo followed by the
MPLAB X IDE desktop as in Figure 1 and Figure 2 respectively.
Figure 1
Figure 2
In order to create code that is executable by the target PICmicro MCU, source files need to be put into a
project. The code can then be built into executable code using selected language tools (assemblers,
compilers, linkers, etc.). Since all the laboratory exercise or project used xC8 compiler, do remember to
install this compiler. In MPLAB X IDE, the project manager controls this process.
· Create Project
MPLAB X Project Wizard will be used to create a Project.
· Create Code
Some code will be added to the template file to send an incrementing value out an I/O port.
· Build Project
The project will be built – causing the source files to be assembled and linked into machine code that
can run on the selected PICmicro MCU.
· Test Code
Finally, the code will be tested by loading it to PTK40A and run the program.
MPLAB X IDE is project-based, so you must set up a project to work on your application. New projects
can be created by selecting either:
· Start Page,
Learn and Discover tab, “Dive In”, “Create New Project”
· From the menu bar on top of the window, click File>New Project (or Ctrl+Shift+N) as shown in
Figure 3.
Figure 3
In Step 1, you need to choose a project category. The following options are available to choose for a
project category:
• Standalone Project – Create a new C and/or assembly code project. This type of project is
shown in this section.
• Existing MPLAB IDE v8 Project – Convert your existing MPLAB IDE v8 project into an MPLAB X
IDE project. For details see “Import MPLAB Legacy Project” in the Help Menu.
• Prebuilt (Hex, Loadable Image) Project – Load an existing project image into MPLAB X IDE. For
details, see “Prebuilt Projects” in the Help Menu.
• User Makefile Project – Create a project that makes use of an external makefile. For details, see
“Create User Makefile Projects” in the Help Menu.
• Library Project – Create a new C and/or assembly code project that will build into a library,
instead of an executable hex file. For details, see “Library Projects” in the Help Menu.
In EET203 laboratory, you needs to choose project category as a Standalone Project as shown in Figure
4. After you have made your selection, click Next> to move to the next dialog.
Figure 4
In Step 2, you required to selects the device you will be using in your application from the “Device”
drop-down list. To narrow your selection list, chose a Family first.
Choose Family Mid-Range 8-bit MCUs and then choose the Controller (PIC16F877A) from the device
drop down and then click Next>. Figure 5 shows the selection before Next > button being pressed.
Figure 5
In Step 3, you must select selects the programmer that compatible with your hardware. Supported tool
for the selected device is signified by the colored circles (lights) in front of the tool name. Figure 6 shows
the meaning of every light. If you cannot see the colors, mouse over a light to pop up text about
support.
Figure 6
For hardware tools that can be used as either a debugger or a programmer, there are two lights next to
the tool name, where the first light is the left-most light and the second light is to the right of the first,
specifying:
1. Debugger Support
2. Programmer Support
Also for hardware tools, you will notice that a serial number (SN) is specified below any tool that is
connected to your computer. This allows you to select from several connected hardware tools.
Select PICkit2 as the programmer as shwin in Figure 7 and then click Next>.
© Copyright School of Electrical Systems Engineering, UniMAP. 2018 (V1.2) 7
EET 203 [MICROCONTROLLER SYSTEMS DESIGN] MODULE 1b
Figure 7
In Step 4, you must selects the appropriate language tool, either a C compiler or an assembler. Again,
the colored circle (light) in front of the compiler name signifies the device support level. Mouse over it
for text. Figure 8 shows the current Microchip Language Tools and the supported devices.
Figure 8
Figure 9
In Step 5, you can selects the project name, location and other project features as shown in Figure 10.
Enter the project name. By default, the name will be appended with .X. This is not required and
simply a convention. You can delete this if you wish in the “Project Folder” text box.
You can create a new project folder if you need one. By default, projects will be placed in:
C:\Users\UserName\MPLABXProjects
However, you may choose to put them in your own location.
• Main Project
Check “Set as main project” to specify this project as your main project.
Check “Use project location as project folder” to create the project in the same folder as the
project location. This is useful if you are importing an MPLAB IDE v8 project and want to
maintain the same folder for your MPLAB X IDE project (as when you want the build to be the
same). MPLAB X IDE uses a folder to store a project information whereas MPLAB IDE v8 uses a
.mcp file. Therefore you can only have one MPLAB X IDE project share a folder with an MPLAB
IDE v8 project (.mcp file).
• Encoding
Select the encoding for the project. The default is ISO-8859-1 (Latin 1) character set. This
selection will specify the code syntax coloring, Fonts and Colors button, Syntax tab, which can
be edited under Tools>Options.
In every project created during the laboratory session, you are required to save the project in the folder
with named ‘your matric number’ on drive D. Using Browse button, browse your folder and open the
folder. Project named “Module_1” can be created by typing the project name in the column for “Project
Name”. When you are done, click Finish to complete new project creation.
Figure 10
After pressing the Finish button, review the Project Window on the MPLABX desktop. It should look like
the Figure 11.
Figure 11
PART C: CREATE A SOURCE FILE
1. Source file is a file that you will write the program by using C language. To start create source
file, go to File > New File. Figure 12 shows the process.
Figure 12
2. New window as in Figure 13 will appeared which you required to Choose File Type. In this
project, choose C Main File as file types and then press Next >.
Figure 13
3. After that, you need to set the source File Name as shown in Figure 14. Use “Module1” as the
filename for this source file. The given file name will automatically set to .c as set by the
Extension part. After that press Finish to complete the process.
Figure 14
4. After pressing the Finish button, the window as in Figure 15 will appear.
Figure 15
5. The other way to create new source file is by right click at folder Source File and go to New >
main.c as shown in Figure 16. Then the process will continue as step 3.
Figure 16
1. In this course, we will use C as a programming language for Microcontroller. The first step
writing C is preprocessor directive. Preprocessor directive is a command to the C
preprocessor (which is automatically invoked as the first step in compiling a program). The
two most common preprocessor directive are the #define directive, which substitutes text
for the specific identifier and the #include directive, which includes the text of an external
file into program.
2. Declaration establishes the names and attributes of variables, functions and types used in
the program. Global variables are declared outside functions and are visible from the end of
the declaration to the end of the file. A local variable is declared inside a function and is
visible from the end of the declaration to the end of the function.
3. Definition establishes the content of the variable or function. A definition also allocates the
storage needed for variables and functions.
6. All C programs must contain a function named main where program execution begins. The
braces that enclose the main function define the beginning and ending point of the
program.
7. Retype the program in Example 1 (make sure you check all semicolon and braces to avoid
error) as shown in Figure 17.
8. Once you have complete, Save the file the go to Production>Build Project. The Output
window shows the result of the build process as can been seen in Figure 18. There should be
no errors on any step.
Figure 17
Figure 18
EXAMPLE 1
#include<xc.h>//This is the preprocessor directive which include file xc.h into the program
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit
(RB3 is digital I/O, HV on MCLR must be used for programming)
#define _XTAL_FREQ 20000000 //This is the preprocessor directive which is define the frequency to
20MHz
unsigned char i;
unsigned char j; //This is to define character i and j
i=0b00000001;
j=0b10000000;
EXERSICE
OBJECTIVES
A set of eight (8) LED is connected to one of the port pins of a PIC microcontroller. TheLED is
flashed continuously as in the following sequence after the push button is pressed.