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 to use xdata?

Hi

This is code compiled with Keil C, but not working

#include <AT89X51.H>

xdata char Index _at_ 0x00;
void Clear_DDRAM(char index);

void main(void)
{
Clear_DDRAM(0x00);
while(1);
}

void Clear_DDRAM(char index)
{
int a;
for(a=0;a<65536;a++)
{
P3_4=0; //IOCS=0;
P3_1=0;
P3_0=0;
P1_0=0; //Reset=0;
Index=index;
P3_4=1; //IOCS=1;
Index=Index+a;
}
}

The xdata has 64K address range.
Here is my queation:
How to series to write data(0x00) into next address(Star address at 0x00). The assembly code is "INC DPTR" so have any instruction like it in keil C.

  • try some readable code, once I found Index was not index, I gave up.

    I know that some belong to a club with the motto "If C is readable, it is not true C", please resign your membership and post again.

    Erik

  • Hsu,

    I think people are having a hard time understanding your question. It looks like you are attempting to write a routine to initialize your xdata space. It looks like you want a function that will take a value passed in, and then write that value to every location in xdata. For instance, you want the call:

    Clear_DDRam(0x00)

    to fill the whole xdata address space with zeroes. Is that right? If so, your code is very confused. Maybe try something like this:

    void Fill_DDRAM(unsigned char value);
    
    void main(void) {
       Fill_DDRAM(0x00);
       while(1);
    }
    
    void Fill_DDRAM(unsigned char value) {
       unsigned char xdata * data Index;
       int data loopcount;
       Index = 0x0000;
       P3_1 = 0;
       P3_0 = 0;
       P1_0 = 0;
       for (loopcount = 0; loopcount < 65535; loopcount++) {
          P3_4 = 0;
          *Index = value;
          P3_4 = 1;
          Index++;
       }
       *Index = value;  //Take care of last location
    }
    

    Note that I've changed your loop. The loop as you wrote it is an infinite loop. An in will ALWAYS be less than 65536. I have changed my loop to "<65535" and then took care of the last location outside the loop. It's ugly, but you get the idea.

    Hope that helps.

  • Hi,

    Assuming that the above interpretation(s) is/are correct and all that is required is to fill an area of xram all with the same value, can't the library function memset be used to fill it?

    Mark.

  • Hi Everyone
    I post my assembler code and use a illustration says what it is meant to say and
    hope you can clearly understand my questions?

    Assembler code
    ;------------------------------
    DDRAM_RESET:
    MOV DPTR,#0000H
    MOV R0,#00H
    MOV R1,#00H
    MOV A,#00H
    RESET1: ;reset from 0000h to FF00h
    CALL ZEROWR
    INC DPTR
    INC R0
    CJNE R0,#00H,RESET1
    INC R1
    CJNE R1,#FFH,RESET1

    RESET2: ;reset 00FFh more
    CALL ZEROWR
    INC DPTR
    INC R0
    CJNE R0,#FFH,RESET2

    RET
    ;--------------------------------------------ZEROWR: ;Write 00 to MEM(DDRAM) thru 1352
    CLR P3.4 ;MEMCS#=0,
    CLR P3.1
    CLR P3.0
    CLR P1.0 ;Ereset=0;
    MOVX @DPTR,A
    SETB P3.4 ;MEMCS#=1
    RET
    ;------------------------------------------
    Illustration:

    The data(index)=0x00
    __
    |00|Index(Address 0) | Next address
    |__| V
    |00|Index(address 1)
    |__|
    . .
    . .
    __
    |00|Index(Address 65534) | Next address
    |__| V
    |00|Index(address 65535)
    |__|

    The external of memory start address(Index)=0x00
    The next address(Index+1)=0x01

  • My guess it

    char xdata *xPtr;

    xPtr = 0;
    *xPtr = 0;

  • Mark,

    Sure, but I don't know if this is just a learning sort of test case for him and he wants to actually do something else in practice. For instance, maybe he wants to change the function to write a pattern to memory or something. Who knows?

  • Hsu,

    That's what my code does. Also, you should not be calling the variable that holds your data "index." Aside from the fact that you already have a variable called "Index" and this is confusing, the "index" variable isn't being used as an index. Just take a look at the code I posted, and see if it's not what you're doing.

    Also, you use a CLR operation on P3.1, P3.0, and P1.0 INSIDE your loop, but nothing ever sets these high. You should put these up at the start of DDRAM_RESET for efficiency.

  • Dear Jay

    Thanks for your suggest code that I've tried and modify for my application. It is OK.