Interrupt On Change (IOC) is a hardware feature in most PIC® devices. It's 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 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.
Step by Step Instructions
Using the MPLAB Code Configurator (MCC), this project uses a momentary switch input to create the interrupt on change 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.
The project uses:
- PIC16F1508
- PICDEM™ Lab II Development Board
- Power from a 9 V Power Adapter
- PICkit™ 3 Programmer/Debugger
- MPLAB® X IDE
- MPLAB Code Configurator (MCC) plug-in
- MPLAB XC8 Compiler
- Momentary Switch
- 5 mm LED
- 330 ohm resistor 1/8 watt
PICDEM Lab II Connections:
- Connect 5 V and Gnd to the power bus of the protoboard.
- Connect 5 V to the Vdd pin of the PIC16F1508
- Connect Gnd to the Vss pin of the PIC16F1508
- Connect a wire from the RA2 pin (pin 17) to the breadboard Row 23
- Connect a wire from the ground bus to breadboard Row 27
- Connect the LED Anode to breadboard Row 23
- Connect the LED Cathode to breadboard Row 24
- Connect a 330 ohm resistor between Row 24 and Row 27
- Connect a wire from the RA4 pin (pin 3) to the breadboard Row 7
- Connect the momentary switch to row 7 and 9
- Connect a wire between Row 9 and the breadboard ground bus
To follow along with these steps, MPLAB® X should be open and the Programmer/Debugger connected to both the computer and the Development Board. The setup was described in the Setup and Installation section of this article. You should see a screen similar to the one below before you move on to step 1.
1
Create a new "Stand-Alone" project in MPLAB X for a PIC16F1508. Detailed instructions are included below if this is your first project.
2
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 the MCC. The result should look like the picture below:
4
Open the Pin Manager and then click on the PORTA port 2, input row, blue lock symbol to change it to locked and green in color. Also click on the PORTA port 4 (RA4) blue lock symbol to make it locked and green. This adds the RA2 and RA4 I/O pins to the project. It should look like the picture below when completed:
5
Close the Pin Manager and then the center section should show both RA2 and RA4 listed on the I/O chart.
- The RA4 pin will be the input that will read the switch using the Interrupt On Change Negative Edge (IOCN) so that box needs to be checked. Also the Weak Pull-Up (WPU) box needs to be checked to enable the pull-up resistor on the pin.
- The RA2 pin will be an output and control the LED. Click on the output box to make the pin an output.
6
The System needs to be setup next. Click on the System name in the Project Resources list.
The System section will appear. In this section the oscillator and configuration bit settings are selected.
Oscillator
- Select INTOSC from the drop down menu for the System Clock Select.
- Select the 1MHz_HF selection from the Internal Clock drop down menu.
This will enable the internal 1 MHz internal oscillator as the system clock.
Configuration
Each configuration setting can be changed under the Register tab of the System window. Match the selections shown here.
7
Click on the Generate Code button to have the MCC create the software libraries for this project.
9
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 interrupt service routine file. Each are 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. The MCC already 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 Interrupt Service Routine will handle the changing of the I/O pin.
while (1)
{
// Add your application code
}
interrupt_manager.c
The interrupt_manager.c file contains the Interrupt Service Routine that will run when the interrupt occurs. This is where a function to toggle the LED pin is entered. The function used is the IO_RA2_Toggle() macro that is created by the MCC and placed in the pin_manager.h file. This IO_RA2_Toggle() function is added to the Interrupt Service Routine as shown below.
11
Make sure your project has the programming tool selected (Part of Step 1 above) and connect power to your development board.
The PICkit 3 has limited power capability so we recommend you power the board separately.
The ICD 3 can power a development board, but we recommended you power the board separately.
The RealICE cannot power the development board so powering the board separately is required.
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.
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.
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 make 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
12
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 be asked to be saved before the project is closed.
The project can be closed under the File Menu > Close Project
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
|
| | | |