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 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 on the toolbar.
Click image to enlarge.
Click Select MCC Classic for this project. This will provide a graphical interface for the registers needed.
Click image to enlarge.
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.
Click image to enlarge.
In the upper-left corner of MPLAB X IDE, MCC has the Project Resources window with three modules, System, Pin, and Interrupt, loaded.
Click image to enlarge.
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:
Click image to enlarge.
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).
Click image to enlarge.
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.
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.
Click image to enlarge.
Click on the Generate button in the Project Resources window to have MCC create the software libraries for this project.
Click image to enlarge.
The project will now include both the generated header and source files under the Projects tab.
Click image to enlarge.
Double-click on main.c to open it up in the editor window.
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);
Click image to enlarge.
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.
Click image to enlarge.
Click on the Debug and Run icon to compile and run the code; you should see a BUILD SUCCESSFUL message in the output window under the Digital-out (Build,Load,...) tab.
Click image to enlarge.
Open Window > Target Memory Views > SFRs.
Click image to enlarge.
In the Output window select the SFRs tab, then scroll until you can see PORTA, if it is not already visible.
Click image to enlarge.
Each time you click on Continue 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.
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.