Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
05/02/02 07:40
Read: times


 
#22405 - RE: passing port pin to a function
Well, everyone else had a crack at this. I may as well give it a shot, too.

The most obvious solution (which won't work on the 8051) is to pass the address of the SFR. Well, guess what! You can't pass the "address" of an SFR. The reason is that the 8051 does not support indirectly accessing SFRs. So, you can't generate an SFR pointer.

Something else that is not so obvious is that the address of the SFR cannot be changed at RUN TIME without creating SELF-MODIFYING code. The reason comes back to the lack of indirect access to SFR space on the 8051. It is NOT a compiler limitation. It is a limitation of the 8051 architecture.

The only reasons you would want to pass an SFR to a function is because you want to A) protect the source code (by providing only an OBJ file or a library) or B) create your own personal set of library routines (to do things like bit-banged I2C or LCD panel control).

With these things in mind...the best solution (in my opinion) is to use externally defined symbols.

In your C code, you must extern the bits or SFRs you want to access. For example:

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 */

We'll assume that your function is ACTUALLY in a library. The externs are not required to be resolved in the library. They are also not required to be resolved in the same source file. They are resolved and "fixed-up" by the linker at Link Time. This means the actual addresses may be located in ANY object module that is available to the linker.

So, we can define the addresses for the externs in assembly file...

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

...that we include in the linkage.

Obviously, you must provide a sample file (like my assembler example) which defines the default values for the LED_PORT, SWITCH_PORT, and LCD_DATA_PORT. It is then up to the end-user to modify these for the ACTUAL program. The externs your function (in the library?) uses are fixed-up at link time.

Gees. I hope all that made sense.

Jon Ward
Keil Software

List of 25 messages in thread
TopicAuthorDate
passing port pin to a function            01/01/70 00:00      
RE: passing port pin to a function            01/01/70 00:00      
RE: passing port pin to a function            01/01/70 00:00      
RE: passing port pin to a function            01/01/70 00:00      
RE: passing port pin (to Bert)            01/01/70 00:00      
RE: passing port pin (to Bert)            01/01/70 00:00      
RE: passing port pin (to Mahmood)            01/01/70 00:00      
RE: passing port pin to a function            01/01/70 00:00      
RE: passing port pin (to Mahmood)            01/01/70 00:00      
RE: passing port pin (to Mahmood)            01/01/70 00:00      
RE: passing port pin (to Mahmood)            01/01/70 00:00      
RE: passing port pin (to Mahmood)            01/01/70 00:00      
RE: passing port pin (to Mahmood) J.            01/01/70 00:00      
RE: passing port pin (to Mahmood) J.            01/01/70 00:00      
RE: passing port pin (to Mahmood) J.            01/01/70 00:00      
RE: passing port pin (to Mahmood)            01/01/70 00:00      
RE: passing port pin (to Mahmood)            01/01/70 00:00      
RE: passing port pin (to Mahmood)            01/01/70 00:00      
RE: passing port pin to a function            01/01/70 00:00      
RE: passing port pin to a function            01/01/70 00:00      
RE: passing port pin to a function            01/01/70 00:00      
RE: passing port pin to a function            01/01/70 00:00      
RE: passing port pin to a function            01/01/70 00:00      
RE: passing port pin to a function            01/01/70 00:00      
RE: passing port pin (to Mahmood)            01/01/70 00:00      

Back to Subject List