how to handle multiple interrupt in PIC using Mikro C software

Discussion in 'Embedded Systems and Microcontrollers' started by bobparihar, Apr 6, 2015.

  1. bobparihar

    Thread Starter Member

    Jul 31, 2014
    93
    0
    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 ??
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    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: Apr 6, 2015
    bobparihar likes this.
  3. bobparihar

    Thread Starter Member

    Jul 31, 2014
    93
    0
    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?
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
  5. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    That is exactly what I mean to say. Every enabled interrupt source has to be handled within a single ISR.
     
    blueroomelectronics likes this.
  6. bobparihar

    Thread Starter Member

    Jul 31, 2014
    93
    0
    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??
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    What problem?
     
  8. bobparihar

    Thread Starter Member

    Jul 31, 2014
    93
    0
    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
     
  9. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    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.
     
    MrChips likes this.
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    You keep asking the same question so I am going to keep repeating the same answer until you read it.
     
    bobparihar likes this.
Loading...