Watch the Video Tutorial

The aim of this course is to teach you how to develop microcontroller based electronic systems using Microchip XC8 (MPLAB X) Compiler.

MPLAB® X IDE is the new Microchip IDE and it runs on a PC with Windows®, Mac OS® or Linux® to develop applications for PIC microcontrollers and replaces all MPLAB® C and HI-TECH compilers.
XC8 is the new C compiler for PIC10, PIC12, PIC14, PIC16 and PIC18 microcontrollers.
This compiler offers different optimization levels to suit your needs with FREE downloads available.
You can download a free copy of  MPLAB® X IDE and XC8 Compiler from Microchip website.
You can also download the Microchip XC 8 Compiler User’s Guide
For PIC32 microcontrollers XC32 is used and for PIC24 and dsPIC XC16 is used.

 Starting a new Project

Let us create a simple project called “Flashing_LED” to switch on and off an LED connected to PORTB.0 with 1 sec interval.

Assuming you have installed the latest version of MPLABX and XC8 compiler, you can start a new project by following these few steps below illustrated with screenshots. We are going to use PIC18F2220 but it should be the same for the rest of microcontrollers.

Depending on your operating system used, open MPLAB X IDE either form a shortcut on the desktop or from all programs.
Go to File menu and click on New Project to start a new project.

Select Standalone Project and click Next.

Choose the PIC Family. In our case because we want to use PIC18F2220, we will select the Advanced 8-bit (PIC18)

Select the PIC to be used. In our case, we will select PIC18F2220. Then click Next.

Select the PIC to be used. In our case, we will select PIC18F2220. Then click Next.

Choose the hardware tool to program and debug your project. We will choose Pickit3 from Microchip.  Ckick Next.

All Microchip compilers installed on your computer will be shown here. Select XC8. If you don’t see it, it means you have not installed it. Go to Microchip website to install a free compiler which is fully functional but without code optimization. Click Next.

Enter the name of the project and select the location where to save it by clicking on the browse button.

Click Finish to finish the wizard.

 Setting Configuration Bits

Setting the configuration bits is very important if they are not correctly set, the processor will operate in an unknown manner or often not start at all.
Click on Window menu, PIC Memory Views then Configuration Bits to open the configuration bits window.

The important settings are the Clock sources (oscillator), low voltage and the reset (MCLR, watchdog timer…).

Always switch ON what you need and switch off the rest.

Clock Settings  

The oscillator settings depend on frequency of the clock, the type of the clock source (crystal, RC…) and whether an internal or external clock is to be used. Your application will determine whether to use an internal or external oscillator and which frequency to use.

Usually it’s cheap to use the PIC internal oscillator especially with newer PIC models but for higher frequencies that your internal oscillator is not able to provide or for applications requiring accurate timing like  USB, CAN, High speed asyncronous serial you want to use an external oscillator.

Don’t set your frequency higher than required, the higher the frequency the higher the power consumption and this can be a huge problem especially for battery powered applications.
Common oscillator settings are XT, HS, EC, RC and INT.
The XT and HS settings are for use with external crystal oscillators, XT is used for crystals 4 MHz and below, HS for 4 MHz and above. EC is for use with an external TTL/CMOS clock source. RC oscillator mode is for using an external RC resonator (usually 8 MHz and below) and INT specifies the processors internal oscillator if equipped.
This figure shows how a crystal can be used as a clock input.

Please always check the PIC datasheet to get the accurate values of C1 and C2 of the oscillator. Higher capacitance increases the oscillator stability but also increases the start-up time.
The table below gives the recommended values.

Resetting a Microcontroller

When a microcontroller is reset, it starts the execution of the program from the beginning of the program (address 0x0000). There are times when a reset is required, this can be done automatically (when a microcontroller is in an endless loop to avoid freezing, very low voltage to avoid unexpected operation…) or even manually when a user wants to force to microcontroller to restart its program.

These following operations can reset the microcontroller:

  • POR (Power On Reset)
  • MCLR reset
  • WDT (Watchdog timer) reset
  • BOR (Brown-out reset)
  • Reset instruction
  • Stack full reset
  • Stack underflow reset

Usually the commonly used reset types are the POR and external reset using the MCLR pin.
The Power-On Reset is generated automatically when power supply voltage is applied to the chip.
The MCLR pin should always be connected to the supply voltage directly or preferably through a 10-K resistor.
To manually reset the microcontroller, the MCLR can be grounded, normally with a pushbutton.
If a manual reset is not needed, the MCLR can be disabled in software.
This figure below shows a typical reset circuit.

Configuration Bits

It is always recommended to disable the low voltage programming field unless low voltage programming is required to prevent possible processor resets. 
The watchdog timer should also be disabled since an infinite loop will be used unless needed.

In this configuration bits window below, we are to select an internal Oscillator so no need for an external crystal oscillator, The MCLR will be disable so no need to reset the PIC with the MCLR and no need to connect a resistor to it as well. The rest of the features are switched off. We will use them in the future.
When done, click on Generate Source Code to Output.

Copy the generated code and paste it in your main C file or preferably create a new C file  (delete the content of the file after the #Include <xc.h> line). IN our example we created a new C file named Settings.C
This is how the content of the header file will look like.

At the bottom of the file, specify the frequency of the oscillator with the  #define _XTAL_FREQ command. The following command below will set the internal oscillator frequency to 8MHz.

Another option is  by coding the configuration bits inside the main file. The following are some common settings for the configuration bits using the #pragma config command:

By placing any of these #pragma config statements after any #include statements, essentially is the same as choosing configuration bit settings from the menu.
The configuration settings for any PIC18 processor can be found in the PIC18 Configuration Settings Addendum.

Creating The C Main File

Right Click on Source Files, then New, then select C Main File. Here is where we are going to write our main program.

Give the C file a name, preferably same as the project name, a dot C file will be generated. Click Finish.

Let us write our code to blink an LED connected to PORTB.0 as shown on the figure below:

In the C main File, don’t forget to include our Settings.C file as shown line 11 below.

                   Building The Project

Now click on Run menu then Build Project(Flashing_LED). The build should be successful.

To get the Hex file, Open your project folder, dist, default and then production. you can now download your Hex file into your PIC with your programmer.

Blink an LED with MPLAB Code Configurator

The MPLAB® Code Configurator (MCC) is a user friendly Graphical User Interface (GUI) plug-in tool for MPLAB® X IDE which generates easy to understand C code that is inserted into an MPLAB® X project, based on the settings, peripherals configurations and selections made in the Graphical User Interface (GUI).  The advantage of  MCC , it can generate codes not only for PIC18F but for a wide range of PICs including PIC16F and PIC24 series.

To learn more, read the article: MPLAB® Code Configurator

Watch the Video Tutorial

Below is PORTB.0 pin configuration using MPLAB Code Configurator. In this example we are using a newer PIC model, the PIC18F26K20.


Below is the Main.c file:

You can download the full project files (MPLAB XC8 source code and Proteus Schematic design) below here.  All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).  

Download: Blink LED

Download: Flash LED Proteus  

Download Mplab Project with Code Configurator: Blink_LED

Download Proteus Schematic with PIC18F26K20: Blinking_LED Proteus