how to handle multiple interrupt in PIC using Mikro C software

Thread Starter

bobparihar

Joined Jul 31, 2014
93
here i am programming PIC16F877a Micro controller for interrupt
i did a program of handling external Interrupt and it is working correct, for the service routine of External interrupt i was instructed to add a ISR function(can be of any name) with interrupt keyword,
for e.g. i use
void interrupt external(void) // interrupt keyword followed by any name for function
{
//ISR code.............
}

now i want to do a program in which there are multiple interrupt lets say i want to program external interrupt and timer interrupt in the same program..
for that i will use
void interrupt timer(void){
//ISR code.............
}

so my question is how compiler will distinguish between timer ISR function and external ISR function ??
 

Papabravo

Joined Feb 24, 2006
21,159
I think you only get one ISR for a midrange PIC device. That single ISR has to process all interrupt sources. If you want vectored interrupts to separate routines, the 8051 can do that.
 
Last edited:

Thread Starter

bobparihar

Joined Jul 31, 2014
93
I think you only get one ISR for a midrange PIC devices. That single ISR has to process all interrupt sources. If you want vectored interrupts to separate routines, the 8051 can do that.
u mean to say that i should include ISR of timer and external interrupt in the same ISR function?
but how i will set the ISR routine order of diffrent interrupt in the same function?
 

ErnieM

Joined Apr 24, 2011
8,377
For every interrupt source there is a corrosponding interrupt flag. Inside the single interrupt routine you need to test each flag to determine what triggered the interrupt, handle the event, and reset that flag.

Should another interrupt occur when handling something else either your code will fall thru and find it, or exit with it unhandled. Should another flag still be pending when you exit the interrupt routine just fires off another time and then your code will catch it.
 

Thread Starter

bobparihar

Joined Jul 31, 2014
93
For every interrupt source there is a corrosponding interrupt flag. Inside the single interrupt routine you need to test each flag to determine what triggered the interrupt, handle the event, and reset that flag.

Should another interrupt occur when handling something else either your code will fall thru and find it, or exit with it unhandled. Should another flag still be pending when you exit the interrupt routine just fires off another time and then your code will catch it.
so what if any interrupt remain un held bcz of any other interrupt is coming when executing isr then?? how to rid of this problem??
 

ErnieM

Joined Apr 24, 2011
8,377
What problem?
so what if any interrupt remain un held bcz of any other interrupt is coming when executing isr then?? how to rid of this problem??
Should another interrupt occur when handling something else either your code will fall thru and find it, or exit with it unhandled. Should another flag still be pending when you exit the interrupt routine just fires off another time and then your code will catch it.
 

Thread Starter

bobparihar

Joined Jul 31, 2014
93
What problem?
suppose external interrupt invoked and MCU is servicing the corresponding ISR meanwhile timer interrupt invoked... and MCU is still servicing the previous ISR i.e. still in the IF condition block of External interrupt ISR block.. so that will lead to the problem that the Timer interrupt will ignored..
so how this problem can be solved if i am using the same code structure as suggested
 

Papabravo

Joined Feb 24, 2006
21,159
Interrupts remain pending until serviced. They can never be ignored, only delayed for a short period. If an interrupt arrives while you are in the ISR, the servicing of that interrupt will be delayed until the ISR finishes it's task and executes the RETFIE instruction. As soon as the interrupt system is enabled again the pending interrupt will cause a NEW invocation of the ISR and eventually all interrupts will be serviced. This behavior rests on the assumption that no interrupt servicing will leave the interrupt system disabled for longer than the periodic arrival time of the fastest interrupt.

The design goal is to code your ISR in such a way that it spends the minimum amount time doing the work required for the job. This will minimize the interrupt latency, which is the average amount of time it takes from the setting of the interrupt flag until the service routine is entered. If you are executing instructions at the non-interrupt level, the latency is small. If you are already in the SINGLE ISR, then you must complete the ISR, and invoke it again. For example, if your timer sets the interrupt flag every millisecond and you have 1.5 milliseconds worth of code in the ISR then things won't work very well. If on the other hand the ISR takes 20 microseconds to execute then you may occasionally encounter a variable latency of from zero to 20 microseconds in processing the other interrupt.

Trust us this is only a problem in your mind. It is not a real problem UNLESS the ISR takes about as long to execute as the fastest periodic arrival rate of any interrupt. The solution is always to recode the ISR to do much less work than you originally planed.
 

ErnieM

Joined Apr 24, 2011
8,377
suppose external interrupt invoked and MCU is servicing the corresponding ISR meanwhile timer interrupt invoked... and MCU is still servicing the previous ISR i.e. still in the IF condition block of External interrupt ISR block.. so that will lead to the problem that the Timer interrupt will ignored..
so how this problem can be solved if i am using the same code structure as suggested
You keep asking the same question so I am going to keep repeating the same answer until you read it.
Should another interrupt occur when handling something else either your code will fall thru and find it, or exit with it unhandled. Should another flag still be pending when you exit the interrupt routine just fires off another time and then your code will catch it.
 
Top