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

help! why?

my codes:

unsigned int i=0;
void Timer(void) interrupt 1
    {
    i++;
    }

void Main (void)
    {
    while (1) printf ("i=%d\n", i);

    }

I had thought that I will get below on serial window

"
i=0;
i=1;
i=2;
i=3;
...
"


but I got

"
i=0
i=-9472
i=-28416
i=29184
i=10240
i=-8960
i=-27904
i=29696
i=10752
i=-8192
i=-27392
i=30464
i=11264
i=-7680
......
"


these values are disorderly and unsystematic, why? You had better try it on your own keilc.

Parents Reply Children
  • The result you expect and the result you are now getting is probably not the result you want. Your while() loop is keeping interrupts disabled 99.9% of the time as there is probably only a jump instruction between EA=1 and EA=0. If you changed your code as follows:

    int x;

    while(1)
    {
    EA=0;
    x=i;
    EA=1;
    printf("%d\n",x);
    }

    you should see the value of x incrementing by a lot more than one for each iteration of the loop. Keeping interrupts disabled almost all the time is almost always a bad idea.

    Incidentally, if you want to printf() an 8 bit value in Keil use "%bd" for signed char and "%bu" for unsigned char.

    Stefan