Using MPLAB® XC8 Built-In Delay

Last modified by Microchip on 2024/02/16 08:15

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 toggle an output with a programmable delay.

Resources

Step-by-Step Project

Create a new standalone project in MPLAB X IDE for a PIC16F1509. Select the simulator as your debug tool.

Open MCC under the Tools > Embedded menu of MPLAB X IDE or click the icon mcc icon on the toolbar.
Select MCC from the menu or the toolbar
Click image to enlarge.

Click Select MCC Classic for this project.  This will provide a graphical interface for the registers needed.
select Classic Mode
Click image to enlarge.

Back to Top


MCC Content Manager Wizard now gives a list of Required Content and Optional Content list.  In this case, no additional content is required for this project. Click Finish.
content manager wizard
Click image to enlarge.

Back to Top


In the upper-left corner of MPLAB X IDE, MCC has the Project Resources window with three modules, System, Pin, and Interrupt, loaded.
project resources window
Click image to enlarge.

Back to Top


Open the Pin Manager: Grid View tab in the output window and then click on the PORTA pin 2 (RA2) Output blue lock symbol. It will turn green.  It should look like the accompanying image when completed:
Pin manager grid view setting RA2 as output
Click image to enlarge.

Back to Top


Click on the Pin Module selection in the Project Resources section. 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).
Pin module window with RA2 set as an output
Click image to enlarge.

Back to Top


The system needs to be set up next. Click on System Module in the Project Resources list.

Select the INTOSC from the drop-down menu for the System Clock Select.
system module and clock selections
Click image to enlarge.
Select the 1MHz_HF selection from the Internal Clock drop-down menu.
Each configuration setting can be changed under the Registers tab of the System window. Match the selections shown in the accompanying image.
shows the register view
Click image to enlarge.

Back to Top


Click on the Generate button in the Project Resources window to have MCC create the software libraries for this project.
The generate button is shown
Click image to enlarge.

Back to Top


The project will now include both the generated header and source files under the Projects tab.
files generated by mcc
Click image to enlarge.

Back to Top


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

Back to Top


Add two lines of code to the end of the main.c file near the bottom of the file below the "Add your applications code" comment:

        // Add your application code
        IO_RA2_Toggle();
        __delay_ms(200);

Main.c file with code added
Click image to enlarge.

Back to Top


Set a breakpoint on the IO_RA2_Toggle() line by clicking on the line number (71) to the left of the code. A red box will replace the line number.
show the breakpoint set
Click image to enlarge.

Back to Top


Click on the Debug and Run icon Debug Project Button to compile and run the code; you should see a BUILD SUCCESSFUL message in the output window under the Digital-out (Build,Load,...) tab.
build successful in the output window
Click image to enlarge.

Back to Top


Open Window > Target Memory Views > SFRs.
open the SFR memory view
Click image to enlarge.

Back to Top


In the Output window select the SFRs tab, then scroll until you can see PORTA, if it is not already visible.
PORTA in the output window
Click image to enlarge.

Back to Top


Each time you click on Continue Continue Button the program will run until it hits the breakpoint on the IO_RA2_Toggle() command.  You will see the RA2 bit of PORTA change state.

RA2 highRA2 low

Back to Top


The project can be closed in MPLAB X IDE. 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.

Close the project under File Menu > Close Project.

Back to Top