5.4.1 Supporting Interrupts for PIC® Devices with the Interrupt Vector Table
This section demonstrates how to enable interrupts in the bootloader and application code using the Interrupt Vector Table for PIC18 devices that support it.
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 Interrupt Vector Table on the PIC18F57Q43 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.
For PIC18 devices that have the Interrupt Vector Table, the bootloader must be configured to use the same interrupt format. Follow the steps below to enable interrupt support in the application or 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.
Figure 5-65 5-71. Set the Bootloader as the Main Project - Open MCC by clicking the icon
in the toolbar.
Figure 5-66 5-72. Open MCC - Open the Interrupt Manager tab
and click the slider for Enable Vectored Interrupt and keep the IVT Base
Address and IVT Name set to default.
Figure 5-67. Set the Bootloader IVT Base Address - Open the Configuration Bits tab and navigate to the IVTLOCK bit One-Way
Set Enable bit option. Select IVTLOCK bit can be cleared and set
repeatedly from the drop-down list.
Figure 5-68. Set the Bootloader IVTLOCK Bit - Open the Project Properties
tab and navigate to the XC8 Linker > Additional options. Add
-mivt=0x8
to the text box in the middle of the window.Important: Add the linker setting into all configurations.Figure 5-69. Set the Bootloader IVT Linker Setting - Click the Generate button and
then merge the required changes into the
config_bits.c
file while leaving the added changed from before.Figure 5-70. Regenerate Bootloader Code and Merge Configuration Changes
- Set the application project as the
main project. From the Projects tab, right click on the project folder and
select Set as Main Project.
Figure 5-65 5-71. Set the Application to the Main Project - Open MCC by clicking the icon
in the toolbar.
Figure 5-66 5-72. Open MCC - Navigate to the Project
Resources tab, and click the red x-box next to the Delay module.
Figure 5-73. Remove the Delay Driver - Navigate to the Device
Resources tab and add the TMR0 driver.
Figure 5-74. Add TMR0 - Setup the TMR0 driver to use
a time-out of 150 ms or more and then enable the Overflow
Interrupt.
Figure 5-75. Configure the Timer Module
- 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 Interrupt Vector Enable. Set the
IVTBASE address to the application offset plus 0x8.
Figure 5-76. Enable Vectored Interrupt and Set the New Base Address - Open the Configuration Bits tab and navigate to the IVTLOCK bit One-Way
Set Enable bit option. Select IVTLOCK bit can be cleared and set
repeatedly from the drop-down list.
Figure 5-77. Set the Application IVTLOCK Bit - Generate the new timer code and
observe the new files being generated in the Output window.
Figure 5-78. Regenerate the MCC Code - Open the Project Properties
tab and navigate to the XC8 Linker > Additional options. Add
-mivt=0x<Application Start Address> + 8
to the text box in the middle of the window.Important: Add the linker setting into all configurations required to offset the IVT base address.Figure 5-79. Set the Application IVT Linker Setting - 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.Figure 5-80. Update the Blinking LED Code to Use Interrupts