Keil Logo

C51: Calling Functions from Interrupts


Information in this article applies to:

  • C51 All Versions

QUESTION

I have created an interrupt service routine as follows:

void isr (void) interrupt 0 using 1
{
.
.
.
}

which calls another function declared as follows:

void doit (void)
{
.
.
.
}

The code inside the doit function fails and causes strange results. What's going on?

ANSWER

Your interrupt service routine operates in register bank 1. The doit function, however, assumes register bank 0. This is the problem.

The reason this is a problem has to do with the way the compiler generates register to register move instructions. On the 8051, there is no register to register move. So, the compiler generates register to memory moves instead. Since the compiler knows the register bank, the physical address of a register in a register bank can be calculated. For example, when the compiler calculates the address of R2 in register bank 0, the address is 0x02. If the register bank selected is not really 0, then the function overwrites this register and another part of the program will likely crash.

This technique of accessing a register using its absolute address is called absolute register addressing.

In the example above, the doit function is looking in the wrong place for its register values.

To solve this problem, you may do one of the following:

  1. Use the REGISTERBANK directive to specify that doit uses register bank 1. No code is generated in the function to switch the register bank. For example:

    #pragma registerbank(1)
    void doit (void)
    {
    .
    .
    .
    }
    
  2. Use the NOAREGS directive to specify that the compiler no use absolute register addressing. This makes the function register bank independent so that it may be called from multiple functions using different register banks. However, the code generated is larger because register to register moves are not allowed.

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.