How to get interrupt on change of logic on Pins (Input Change Notification). Code + Proteus Simulation
This post will help us to understand how to get interrupt on change of logic at selected pins. The importance of this is you get interrupt whenever state of the pin changes. Even when your microcontroller is in sleep mode when the clocks are disabled.
I am using MPLAB x IDE and C30 Compiler and Proteus Simulation on v8.0 (can be downloaded from downloads section at the bottom of this post). This code will work on PIC24, DsPIC33F and DsPIC30F (16bit microcontroller) by slightly changing code.
I supposed that you know following.
- How to blink an LED (if not then I suggest you to read this (link) first before continuing this post)
- How to get input from button. (if not then I suggest you to read this (link) first before continuing this post)
We have 11 external signals in 14pin PIC24. These pins are labeled as CNx where x is number of input change notifications all of these pins can be configured by setting their register values mentioned below in this post.
Each pin has its own pullup or pulldown resistors. They can be enabled by setting there corresponding registers. The main purpose of using these is to reduce number of external components. But remember that they are weak resisters just to interface buttons or keypad. Warning !!! Do not use external pullups/pulldowns resistors when internal pullups/pulldowns are enabled.
In this figure, a button is attached to PortA pin 6. Every time when button is pressed yellow LED will glow.
In the following figure I used push button on pin RA6 as input and LED on pin RB0 as output and as shown in circuit I am not using any external pullup or pulldown resistor that’s why I enabled PIC internal Pulldown resistor by writing 1 to “CNPD1bits.CN9PDE = 1”.
Enabled Changed Notification interrupt on PIN CN8 “CNEN1bits.CN8IE = 1”. Enabling interrupt for Change Notification form interrupt controller “IEC1bits.CNIE = 1”.
Now every time when logic is changed on CN8 (RA6) an ISR (interrupt service routine) will be executed. LED will glow when logic is high on CN8 (RA6).
You can download Code and Proteus Simulation Click here