Keil Logo

C51: What is the Best Way to Disable/Re-Enable Interrupts?


Information in this article applies to:

  • C51 All Versions
  • C251 All Versions

QUESTION

What's the best way to disable interrupts (for ISR configuration) and reenable them when I'm done?

I know that the 8051 has an EA (enable all) flag for interrupts, but is it guaranteed that the interrupt does not occur after the instruction EA=0?

ANSWER

There are 2 good ways to accomplish this.

  1. Use a #define to create definitions for ENABLE_INTERRUPTS and DISABLE_INTERRUPTS. For example:

    #define ENABLE_INTERRUPTS { EA = 1; }
    #define DISABLE_INTERRUPTS { EA = 0; }
    

    Then, in your program:

    void ISR_setup (void)
    {
    DISABLE_INTERRUPTS
    // Set timer reload
    // Set timer mode
    // EtCetera
    ENABLE_INTERRUPTS
    }
    
  2. Use the #pragma disable directive to disable interrupts for an entire function. Then, you can create a function that sets up the ISR or whatever and not even DEAL with the EA=0 bit. For example:

    #pragma disable
    void ISR_setup (void)
    {
    // Set timer reload
    // Set timer mode
    // EtCetera
    }
    

    The benefit of this method is that #pragma disable restores the previous state of the EA bit. So, if interrupts were disabled when you entered this function, they will still be disabled on exit.

The hardware of the 8051 guarantees that interrupts are disabled after execution of EA = 0. So you need not to worry about any pipeline effects.

MORE INFORMATION

  • Refer to DISABLE in the Cx51 User's Guide.

SEE ALSO


Last Reviewed: Thursday, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.