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

C51: variables in XDATA - why doesn't that work?

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

8<-----------------------------------------

Thanks for any help!

Tobias

0