Electronic – How to achieve a third interrupt priority level on 8052

8051assemblyembeddedinterrupts

Most standard 8052 platforms allow the user to configure interrupts to be of two possible priorities: higher, during which any pending interrupt must wait, and lower, which can be interrupted by the higher level interrupts, but lower level interrupts can wait.

Now, if you have a job to be done within the interrupt that takes some time, isn't absolutely time-critical, and can be interrupted by other interrupts, the usual approach is to drop whatever data the interrupt collected into a buffer, set a flag that there is the handling of a job pending, and then let the main loop reach a check for the flag, and perform that job.

There is, however, a trick that allows to save some time and have the job executed directly from the interrupt vector, but at main loop priority – interruptible by both interrupt levels, essentially creating a third interrupt level, below the two existing levels.

How does one achieve this?

Best Answer

Here's the trick:

  • You configure your "extra-low priority interrupt" as normal low pri interrupt: set up the vector, activate the interrupt, all the standard stuff.
  • Write the handler procedure at an arbitrary address as usual. Standard interrupt restrictions apply - saving/restoring registers, stack discipline etc. End it with RET, not RETI.
  • At the interrupt vector address perform three operations:
    • push lower byte of your handler address
    • push higher byte of your handler address
    • RETI

That way, the interrupt flag is cleared and normal execution is resumed, but not from the address where the interrupt occurred, but from your handler vector. You can perform the rest of the interrupt activity and then just resume the main loop from where it was interrupted by performing a standard RET - the address stored by the interrupt on stack is still there.