Jump to content
  • 0

Code optimization pragma


Tobias

Question

hi there,

I have a question about code optimization with MPLAB/gcc.

I have a part of code which is not working while optimization is set to level 1.

So I added this pragma:

 

    #pragma optimize 0
    //Some code...
    #pragma optimize 1

 

Now it is working if I set optimization to level 1 in the MPLAP gcc preferences for this project.

But my question is, what is the gcc doig, if I set optimization to level 0 I the gcc settings? Is it in this case anyways doing optimization with level 1 after using this pragma?

How long is this pragma setting valid, only for this .c or .h file, or for the complete project, ...?

Or how could I manage this, to set the optimization level back to the level it had before setting it to 0?

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

Hi, generally, it skips the the loop iteration to reduce the code size and compilation time. In the below pseudo code, you have a software delay loop to flash LEDs on MCU development platform. Try different optimization level to see what happens. If you use optimization level 0 (no optimization), you will flash LEDs, otherwise, the compiler optimizes out the loop and assumes that the hardware register value stays the same. 

Once you compile the program, you can look at the assembly code. 

See the link http://microchip.wikidot.com/mplabx:configuration to change the optimization level 

Here is a brief introduction about optimization http://www.avrfreaks.net/forum/tutcoptimization-and-importance-volatile-gcc 

 

void delay(unsigned int val)
{
	unsigned int temp;
	unsigned int temp2;

	for(temp = 0; temp < val; temp++) 
		for (temp2 = 0; temp2 < val; temp2 ++) ;
}

int main(void)
{
	// make the port as ouput;
	while(1) {
	 	// turn on the LED;
	 	delay(500);
	 	// turn off the LED;
	 	delay(500);
	}

	return(0);
}

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...