PIC24FJ1024GB610 Microcontroller Interrupt on Change (IOC) Tutorial

by Navy_Guy in Circuits > Microcontrollers

1161 Views, 2 Favorites, 0 Comments

PIC24FJ1024GB610 Microcontroller Interrupt on Change (IOC) Tutorial

20220822_105521.jpg

This project is going to explain the Interrupt On Change (IOC) feature on the PIC24 Microcontroller. An IO pin from the PIC24 is connected to SW4 on the Explorer 16/32 Development Kit, and it will write how many times the switch was pressed on the LCD. I will walk you through the steps used to create this project.

I included an mp4 video to show the program in action.

Supplies

Development Hardware:

  • Explorer 16/32 Development Kit from Microchip. Part # DM240001-3
  • PIC24FJ1024GB610 Microcontroller from Microchip.

Software:

  • MPLAB X IDE Version 6.00
  • MCC - MPLAB Code Configurator Version 5.1.17 with Core Version 5.4.11
  • Foundation Services Version 0.2.3
  • PIC24/dsPIC33/PIC32MM MCUs Version 1.171.1
  • Packs: PIC24F-GA-GB-DFP Version 1.7.214
  • XC16 Version 2.00 Compiler Tool chain

Opening the Project in MPLAB X

Choose Project.jpg
  1. From the toolbar select: File->New Project and it will open up the Window shown above.
  2. Select "Standalone Project", then click the Next button.

Project Setting Dialog Boxes

Select Device.jpg
Select Compiler.jpg
Sel_Proj_Name_Folder.jpg
Project_Properties.jpg
  1. In the "Select Device" dialog box, pick the Device, and Tool in the drop down boxes as shown in the images above, then click Next.
  2. In the "Select Compiler" dialog box, pick XC16 v2.00, then click Next.
  3. In the "Select Project Name and Folder" select the Project Name and Browse to the folder where you wish to store the project. Then click the Finish button.
  4. Now in the Projects Tab, Right click the project name. A popup menu will open and you will pick "Project Properties" located at the bottom of the popup menu. Set the "Tool", "Packs" and the "Compiler Toolchain" as per the image above.

Start Up MPLAB Code Configurator

Click on MCC.jpg
MCC Content Manager Wizard.jpg
  • Click on the Blue "MCC" icon on the toolbar to open up MPLAB Code Configurator (MCC).
  • In the "MCC Content Manager Wizard" click the "Select MCC Classic" button.

Next, Lets Set Up the Hardware

MCC Core Versions.jpg
System Module.jpg
  • On the left side of the IDE look for "MCC Core Versions" and Verify the Plugin and Core.
  • Click on the "System Module" tab in MCC
  • Set the "Clock" to Primary Oscillator and set it to 8 MHz.
  • Set the In Circuit Debugger (ICD) to "Communicate on PGEC2 and PGED2". This will allow you to debug the code.

Set Up the LCD in MCC

Project Resources Generate Code.jpg
CharLCD1 Tab.jpg
  • In the Device Resources window click the Plus sign next to CharLCD1 to add it to the project.
  • Now click on the "CharLCD[CharLCD1]" tab and verify the setting match the image above. "Enable printf" will allow us to use the printf fuction to print to the LCD on the Explorer 16/32 board.

Refer to LCD Schematic for the Pin Numbers

LCD_Schematic.jpg
  • The schematic above will be used to look up the pin numbers need to connect the PIC24 microcontroller to the LCD.

Connecting the LCD Pins in Software

CharLCD1 Pin Manager.jpg
  • In the "Pin Manager: Grid View" tab, set the pins as shown in the image above. Refer to the Schematic to Connect the pins from the microcontroller to the LCD.

Review Explorer 16/32 Schematic to Locate Pin

Button_Schematic.jpg
  • Pull up the schematic and find which Pin on the microcontroller is connected to SW4 on the Explorer 16/32 board.
  • Note that SW4 is connected to Pin 80 on the PIC24 Microcontroller.
  • Also, note that there is a pullup resistor on this switch.

Connecting SW4 to P80 on the Microcontroller

Pin Manger IOC Pin S4.jpg
  • In the bottom window of MPLAB X IDE, click on the "Pin Manager: Grid View" tab.
  • Locate Pin 80 and click the open lock to set this pin as an input. Once clicked the lock will close and turn green.
  • Also, take note that this pin is labeled Port D 13. AKA: RD13

Open Pin Module Tab

Pin Module Settings.jpg
  • Click on the "Pin Module" tab in the main Window of the IDE.
  • Locate Pin RD13 and change the "Custom Name" to SW_4.
  • Now, set the Interrupt on Change (IOC) to Negative. This will cause the interrupt to trigger on the falling edge of the signal.
  • NOTE: If there was not a pullup resistor hooked to the switch, you could enable the "Weak Pullup" WPU on this pin to set the value to a HIGH or Digital one.
  • Leave all of the other pins set at their default values.

Lets Generate the MCC Code

Project Resources.jpg
  • In the "Project Resources" window, click the "Generate" button as shown in the image above.
  • This will generate some initial code to get us started.
  • Now click on the Blue "MCC" icon on the toolbar to close MCC.

Open the Projects Tab

Projects Tab.jpg
  • Click on the "Projects" tab in the left window.
  • Expand the folders as shown in the image above.

Click on Main.c

Main.jpg
  • Click on main.c to open it up in the editor window.
  • Make the changes as shown in the image above.
  • We also need to enable the Interrupts for Pin 80 to Interrupt on a Change.

Click on Mcc.h

mcc.h.jpg
  • Click on mcc.h to open it up in the editor window.
  • Add the include for the LCD to the file as shown in the image above.

Click on Pin_manager.c

pin_manager.c.jpg
  • In the callback function "SW_4_CallBack(void)" make the changes as annotated in the image above.
  • Also, check the include files and make sure it matches:

#include <xc.h>

#include <stdio.h>

#include "mcc.h"

#include "pin_manager.h"

uint8_t btnPressedCtr = 0; // Make sure to add this variable below the includes.

  • I urge you to study the code in this file to fully understand the interrupt code and the callback function. This will give you a better insight on how this code works.

Compile the Program

clean and build.jpg
  • Click the Hammer with the Broom to "Clean and Build" the program. Make sure there are no errors.
  • Hook up the USB cord to J18 on the Explorer 16/32 Development Kit board.
  • Click the green down arrow that points to the chip to "Make and Program" the PIC24.
  • Check Jumper J19 on the Development board and make sure that it is closed, so that the LCD will work.

Test the Program

  • When you first load the program, the LCD will be blank until you push S4 for the first time.
  • Push S4, then you should see "SW Pressed 0" on the LCD.
  • Each time you push S4, the counter will increment and will update on the LCD.

Summary

Some idea's on what the Interrupt On Change (IOC) feature can be used for listed below:

  • High water switch connected to the IOC pin can turn on a sump pump.
  • The IOC Pin can be used on a pushbutton to enter modes on an alarm clock. IE. Set the time, set the alarm, etc...
  • It could be used on a thermostat to contol a HVAC system.

There are many other uses for the IOC Pin on the PIC24 microcontroller. My goal in this instructable was to help you understand the IOC Pin software, and how to use it.