Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top