|
|||||||||||
|
Technical Support Support Resources
Product Information |
C51: ACCESSING EXTERNAL SFR'S IN A C PROGRAMInformation in this article applies to:
QUESTIONIn C51, how can I declare SFRs in one source module and access them as external declarations in another source module? ANSWERFirst, you need to declare the SFR variables in an assembly program as shown below.
; ---------------------- 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
; -----------------------------------------------------------
Then, you can write a C program to access these "external" 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 */ // ----------------------------------------------------------- This is useful if you want to create a library of routines that access STUFF attached to the port pins of the 8051 like 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. Furthermore, you want to 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 why this is a cool idea. 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. To make this work, 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 PRESTO, you have external SFR definitions! SEE ALSO
FORUM THREADSThe following Discussion Forum threads may provide information related to this topic.
Last Reviewed: Wednesday, January 25, 2006 | ||||||||||
|
|||||||||||
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.