avr-gcc-list
[Top][All Lists]
Advanced

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

Re: [avr-gcc-list] A Question Of Optmization


From: Ruud Vlaming
Subject: Re: [avr-gcc-list] A Question Of Optmization
Date: Tue, 15 Apr 2008 22:09:59 +0200
User-agent: KMail/1.9.1

Yeah,

I think it is not as strange as it might seem.

If you say -O0 the compiler will do exactly what you
asked him to do. And this is what you intended.

But if you let the compiler optimize, it recognizes
that your variable TimerTick is set to 10 and will
never change. So it will just make sure the location
reserved for the variable is correctly filled, and 
then will loop forever.

You must tell the compiler explicitly the variable
TimerTick may be changed by a 'backdoor'.
Try 

volatile TimerTick = 10;

Ruud.


On Tuesday 15 April 2008 20:12, Karl O. Feger wrote:

> Dear all,
> 
> I'm relatively new to AVR-GCC and try to do something,
> 
> that is standing procedure in C51 for me. I want to
> 
> program a delay based on a timer-ISR. The code looks
> 
> like this:
> 
> *********************************
> 
> //isr-test, very basic
> 
> // cpu: ATMega128
> 
> // speed: 16 mhz
> 
>  
> 
>  
> 
> #include <avr/io.h>
> 
> #include <avr/interrupt.h>
> 
>  
> 
>  
> 
> unsigned char TimerTick;
> 
>  
> 
>  
> 
> //---------------------------------------
> 
> // Timer/Counter1 Compare Match A
> 
> //---------------------------------------
> 
>  
> 
> ISR(TIMER1_COMPA_vect) // timer_compare_a
> 
> {
> 
>  
> 
>         TimerTick ? TimerTick-- : 0;
> 
> }
> 
>  
> 
> //---------------------------------------
> 
> // main
> 
> //---------------------------------------
> 
>  
> 
> int main()
> 
> {
> 
>  
> 
>  
> 
>         unsigned char a;
> 
>  
> 
>         // --- TIMER1_COMPA irq ---
> 
>         // selected time = 10 ms (160000 ticks)
> 
>         // prescaler = 8 (8 ticks ... 32.768 ms)
> 
>         TCCR1B = (1<<WGM12)|(1<<CS11);
> 
>         OCR1AH = 0x4e;
> 
>         OCR1AL = 0x20;
> 
>         TIMSK |= (1<<OCIE1A);
> 
>  
> 
>         // main loop
> 
>         sei();
> 
>  
> 
>         for (;;)
> 
>         {
> 
>         TimerTick = 10;
> 
>         while(TimerTick);
> 
>         a = ~a;
> 
>         }
> 
>         return(0);
> 
> }
> 
> ********************************
> 
>  
> 
> When I compile this without optimization (-O0)
> 
> Everything is just fine and as expected. When I select
> 
> ANY other optimization level, the resulting assembler
> 
> code looks like this (starting at "TimerTick = 10;"):
> 
> **********************
> 
> 45:            TimerTick = 10;
> 
> +00000086:   E08A        LDI     R24,0x0A         Load
> 
> immediate
> 
> +00000087:   93800100    STS     0x0100,R24      
> 
> Store direct to data space
> 
> +00000089:   CFFF        RJMP    PC-0x0000       
> 
> Relative jump
> 
> ***********************
> 
> so the program stops here and loops.
> 
>  
> 
> Where is my thinking wrong? I tried all sources I could
> 
> imaging.
> 
>  
> 
> Thanks for any help
> 
> Karl
> 
>  
> 
>  
> 
> 




reply via email to

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