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.
- Open MCC by clicking the icon in the toolbar.
- 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.
- 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.
- 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. - Click the Generate button and
then merge the required changes into the
config_bits.c
file 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 Interrupt Vector Enable. Set the IVTBASE address to the application offset plus 0x8.
- 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.
- 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
-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. - 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.