Thursday , March 30 2017
Home / Embedded Systems / Interrupt On Change (IOC) in PIC Microcontrollers

Interrupt On Change (IOC) in PIC Microcontrollers

The interrupt on change is a cool feature on microcontrollers. Like you guessed, the interrupt occurs when there is a state change in any of the port pin associated with this feature. In PIC microcontrollers, PORT B is has the Interrupt on Change feature. The controller jumps into the interrupt vector when there is a change in the state of any of the pins in the port.

In my previous post on interfacing matrix keypad, we used the conventional polling technique to poll for the data. In this post we will see how the same task can be done with interrupt on change. This is probably the best example that I can think off for demonstrating the interrupt on change feature as the entire PORT B is used by the keypad.

The keypad is not always used in the system. It is mostly used while staring up the system or making some configuration settings. In either case it is used for a small fraction of the controller’s up time, so checking it in each run is purely a waste of time. Lets see how this can be done with interrupts.

Whats an Interrupt?

An interrupt is a way of letting the controller know that something important happened without having it to look at the possibility each time. This save a lot of time; time spent in checking for that event. Besides saving time, it is more accurate in reading that event without any sampling issues.

The idea is this, either you can keep looking at the power indicator on the wall and check if the power is back or switch on the fan and be notified once it is there 🙂

Modifying the code to work with IOC

There was not much to change in the code that we wrote for the matrix keypad interface here. If you open that code in a separate window and compare it with this code you will see that there is not much difference between them. The major working logic of that program has been moved into the ISR and some IOC related configuration is made in the main program.

Note: The code in the 4×4 matrix keypad interface was written for the C18 compiler and this one below is for the HI-Tech C Compiler for PIC Microcontrollers. If you are not familiar with this compiler, have a look at my previous post on migrating to the Hi-Tech C compiler its not really that difficult.

As far as the hardware is considered, there is absolutely no change and the output is also essentially the same. Only, now you have the while (1) loop all for yourself.

Wrapping up!

This Interrupt on change feature can actually be considered as 8 separate external interrupts that are mapped on to the same interrupt vector. The only drawback is that there is no way to prioritize the interrupts. But cheer up!! you can always use it in applications that can tolerate some time lag in servicing the ISR.

About Siddharth

Siddharth is a Firmware Engineer, techie, and a movie-buff. His interests include, Programming, Embedded Systems, Linux, Robotics, CV, Carpentry and a lot more. At times, you could see some of his sunday projects converge on release quality. You get to know him on the following social channels.

Check Also

c2000_lauchpad

Interfacing 16×2 LCD with C2000 launchpad

  The C2000 Launchpad is an inexpensive evaluation module based on Piccolo family microcontrollers from …

  • I am still confused regarding the interrupt on change function of PIC Controllers. Where you have defined which pin will have IOC ? how to detect on which pin the last level had changed ?

    • Kalpesh Patel, as I mentioned in the post the interrupt vector for all the 8 pins are the same. So when there is a change in any one of the pins of PORT B, the controller interrupts. There is no straight forward way to know which pin was pressed. You will have to scan for the pin in the ISR. In the code, there is a line RBIE = 1; this will enable the port B interrupt (again mentioned in the comment). I hope this clears your doubts.

  • Yes Siddharth,

    Thanks for your comment. 🙂 i need to detect 2 PWM signal individually on IOC is it possible any how using some software technique ?

    • Kalpesh, I think you should use the CCP module to achieve this if you want the results to be accurate. There are 2 CCP module in PIC 16/18. But if you have already used them for other application then you can use any digital pin to read the ON time in polling mode. Using interrupts (especially IOC) for this is not advisable as PWM is a continuous signal and your controller will keep on interrupting.

  • Thanks for information…yes i think that would be last option for me. i was using PIC16F628A which is having 1 PWM module so my CCP Module is used. So i will use now PIC16F877A which had 2 CCP module one i will use for PWM generation and 2nd i will use to detect the Feedback from TSOP sensor.

  • Germanr52

    Hi! Great tutorial, just a question: what about debounce?

    • Hi Germanr, It is not a good practice write this kind of code (the ISR is doing all the processing). But on this case, it eliminates (or, so it appeared to me then) the need for a denounce logic as it is compensated by the time taken for the ISR to run.

  • danny

    can i run this as it is code in micro c or their might be changes i have to do to run this in micro c??if requires then please tell me asas.ty

Keep in touch with the current trends!
Did you like this article? Sign up and get our latest posts delivered to your inbox!
  We hate spam and never share your details.