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
  • 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