pic16f84 two interrupts at once

Discussion in 'Embedded Systems and Microcontrollers' started by krvavizmaj, Jan 25, 2008.

  1. krvavizmaj

    Thread Starter New Member

    Jan 25, 2008
    Hi all,

    I'm using pic16f84 for some simple projects and next thing I want to do is a device that should have a TMR0 interrupt and an external interrupt on RB0. The timer is supposed to count, and interrupt, each second and on RB0 there will be fast external interrupts, about 20+ times a second. The device will count the interrupts on RB0 for a duration of 60 seconds.

    The problem here is what happens when I get a timer interrupt and while proccessing that interrupt I get an external interrupt. Or the other way around. Normaly after the first interrupt, global interrupts will be disabled so the second interrupt won't be detected. The service routines for each interrupt will be about 20-30 instructions max.

    My question is can I just reenable global interrupts immideately after I get the first interrupt, and then if a second interrupt occurs it will proccess the second interrupt and then return to complete the first interrupt. Or should I just put a faster osscilator and hope that this happens once in a 100 or so years.

  2. Papabravo


    Feb 24, 2006
    No need for that. There are two approaches to your problem.
    1. After you finish the first ISR(Interrupt Service Routine) check for any pending interrupt flags and service them before you exit.
    2. Perform the normal exit. Any pending interrupt will take you right back in after executing an instruction after the RETFIE.
    Either way nothing will be lost. Each interrupt has a flag associated with it and you must remember to clear the flag as part of the servicing. I don't think you can nest interrupts on a PIC. The lack of hardware stack probably makes managing this a bit difficult.
  3. atferrari

    AAC Fanatic!

    Jan 6, 2004
    Hola PB,

    Your No. 1 is something I never considered!! Probably better to check I am not affecting any register related to the previous one.

    Good and SO simple!
  4. Papabravo


    Feb 24, 2006
    The only reason you might prefer No. 2 is to give the background program the chance to execute at least one instruction between interrupts. You would do this only in the case that interrupt processing took up most of the available time. Normally this is not the case and even if it is there is an alternate design that will relieve the necessity to spend so much time in an interrupt routine.