8-Bit Interrupt On Change

Interrupt On Change (IOC) is a hardware feature in most PIC® devices. It is available on select I/O pins. When the hardware senses a change in state, low to high or high to low, an interrupt will occur. The Interrupt Service Routine (ISR) will be run, implementing any custom code the designer has developed. This project controls an LED, based on a momentary switch connected to an IOC pin.

proj9.png

Step by Step Instructions

Using MPLAB® Code Configurator (MCC), this project uses a momentary switch input to create the IOC and control an LED from a digital output. When the switch is pressed, the interrupt will toggle the state of the LED from off to on or on to off.

proj6bd.jpg

The project uses:

  • PIC16F18875
  • HPC Curiosity Board
  • MPLAB X IDE
  • MPLAB Code Configurator (MCC) plug-in
  • MPLAB XC8 Compiler

To follow along with these steps, MPLAB X should be open and the HPC Curiosity Board connected to the computer through a USB cable.

1

Create a new standalone project in MPLAB X for a PIC16F18875.

If this is your first time creating an MPLAB X project, please visit the "Create a Standalone Project" page to follow a step-by-step instruction on how to do this.

2

Open MCC under the Tools > Embedded menu of MPLAB X IDE.

mcclaunch.png

3

Select the peripherals for your project. In this project, you need to select these peripherals:

  • System Module
  • Interrupt Module
  • Pin Module

The System Module, Interrupt Module, and the Pin Module will all be automatically included when you launch MCC. The result should look like the picture below:

proj9per2.png

4

Open the Pin Manager and then click on the PORTA port 4 (RA4) output row. Select the blue lock symbol to change it to show a green locked symbol. Follow the same process on the PORTB port 4 (RB4) input row, so it is locked and green. This adds the RA4 and RB4 I/O pins to the project. It should look like the picture below when completed:

project9io.png

5

Close the Pin Manager and then click on the Pin Module selection in the Project Resources section.

pin.png

The center section will show the Pin Module settings. It will show both RA4 and RB4 listed on the I/O chart.

  1. The RA4 pin will be an output and control the LED. Click on the output box to make the pin an output.
  2. Change the pin name to D2_LED.
  3. The RB4 pin will be the input and will read the switch using the Interrupt On Change Negative Edge (IOCN), so it needs to be selected.
  4. Change the pin name to SW1 to match the switch used on the HPC board.
project9_pin.png

6

The Interrupt Module is next. Click on the Interrupt Module in the Project Resources tab.

project9interupt.png

The interrupt will be automatically setup because of the IOCN selected in the Pin Module. The IOC interrupt is the only interrupt, so it is the first one to activate.

project9interruptsystem.png

7

The System Module needs to be setup next. Click on the System Module name under Project Resources.

project7menusystem.png

In this section, the oscillator settings and the configuration settings are selected.

Oscillator

  1. Select HFINTOSC from the Oscillator Select drop-down menu.
  2. Select 4_MHz from the HF Internal Clock drop-down menu.
  3. Select a Clock Divider value of 4.

This will enable the internal 1 MHz internal oscillator as the system clock.

project7osc.png

Also, make sure the Low-voltage Programming Enable mode is selected at the bottom of the System Module screen.

lvp.png

8

Click on the Generate Code button to have MCC create the software libraries for this project.

generate.png

9

The project will now have both generated header files and source files. It should also have generated a main.c file.

Note: MCC may offer to generate a main.c file. Click yes to allow it to complete that task.

project9files2.png

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

mainfile.png

10

There are two files that need to be modified for this project: main.c and interupt_manager.c. These are the main code file and the ISR file. Each is modified as described below.

main.c

The main.c file requires a few lines to be uncommented for the interrupt to work properly. To enable the interrupt to work, Global Interrupts and Peripheral Interrupts need to be uncommented. MCC has the control commands in the default main.c file, they are just commented out with two forward slashes (//). The forward slashes need to be removed to enable these lines of code.

    // Enable the Global Interrupts
    INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    INTERRUPT_PeripheralInterruptEnable();

The main loop doesn't require any additional code beyond the default while(1) loop. The ISR will handle the changing of the I/O pin.

while (1)
    {
        // Add your application code

    }

pin_manager.c

The pin_manager.c file contains the IOCBF4_DefaultInterruptHandler that will run when the interrupt occurs. This is where a macro to toggle the LED pin is entered. The macro used is the D2_LED_Toggle(), created by MCC and placed in the pin_manager.h file. This D2_LED_Toggle() macro is added to the IOCBF4_DefaultInterruptHandler as shown below.

project9isr.png

11

Click on the Build Project icon (the hammer) to compile the code and you should see a "BUILD SUCCESSFUL" message in the output window of MPLAB X.

Main_Build_Project.png
BUILD SUCCESSFUL (total time: 4s)

12

Make sure your project has the programming tool selected (part of Step 1 above) and connect power to your development board.

Click on the Make and Program Device icon. This will build the project again and launch the programmer. In the output window, you should see a series of messages and, if successful, it will end with a "Programming and Verify Successful" message.

Main_Program_Target_Project.png

Output Window:

Connecting to MPLAB PICkit 3...
Firmware Suite Version.....01.34.11
Firmware type..............Enhanced Midrange

Target detected
Device ID Revision = 6

The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x7ff
configuration memory
Device Erased...

Programming...
Programming/Verify complete

The code has been programmed into the microcontroller. When the momentary switch is pressed, the LED will change state from off to on or on to off. In the picture below, it changed from off to on.

project9final.png

If it's the first time the programmer is connected to the board, the programming tool may need to download the proper operating firmware for the exact device. You may see a series of processes if this occurs. This should only happen once.

Downloading Firmware…
Downloading bootloader
Bootloader download complete
Programming download…
Downloading RS…
RS download complete
Programming download…
Downloading AP…
AP download complete
Programming download…
Firmware Suite Version…..01.34.11
Firmware type…………..Enhanced Midrange

13

The project can be closed in MPLAB X. 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. The project can be closed under File > Close Project.

closeproject.png

Download

If you have any problems with your project, the completed MPLAB X project file can be downloaded from the link below:

File Download
Installation
Instructions
Windows Linux Mac OSX
Project 9 Files
© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.