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 everybody,
I would like to access a variable in XDATA from different .c source files.
Obviously, that doesn't work the way i'm doing it, but what am I doing wrong?
Here is a tiny code example that shows the problem:
8<----------------------------------------- (main.c) unsigned char xdata receivedstring[1] _at_ 0x124; unsigned char a; void testfunction(void);
void main(void) { receivedstring[0]='q'; a=receivedstring[0]; testfunction(); while(1); }
8<----------------------------------------- (test.c) extern unsigned char xdata receivedstring[1] _at_ 0x124; unsigned char b; void testfunction(void);
void testfunction(void){ b = receivedstring[0]; //doesn't work }
8<-----------------------------------------
I would expect the variable 'b' to be set to 'q' at the end of the program. Watching at receivedstring[0] in the testfunction shows that its content is 'q' at address 0x0124, but the assembly shows that the content of 0x0000 instead of 0x0124 is assigned to the variable 'b':
8<----------------------------------------- //assembly translation:
7: void testfunction(void){
8: b = receivedstring[0]; C:0x001A 900000 MOV DPTR,#0x0000 //why not "DPTR,#0x0124" ? C:0x001D E0 MOVX A,@DPTR C:0x001E F509 MOV b(0x09),A
9: } C:0x0020 22 RET
Thanks for any help!
Tobias
Have you tried leaving out the "_at_ 0x124" at the end of the extern definition ?
It is unnecessary might cause problems. Where a variable actually is located needs to be specified only once, and that was already in the original declaration of the variable.
Yes, after a Mah-Mee... i deleted the "_at_ 0x124" at the extern definition. That caused the problem. (A linker warning would be helpful..)
Thanks and regards, Tobias