Keil Logo

C51: CHANGING OPTIMIZER LEVEL FOR A SINGLE FUNCTION


Information in this article applies to:

  • Cx51 Version 6.0 or higher

QUESTION

I'm using optimizer level 9 (Common Block Subroutines) and code that accesses the timed access protected SFRs (on parts like the Dallas 320, 520, 530, and 390) is replaced by subroutines. This causes the timed access to fail. For example, the following C code:

void TimeAccess (void)  {
  EA = 0;
  TA = 0xAA; TA = 0x55;
  RTCC = 1;
  TA = 0xAA; TA = 0x55;
  TRIM |= 0x80;
  TA = 0xAA; TA = 0x55;
  TRIM |= 0x40;
  EA = 1;
}

is compiled to the following:

             ; FUNCTION Com0005 (BEGIN)
0000         L?0006:
0000         L?0007:
0000 75C7AA            MOV     TA,#0AAH
0003 75C755            MOV     TA,#055H
0006 22                RET
             ; FUNCTION Com0005 (END)

             ; FUNCTION TimeAccess (BEGIN)
0000 C2AF              CLR     EA
0002 120000      R     LCALL   L?0006
0005 75F901            MOV     RTCC,#01H
0008 120000      R     LCALL   L?0006
000B 439680            ORL     TRIM,#080H
000E 120000      R     LCALL   L?0007
0011 439640            ORL     TRIM,#040H
0014 D2AF              SETB    EA
0016 22                RET
             ; FUNCTION TimeAccess (END)

If I compile with optimization level 8 or lower, this problem goes away, however, my program is then too big. Is there a way to change optimizer levels for just one function?

ANSWER

Yes. The Keil C51 Compiler allows you to specify the optimization level on a function-by-function basis. You may change the optimization level for critical functions as shown below:

#pragma SAVE        // save current optimization level
#pragma OPTIMIZE(8) // prevent common subroutine optimization

void TimeAccess (void)  {
  EA = 0;
  TA = 0xAA; TA = 0x55;
  RTCC = 1;
  TA = 0xAA; TA = 0x55;
  TRIM |= 0x80;
  TA = 0xAA; TA = 0x55;
  TRIM |= 0x40;
  EA = 1;
}

#pragma RESTORE    // restore previous optimization level

SEE ALSO

FORUM THREADS

The following Discussion Forum threads may provide information related to this topic.

Last Reviewed: Monday, November 28, 2005


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.