Using MPLAB® XC8 Built-In Delay

The MPLAB® XC8 Compiler has a built-in delay function that can be handy. This project demonstrates how to use it with MCC macros to control an output and blink a Light Emitting Diode (LED).


Step by Step Instructions

Using the MPLAB Code Configurator (MCC), this project generates the code to blink an LED using a hardware Timer 0 Interrupt to generate the delay between the on and off state.


The project uses:

PICDEM Lab II Connections:

  • Connect 5 V and GND to the power bus of the protoboard.
  • Connect 5 V to the Vdd pin of the PIC16F1508
  • Connect Gnd to the Vss pin of the PIC16F1508
  • Connect a wire from the RA2 pin (pin 17) to the breadboard Row 23
  • Connect a wire from the ground bus to breadboard Row 27
  • Connect the LED Anode to breadboard Row 23
  • Connect the LED Cathode to breadboard Row 24
  • Connect a 330 Ω resistor between Row 24 and Row 27

To follow along with these steps, MPLAB X should be open and the programmer/debugger connected to both the computer and the Development Board. The setup is described in the "Setup and Installation" section of this training module. You should see a screen similar to the one below before proceeding to step 1.



Create a new 'Stand-Alone' project in MPLAB X for a PIC16F1508.

If this is your first time creating an MPLAB X project, please visit the "Create a Standalone Project" page to follow a step-by-step instruction on how to do this.


Open the MPLAB Code Configurator under the Tools > Embedded menu of MPLAB X IDE.

The sections of the MCC tool are shown in the image below. We will refer to these sections by name throughout this page. This highlight can be enabled over the MCC at any time by clicking on the information icon mccinfo.png.


Select the peripherals for your project.
In this first project these peripherals need to be selected:

  • System Module
  • Interrupt Module
  • Pin Module

These modules will automatically be included when you launch the MCC. The result should look like the picture below:



Open the 'Pin Manager' and then click on the PORTA pin 2 (RA2) output blue lock symbol. It should turn green. This adds this RA2 I/O pin to the project. Also, make sure the reset pin has the blue lock shown. If it is green, then click on the green lock to make it blue. This will make the reset internal. It should look like the picture here when completed:



Close the 'Pin Manager' and then click on the 'Pin Module' under 'Project Resources'.


The center screen should show RA2 listed on the I/O chart. Click on the output box to make the pin an output (if not checked) and make sure Analog and WPU are not checked (click on them to uncheck them).



The System needs to be setup next. Click on 'System Module' under 'Project Resources'.


In this section, the oscillator settings and the configuration settings are selected.


  1. Select 'INTOSC' from the System Clock Select drop-down menu.
  2. Select the '1 MHz_HF' selection from the Internal Clock drop-down menu.

This will enable the internal 1 MHz internal oscillator as the system clock.



Each configuration setting can be changed under the 'Register tab' of the System Module. Match the selections shown here:



Click on Generate Code to have the MCC create the software libraries for this project.



The project will now have both generated Header Files and Source Files. It should also have a generated main.c file.

Note: MCC may offer to generate a main.c file. Click on Yes to allow it to complete this task.


Double click on main.c to open it up in the Editor window.



The main.c file is where the application code is added. A list of I/O controlling macros are generated by the MCC and placed in the pin_manager.h file. The MPLAB XC8 compiler also has a built-in delay in the format: __delay_ms(delaytime). The delaytime is a value indicating how many milliseconds to delay.

Within the while (1) brackets in main.c, there is a comment that states Add your application code. Place the following code under that comment:

   while (1)
        // Add your application code


Click on 'Build Project' (hammer icon) to compile the code and you should see a "BUILD SUCCESSFUL" message in the Output window of MPLAB X.

BUILD SUCCESSFUL (total time: 325ms)


Make sure your project has the programming tool selected (part of Step 1 above) and connect power to your development board.

  • PICkit 3 has limited power capability so we recommend you power the board separately.
  • ICD 3 can power a development board, but for simplicity we recommend you power the board separately.
  • REAL ICE cannot power the development board so powering the board separately is required.

Click on the 'Make and Program Device' icon. This will build the project again and launch the programmer. In the Output window, you should see a series of messages and if successful it will end with a "Programming and Verify Successful" message.


Output Window:

Connecting to MPLAB PICkit 3...

Currently loaded firmware on PICkit 3
Firmware Suite Version.....01.41.06
Firmware type..............Enhanced Midrange

Target voltage detected
Target device PIC16F1508 found.
Device ID Revision = 2

The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x5f
configuration memory

Device Erased...

Programming/Verify complete

The RA2 LED will blink at a 200-millisecond rate on then off. The picture below shows the LED caught in the lit position but the application will blink continuously.

If it is the first time the programmer is connected to the board, the programming tool may need to download the proper operating firmware for the exact device. You make see a series of processes if this occurs. This should only happen once.

Downloading Firmware…
Downloading bootloader
Bootloader download complete
Programming download…
Downloading RS…
RS download complete
Programming download…
Downloading AP…
AP download complete
Programming download…
Firmware Suite Version…..01.34.11
Firmware type…………..Enhanced Midrange


The project can be closed in MPLAB X. The project is saved automatically when it is built but any changes to files or configuration may ask to be saved before the project is closed. The project can be closed under File > Close Project.



If you have any problems with your project, the completed MPLAB X project file can be downloaded from the link below:

File Download
Windows Linux Mac OSX
Project 7 Files
© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.