We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi,
I try to accces external Memory using XDATA.
I use the following code:
extern volatile unsigned char xdata interface_register _at_ 0x3;
This is the variable I want to acces, like this:
interface_register = 0x82;
Everything ok, so far.
But when I run the code in the simulator, it doesn't work. The compiler generated the following code(dissasembled):
40: interface_register = 0x82; // Interface konfigurieren: PA = Out, PB = IN, PC = OUT 41: C:0x00FB 900000 MOV DPTR,#0x0000 C:0x00FE 7482 MOV A,#DPL(0x82) C:0x0100 F0 MOVX @DPTR,A
So far as I can see, the Datapointer is not set correct. It should be set to 0x02. I can't find the problem.
The corresponding code from the listing is:
; SOURCE LINE # 27 0009 900000 MOV DPTR,#interface_register 000C 748B MOV A,#08BH 000E F0 MOVX @DPTR,A
The compiler used the # Operator to get the value of interface_register, to load it into DPTR. But why does the compiler use the # Operator? It should use the @ Operator to get the definend adress of interface_register (0x2).
Any suggestions? What did I do wrong?
Thanks
omit the word 'extern'; i.e.,
volatile unsigned char xdata interface_register _at_ 0x3;
Not sure of the precise language semantics (I'm sure the manual or others could guide you on that), but this works for me.
in this moment I discovered exactly the same :D
I'm very happy now. Because, I tried to find the error for approx 2 ours.
The reason for the "extern" is, that i defined interface_register in a header file and I want acces to this memory place as well from other C Source files. So I defined them as extern. Like I do it with functions. I know that "extern" doesn't mean in the external memory, or something like that.
But why does this not work with xdata variables?
Thank you very much!
"But why does this not work with xdata variables?"
I doubt it's anything to do with xdata - it'll be the _at_ that's causing the problem!
Think about it: _at_ is only really relevant in a data definition - it is at best irrelevant in an extern declaration
"i defined interface_register in a header file"
That's a really bad idea!
By doing that, every file that #includes the header will produce another definition of the register. Multiple definitions are not allowed.
Your header should contain only extern declarations of the variables
See: c-faq.com/.../decldef.html