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

program thinking 019c and 009c are equal

the code is as follows:

the declaration of two global variables:
unsigned short int xdata x;
unsigned char xdata META_TEDS[MAXINBUFF];
/*MAXINBUFF = 128*/

later in a function I have:
for( x=2;x<12;x++ ){
printf( "UUID %d\r\n", x );
printf( "UUID address %p\r\n", &x );
printf( "META_TEDS address %p\r\n", &META_TEDS[x] );
META_TEDS[x]=_getkey( );
printf( "UUID %d\r\n", x );
printf( "UUID address %p\r\n", &x );
printf( "META_TEDS address %p\r\n", &META_TEDS[x] );
}

all works well until the iteration where &META_TEDS[x] = 009c. here is what i get output in hyperterm:

/*previous iterations are correct*/

UUID 8
UUID address x:019c
META_TEDS address x:009a
UUID 8
UUID address x:019c
META_TEDS address x:009a
UUID 9
UUID address x:019c
META_TEDS address x:009b
UUID 9
UUID address x:019c
META_TEDS address x:009b
UUID 10
UUID address x:019c
META_TEDS address x:009c
UUID 12554
UUID address x:019c
META_TEDS address x:009c

the iteration where x=10 is where the problem lies. under default and OPTIMIZE(0) it behaves the same. the assembly for the code segment "META_TEDS[x]=_getkey( );" is as follows:

; SOURCE LINE # 84
00AD 120000 E LCALL _getkey
00B0 900000 E MOV DPTR,#x+01H
00B3 E0 MOVX A,@DPTR
00B4 FE MOV R6,A
00B5 7400 E MOV A,#LOW META_TEDS
00B7 2E ADD A,R6
00B8 F582 MOV DPL,A
00BA E4 CLR A
00BB 3400 E ADDC A,#HIGH META_TEDS
00BD F583 MOV DPH,A
00BF EF MOV A,R7
00C0 F0 MOVX @DPTR,A

it seems that in the above segment during the said iteration what _getkey() retrieves is put into 019c (x) instead of 009c (META_TEDS[x]).

-Nathan

  • I entered your application and tried it and it works just fine.

    #include <reg51.h>
    #include <stdio.h>
    
    
    #define MAXINBUFF 128
    
    unsigned short int xdata x;
    unsigned char xdata META_TEDS[MAXINBUFF];
    
    void main (void)
    {
    SCON  = 0x50;               /* SCON: mode 1, 8-bit UART, enable rcvr      */
    TMOD |= 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload        */
    TH1   = 221;                /* TH1:  reload value for 1200 baud @ 16MHz   */
    TR1   = 1;                  /* TR1:  timer 1 run                          */
    TI    = 1;                  /* TI:   set TI to send first char of UART    */
    
    
    for( x=2;x<12;x++ ){
      printf( "UUID %d\r\n", x );
      printf( "UUID address %p\r\n", &x );
      printf( "META_TEDS address %p\r\n", &META_TEDS[x] );
      META_TEDS[x]=_getkey( );
      printf( "UUID %d\r\n", x );
      printf( "UUID address %p\r\n", &x );
      printf( "META_TEDS address %p\r\n", &META_TEDS[x] );
    }
    
    while (1);
    }
    

    I also examined the assembler output you listed and it works perfectly.

    It appears that you have a stray pointer or an interrupt routine that corrupts some part of memory.

    Since this happens to UUID (x), why don't you set a breakpoint for writes to the c variable.

    Jon

  • we have not modified the _getkey function and we have no pointers in the code. everything is a global variable. we are however running at 9600 baud using the following settings:

    T3CON = 0x82;
    T3FD = 0x12;
    SCON = 0x52;

    instead of reg51.h we are including aduc834.h from analog devices. it contains all that reg51 has plus maybe 2 or 3 other lines that reg51 does not have. we are using the ADuC834 evaluation board, and win2k sp3.

    -Nathan

  • You're using %d to print chars - which is a very common mistake.
    You need to use %bd

  • >You're using %d to print chars - which is a very common mistake.
    >You need to use %bd

    No he's not. The variable being printed with %d is "x", which is a short int.

    Note that x was 10d = 0x0A
    it got changed to 12554d = 0x310A
    So it looks like an ASCII '1' has been written into the upper byte of x.

    How is XDATA implemented on your hardware? Maybe the upper address byte is not being latched properly, or something else is modifying it.

  • Keil use "big endian" storage for integers, so the upper byte of x is at address 0x019c, so I am confident that you have a hardware problem with the upper half of the address for XDATA.

  • it seems that in the above segment during the said iteration what _getkey() retrieves is put into 019c (x) instead of 009c (META_TEDS[x])

    If you think that's the case, then you should modify your program to output what's stored in META_TEDS[x] each iteration.

    Jon

  • Thanks for all the help guys, I found out the problem. Since i got on this project a few months ago we have been using the same eval board and I have only changed a few of its link positions. until recently we have never had over 256 bytes in the program so everything functioned with LK9 in the B position. We are now up to about 458 in xdata and LK9 in position B has been causing these inconsistancies in our variables. With LK9 in position A it does function properly. Thanks again for your comments.

    -Nathan