5.4.2 Supporting Interrupts for PIC® Devices with Hardware Interrupt Vectors
This section demonstrates how to enable interrupts in the application code using High/Low Interrupt Vectors for PIC18s and the standard interrupt vector for PIC16s.
Description: The following section will walk through the setup of an application
that is blinking the LED using a timer interrupt instead of using the Delay driver. This
section will use the High/Low Priority Interrupt Vectors on the PIC18F47Q10 Curiosity Nano.
Note: The projects used in
this example can be created by following the Creating a Bootloader Client
for PIC® Devices and the Creating an Application for
PIC® Devices sections of this document but on the PIC18F47Q10 Curiosity
Nano.
For PIC18 devices that have hardware interrupt vectors, the bootloader must be configured to use the same form of interrupts. Follow the steps below to enable interrupt support in the application and bootloader.
- Set the bootloader project as the main project. From the Projects tab, right click on the project folder and select Set as Main Project.
- Open MCC by clicking the icon in the toolbar.
- Open the Interrupt Manager tab and click the slider for Enable High/Low Interrupt Priority.
- Open the Project Properties
tab and navigate to the XC8 Linker > Additional options. Add
-Wl,-Pintcode=0x8,-Pintcode=0x18
to the text box in the middle of the window.Note: PIC16 devices will not need to remap any vector as it will be handled by the compiler.Important: Add the linker setting into all configurations. - Click the Generate button and
then merge the required changes into the
interrupt.c
andinterrupt.h
files while leaving the added changed from before.
- Set the application project as the main project. From the Projects tab, right click on the project folder and select Set as Main Project.
- Open MCC by clicking the icon in the toolbar.
- Navigate to the Project Resources tab, and click the red x-box next to the Delay module.
- Navigate to the Device
Resources tab and add the TMR0 driver.
- Setup the TMR0 driver to use a time-out of 150 ms or more and then enable the Overflow Interrupt.
- Navigate to the Interrupt
Manager tab and flip the switch for High/Low Interrupt Priority.Note: PIC16 devices do not have a priority on the interrupt vector. Simply calling the enable function in the
main.c
is enough to enable the interrupts on the device. - Generate the new timer code and observe the new files being generated in the Output window.
- Open the Project Properties
tab and navigate to the XC8 Linker > Additional options. Add
-Wl,-Pintcode=0x<Application Start Address> + 0x8,-Pintcode=0x<Application Start Address> + 0x18
to the text box in the middle of the window.Note: PIC16 devices will not need to remap any vector as it will be handled by the compiler.Important: Add the linker setting into all configurations that you need to offset the interrupt vector psects. - Lastly, update the
main.c
to blink the LED using the overflow interrupt callback. Remove the calls to the Delay driver and update themain.c
similar to the example below. - In some cases you will want to use
the low priority interrupt vector inside of the bootloader code, doing so will
result in a compilation failure due to the fact that we provide a placeholder
function for the low priority vector by default. To correct this error, simply
uncomment the dummy function that gets generated in the bl_interrupt.c file.
To correct this error, simply comment out the below placeholder function located inside of the bl_interrupt.c file.
/** Dummy Function */ void __interrupt(low_priority) BL_LowPriorityInterrupt_Placeholder(void) { NOP(); }