ARM Technical Support Knowledge Articles

CONFIGURABLE SFR BIT ACCESSES

Applies to: C51 C Compiler

Answer


Information in this article applies to:


QUESTION

I want to have one file for manipulating C SFR variables that are declared in other modules. I also want to make a library out of it, and include it when I link, so it will use the right SFRs. How can I do this?

ANSWER

This is a little tricky (since SFRs cannot be accessed indirectly), but it can be done.

Let's say that you want to have a library of routines that access STUFF attached to the port pins of the 8051. For example, you have a push button on P1.1 and an LED on P2.3. However, you realize that the next project you do will have a button on a different port and an LED on still a different port.

Make the library generic so you can use it over and over once it is debugged. Obviously, you need to have a way to "assign" the SFR addresses outside of the library. There are several reasons to do this. One is so that you can have a generic library of routines that you use that always work that you provide as a courtesy to clients. You give them the library but NOT the source code.

Here's how to proceed. First, write your C files using external bits and external data unsigned chars to access the SFRs. For example:

//----------------------- C PROGRAM -------------------------
extern bit LED_PORT;
extern bit SWITCH_PORT;
extern data unsigned char LCD_DATA_PORT;


LED_PORT = 0;        /* turn off the led */

a = SWITCH_PORT;     /* read the switch */

LCD_DATA_PORT = 'A'; /* print A on the LCD */
//-----------------------------------------------------------

Now, you need an ASSEMBLY file (not a C file because the _at_ keyword doesn't allow the SFR address ranges) to declare the bits and bytes. For example:

;---------------------- A51 PROGRAM ------------------------
LED_PORT        bit     091h    ; Port 1.1
public  LED_PORT

SWITCH_PORT     bit     0A2h    ; Port 2.2
public  SWITCH_PORT

LCD_DATA_PORT   data    0B0h    ; Port 3
public  LCD_DATA_PORT

         END
;-----------------------------------------------------------

Now, all you need to do is to change the addresses in the A51 program to match your hardware, link it in with the library, and then you have external SFR definitions.

SEE ALSO

Article last edited on: 2005-04-18 09:31:09

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential