8-Bit Using MPLAB® XC8 Built-In Delay

The Timer0 module is an 8-bit timer/counter within most PIC® devices. The Timer0 can also trigger an interrupt when it overflows. This project creates a blinking Light Emitting Diode (LED) based on a Timer0 Interrupt Delay.

proj6.png

Step by Step Instructions

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

proj6bd.jpg

The project uses:

  • PIC16F18875
  • HPC Curiosity Board
  • MPLAB X IDE
  • MCC plug-in
  • MPLAB XC8 Compiler

To follow along with these steps, MPLAB X should be open and the HPC Curiosity Board connected to the computer through a USB cable.

1

Create a new standalone project in MPLAB X for a PIC16F18875.

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.

2

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

mcclaunch.png

3

Select the peripherals for your project. For this project, the following peripherals need to be selected:

  • System Module
  • Interrupt Module
  • Pin Module

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

project7menu.png

4

Open the Pin Manager and then click on the PORTA pin 4 (RA4) output blue lock symbol. The lock symbol will turn green. Add RA4 I/O pin to the project. The RA4 pin is connected to the D2 LED. It should look like the image below when completed:

pinmanager.png

5

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

project7menupin.png

The center screen should show RA4 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). Change the Custom Name column to say "D2_LED".

pinmodule.png

6

The System needs to be set up next. Click on the System Module name under Project Resources.

project7menusystem.png

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

Oscillator

  1. Select the HFINTOSC from the Oscillator Select drop-down menu.
  2. Select the 4_MHz selection from the HF Internal Clock drop-down menu.
  3. Select Clock Divider value of 4.

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

project7osc.png

Also, make sure the Low-voltage Programming Enable mode is selected at the bottom of the System Module screen.

lvp.png

7

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

generate.png

8

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.

project7files.png

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

9

The main.c file is where the application code is added. A list of I/O controlling macros is generated by 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
        D2_LED_Toggle();
        __delay_ms(200);
    }

10

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

Main_Build_Project.png
BUILD SUCCESSFUL

11

Make sure your project has the Curiosity Board selected and the USB cable is connected to the board.

Click on Make and Program Device Main Project. 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/Verify complete message. Additionally, the D2 LED connected to RA4 will be blinking on the HPC Curiosity Board.

Main_Program_Target_Project.png
Connecting to Starter Kit on Board...

Currently loaded firmware on Starter Kit on Board
Firmware Suite Version.....01.54.00
Firmware type..............Enhanced Midrange

Target voltage detected
Target device PIC16F18875 found.
Device Revision ID = 2002

Device Erased...

Programming...

The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x7ff
configuration memory
**Programming/Verify complete**



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 an LED.
proj6finished.png

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 may 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

12

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 the File > Close Project.

closeproject.png

Download

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

File Download
Installation
Instructions
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.