Electronic – PIC IOC vs INT and the interrupt function

interruptspic

I am using PIC18F26K83 and I want to use interrupts for a switch that will be connected to one of the pins of my PIC. So I have some related questions:

  1. There are 2 interrupt options in PIC18F26K83: IOC vs INT. What is the difference? I heard that is quite easy to miss edges with IOC though. Can I use both of them at the same time?
  2. Other question is related to interrupt function. If I have 2 interrupt functions : interrupt i1(void) and interrupt i2(void), in case of any interrupt, does the software enters all the interrupts one by one? If not how does it get which function is related to coming interrupt? For example I am using CANBus interrupt, I2C interrupt, timer interrupt and one external interrupt coming from a switch. If I press the switch, how will software know which interrupt function is related to switch? Thanks beforehand.

Best Answer

There is an interrupt flag associated with the interrupt you choose to use. That will tell you which pin was responsible when you service the interrupt (it is set automatically by the pin change). You will reset it in your ISR. That ensures you won't miss an pin change interrupt if your ISR is properly written.

But if the switch is bouncing you'll get additional interrupts when the interrupts have been re-enabled. Maybe sometimes, maybe not other times depending on what the processor is doing. Maybe you'll reset the flag and it will immediately be set again by the hardware before you even exit the ISR, then the interrupt will occur immediately when you exit. You could put a (blocking) delay routine in your ISR but I think most real-time programming folks would be nauseous or possibly physically ill by that point.

Generally manual switches should not be serviced by interrupt routines. Institute a periodic interrupt (typically no faster than 1kHz, and maybe a bit slower depending on the switch- you can also check every n'th time through with a 1kHz interrupt) and you can check and debounce the switch input by polling. You would have the current state of the switch, the previous state and the previous stable state. If you have two readings the same (in a row) that differ from the previous stable state you know you have an edge, and you can discard the polarity of edge you're not interested in (say the release of the switch).