[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-gcc-list] A Question Of Optmization
From: |
Jonathan Blanchard |
Subject: |
Re: [avr-gcc-list] A Question Of Optmization |
Date: |
Tue, 15 Apr 2008 17:06:20 -0300 |
You must declare TimerTick as volatile. Like this :
volatile unsigned char TimerTick;
If not the optimizer will see that it do not get modified again inside
the loop so optimize it out and it the delete the empty loop.
Declaring it as volatile tell the compiler that it must check the
value each time it is used.
--
Jonathan Blanchard
On Tue, Apr 15, 2008 at 3:12 PM, Karl O. Feger <address@hidden> 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
>
>
>
>
> _______________________________________________
> AVR-GCC-list mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/avr-gcc-list
>
>