Discussion Forum

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

Next Thread | Thread List | Previous Thread Start a Thread | Settings

DetailsMessage
Read-Only
Author
Tomy Lee
Posted
16-Apr-2005 06:15 GMT
Toolset
C51
New! 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!
Read-Only
Author
Tomy Lee
Posted
16-Apr-2005 06:19 GMT
Toolset
C51
New! RE: How can I change SFR (bit) address definition when program is running?
In other words, how can I redefine sfr's in program field,such as:

main()
{
....
sbit ram_en=P1^2;
....
}
Read-Only
Author
Andrew Neil
Posted
16-Apr-2005 08:45 GMT
Toolset
C51
New! RE: How can I change SFR (bit) address definition when program is running?
NO, you cannot do this.

The 8051 instruction set has no way to indirectly address SFRs.
This is part of the processor architecture - nothing to do with any particular toolset.

You will have to do something like:
sbit ram_en0=P1^0;
sbit ram_en1=P1^2;

:

   if( whatever )
   {
      // use ram_en0
   }
   else
   {
      // use ram_en1
   }
Why do you want to do this?
If you say what you're actually trying to achieve here, it would be possible to make a more informed suggestion.
Read-Only
Author
Tomy Lee
Posted
16-Apr-2005 06:22 GMT
Toolset
C51
New! RE: How can I change SFR (bit) address definition when program is running?
Oh, I forgot that I use C51 structure process...

Thanks for your comment!
Read-Only
Author
Andrew Neil
Posted
16-Apr-2005 08:39 GMT
Toolset
C51
New! RE: How can I change SFR (bit) address definition when program is running?
"C51 structure process"

What on earth is that?
Read-Only
Author
Tomy Lee
Posted
16-Apr-2005 14:36 GMT
Toolset
C51
New! RE: How can I change SFR (bit) address definition when program is running?
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!
Read-Only
Author
A.W. Neil
Posted
16-Apr-2005 14:55 GMT
Toolset
C51
New! RE: How can I change SFR (bit) address definition when program is running?
"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?
Read-Only
Author
Tomy Lee
Posted
16-Apr-2005 15:52 GMT
Toolset
C51
New! RE: How can I change SFR (bit) address definition when program is running?
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!
Read-Only
Author
A.W. Neil
Posted
16-Apr-2005 16:05 GMT
Toolset
C51
New! RE: How can I change SFR (bit) address definition when program is running?
You might be able to do this using the Crossbar feature on (some of) the Silicon Labs (nee Cygnal) parts...
Read-Only
Author
Tomy Lee
Posted
16-Apr-2005 16:36 GMT
Toolset
C51
New! RE: How can I change SFR (bit) address definition when program is running?
OK, I see, thanks again!
Read-Only
Author
Hans-Bernhard Broeker
Posted
17-Apr-2005 18:11 GMT
Toolset
C51
New! RE: How can I change SFR (bit) address definition when program is running?
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.
Read-Only
Author
Jon Ward
Posted
17-Apr-2005 19:52 GMT
Toolset
C51
New! RE: How can I change SFR (bit) address definition when program is running?
Take a look at the following knowledgebase article for an idea you may find useful:

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

Jon
Read-Only
Author
Andrew Neil
Posted
17-Apr-2005 21:20 GMT
Toolset
C51
New! RE: How can I change SFR (bit) address definition when program is running?
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...!
Read-Only
Author
Drew Davis
Posted
19-Apr-2005 00:11 GMT
Toolset
C51
New! RE: How can I change SFR (bit) address definition when program is running?
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.
Read-Only
Author
A.W. Neil
Posted
19-Apr-2005 21:05 GMT
Toolset
C51
New! RE: How can I change SFR (bit) address definition when program is running?
"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).
Read-Only
Author
Drew Davis
Posted
20-Apr-2005 04:09 GMT
Toolset
C51
New! RE: How can I change SFR (bit) address definition when program is running?
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...)
Read-Only
Author
erik malund
Posted
20-Apr-2005 13:09 GMT
Toolset
C51
New! RE: How can I change SFR (bit) address definition when program is running?
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

Next Thread | Thread List | Previous Thread Start a Thread | Settings