This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How can I change SFR (bit) address definition when program is running?

In my application, I want to easily change address of SFR include user's commands. Such as:

sbit ram_en=P1^0;
......
/*In another place, I want to change ram_en to P1^2 dynamiclly.*/

I am try to do this(not definition field) in my code:
...
sbit ram_en=P1^2;
....

But it can not pass when compiling.
So, how can I implement this function?

Best regards!

Parents Reply Children
  • Sorry, that's my mistake, it should be "processor"...

    I have one idea to make some C51 learning system that user can directly modify some interface definition from console. Such as, user can either connect IIC device to P1.0 and P1.1, or P3.0 and P3.1, they just need redefine SDA from P1.0 to P3.0, etc.
    Thus it does not need re-compile code. This can give user more flexibility and more convenience.

    Best regards!

  • "Sorry, that's my mistake, it should be 'processor'..."

    Hmmm... "C51 structure processor" still doesn't mean anything. What do you mean?

    "Thus it does not need re-compile code. This can give user more flexibility and more convenience."

    Well, yes - more flexibility and more convenience, but fundamentally impossible on an 8051!

    I think you would be misleading your students by suggesting that they can do this without having to recompile.

    Anyhow, how long does it take to recompile such student projects?

  • Hehe, I believe it will take less time to re-compile project code. But I just want to make one system that can cast off IDE and programmer when I change some pin functions, that's it.
    If so, it seems I have to give up this idea....

    Thanks for your help!

  • You might be able to do this using the Crossbar feature on (some of) the Silicon Labs (nee Cygnal) parts...

  • There is a way around this limitation, if you're doing your own hardware: create a von-Neumann wired RAM area and write self-modifying code into it.

  • Take a look at the following knowledgebase article for an idea you may find useful:

    http://www.keil.com/support/docs/101.htm

    Jon

  • http://www.keil.com/support/docs/101.htm

    Jon,

    that still requires re-building the Project; Tomy wants to have this dynamically reconfigurable at run time...!

  • 1) Self-modifying code. Requires execution out of RAM, and mapping program store into xdata space so that you can write to it. Such programming developed an evil reputation back in the day before people stopped feeling so squeezed they had to do it.

    2) Step back from the bits, and just read the whole port byte. You can then keep a variable for your current desired bit position. Instead of

    if (my_sbit)...
    

    write

    U8 my_mask = 1 << my_bit;
    if (P1 & my_mask)...
    

    Changing the value of my_mask then tests different bits in P1. You can pre-calculate the mask when you choose a bit to test.

    3) Run-time dispatching
    U8 my_bit;
    
    bit result;
    switch (my_bit)
        {
        case 0: result = P1^0; break;
        case 1: result = P1^1; break;
        case 2: result = P1^2; break;
        ...
        }
    // use result
    

    Not pretty and probably not even as fast as (2) unless you spend a lot more time changing my_bit than you do testing P1.

  • "2) Step back from the bits, and just read the whole port byte."

    But he also wants the port number to be run-time configurable - not always P1!
    So he'd have to do this in conjunction with (3).

  • I missed that requirement. There are no pointers to SFRs possible in the 8051 architecture. So, yes, you'd need another
    level of dispatching for the port byte:

    if (ReadCurrentPort() & my_mask)
    

    Or fold it all together into an extended version of (3).

    (You could also define N ports * 8 tiny functions and use an index into a constant table of function pointers. But I find that the compiler is pretty good about turning switch statements into jump tables on its own. The function pointer table could sometimes be handy for moving the constants out of code space.)

    Maybe that self-modifying code isn't looking so bad after all.

    (Yes... yes... it's just this once... feel the hate flow through you... come to the Dark Side...)

  • I have one idea to make some C51 learning system that user can directly modify some interface definition from console

    So, you are going to make a learning system that makes the "student" learn methods that are not possible in real life.

    Please keep us informed as to who learns from it so we can avoid hiring them.

    Thanks

    Erik