 | Discussion Forum |  |
|
|
passing ports to a functionNext Thread | Thread List | Previous Thread Start a Thread | Settings | Details | Message |
|---|
Read-Only Author Justin Moses Posted 24-Nov-2002 19:59 GMT Toolset C51 |  passing ports to a function Justin Moses I have a program in which I have to manipulate 4 LEDs. I have a function that is designed to find out exactlywhat I need to do to each LED ie flash, fade, dim, etc but in order to save on code space I would like to send the program the LED by reference. For instance on of my functions looks like the following:
void switchon(&sbit LED, bit turnon)
{
if(turnon)
{
LED=ON;
}
if(!turnon)
{
LED=OFF;
}
}
where LED is the LED I am currently wanted to manipulate. I keep getting errors with this code. I have even tried just passing the LED by value and as a bit. Nothing seems to work. Is this valid?
Thanks Justin Moses | | Read-Only Author Hans-Bernhard Broeker Posted 24-Nov-2002 20:59 GMT Toolset C51 |  RE: passing ports to a function Hans-Bernhard Broeker It's quite simple, really: "pass by reference" doesn't exist in C. You have to pass a pointer instead:
void switchon(sbit *LED, bit turnon)
{
*LED=(turnon ? ON : OFF);
}
| | Read-Only Author Dan Henry Posted 24-Nov-2002 22:19 GMT Toolset C51 |  RE: passing ports to a function Dan Henry "It's quite simple, really: "pass by reference" doesn't exist in C. You have to pass a pointer instead:"
Except that bits are not "addressable" and therefore cannot be directly passed by reference. Also, presuming LED is a port (meaning SFR) bit, the SFR cannot be passed by reference assuming you want to directly dereference it. | | Read-Only Author Dan Henry Posted 24-Nov-2002 22:40 GMT Toolset C51 |  RE: passing ports to a function Dan Henry "I have even tried just passing the LED by value and as a bit. Nothing seems to work. Is this valid?"
No. To understand why, refer to the C51 User's Manual. You also need to understand the limitations of the 8051's memory/SFR architecture and instruction set.
Once you understand these limitations, you can devise a function that does what you want using switch, shifts, and bit masking. This will be a much less "direct" solution than what you were probably hoping for. | | Read-Only Author Jon Ward Posted 25-Nov-2002 00:27 GMT Toolset C51 |  RE: passing ports to a function Jon Ward The following knowledgebase article may be helpful:
http://www.keil.com/support/docs/168.htm
Jon | |
Next Thread | Thread List | Previous Thread Start a Thread | Settings |
|