[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [avr-gcc-list] register wierdness?

From: Dean Ferreyra
Subject: RE: [avr-gcc-list] register wierdness?
Date: Sat, 6 Apr 2002 21:37:07 -0800


> I seem to have my port-A data-direction register (DDRA)
> clobbered when a piece of unrelated (and, for that matter,
> uncalled) code is added.  Attached is a C source file which
> reproduces the problem.
> main() initializes DDRA to 0xff, and then proceeds to write
> incrementing values to PORTA to blink some LEDs.  Normally, the
> LEDs blink quite brightly. When the unrelated sourceline (in
> write_byte()) is compiled in, they do not - somehow DDRA is
> being reset to 0, because the blinking is very dim!

If your DDRA was reset, you would not see any blinking at all, so your DDRA
is probably fine.

I believe what you're seeing is the compiler's optimizer changing the timing
of your code:

> int main() {
>       unsigned char i = 0;
>       outp(0xff,DDRA);
>       while(1) {
>               unsigned char j = 0, k = 0;
>               outp(i++,PORTA);
>               while(++j) {
>                       outp(j,PORTC);
>                       while(++k);
> #endif
>               }
>       }
> }

If you want to add delays using loops then you should define your loop
variable using the "volatile" keyword:

        volatile unsigned char k = 0;

This will indirectly tell the optimizer not to throw out your loop that,
from the optimizer's perspective, does nothing of consequence to the rest of
the program:

>                       while(++k);

The optimizer is always correct, but it is not always consistent, which may
explain why unrelated code changes make your "problem" appear or disappear.

I suggest that you use the volatile keyword and experiment some more.

Good luck.


avr-gcc-list at http://avr1.org

reply via email to

[Prev in Thread] Current Thread [Next in Thread]