문제

I have the following C code.

#include <avr/io.h>
int main(void) {
DDRA = 0xFF;
PORTA = 0x00;   
while(1) {
    /*volatile*/ unsigned long int counter;
    for (counter=0; counter<14285L; counter++) {
        PORTA = ~PINA; // run to cursor here
    }
}
return(0);
}

This code is for potential use on the Atmega128 microcontroller board.

Noticed the commented word "volatile". When I simulate it in Atmel Studio with "volatile" commented out it runs at 19 cycles. When it's not commented it runs at 46 cycles.

Why the huge difference?

도움이 되었습니까?

해결책

The volitile keyword is a hint to the compiler that certain optimizations should not be done since the value cannot be assumed to stay the same due to things like interrupts. Without the optimizations, things run slower.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top