interrupt in pic c c language
Hi,
I’ll try write a more general explanation below (I won’t relate to PIC or any specific µC/µP), in the hope that more can read and understand the concepts.
You never "call interrupts" yourself, in a program. An interrupt not function, but an event (usually external to your program, but it can also be a software generated) that interrupts the normal flow of execution, hence the name.
When detecting such an event, the "hardware" is built such that it will stop executing your program and select to jump and execute from a predetermined address (specific to each µC/µP and source).
Your job is to write small routines to serve these interruptions, and locate them at the right addresses.
Take Reset for instance that is an event defined on all the µC/µP I know of. When Reset happens (due to RESET signal kept low and then released, or elapsed watchdog timer, or low voltage detector, or whatever reason), the CPU will start executing from a specific address. When you write your program (in C) and specify main(), the compiler will locate it at the right address for your specific CPU so that it will be executed after every Reset event.
It is more or less the same with any other interrupt source (timer, ADC, UART etc.), but since there are multiple sources, you need to let the compiler know where to place your little Interrupt Service Routines (ISR) and you do that with specific pragmas and/or compiler-specific keywords.
It might be that all the interrupting events will use the same address to execute from. In such a case you’ll need to read specific flags and determine the source and do things accordingly, but the principle is the same.
Interrupt priorities are a totally different matter and it’s just a way the hardware chooses to handle cases when more than one interrupting event is active.
For instance, it might be that a timer has finished counting exactly when the ADC has finished converting, but since (we say that) the timer interrupt event has higher priority, the hardware will choose to jump and execute its ISR first and then the ADC’s.
Now I simplified my explanation a bit, because things are a tad more complicated than this, but for serving an interrupt in C that’s about all there is to know (plus, of course, read the funky manual!).
This is the kind of topic many have a problem with, but if you understand the simple concepts above (plus some other stingy issues that you might never encounter if you keep things simple), there’s really no reason to avoid using interrupts in your designs (on the contrary, there are great advantages).
Maybe someone else can help with the details of PIC, which are foreign to me.
Arthur