Optimize Your C Code for 8-bit AVR Microcontrollers

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

 

I just stumbled upon this document at Atmel and i want to share it with you. It may have been posted before but I probably won't hurt to be reminded of again:

It discusses how to optimize code for size or speed by writing. It does not discuss compiler optimization.

Did you know that a increment loop will produce  slightly larger code than a decrement loop? enlightened

 

Tips and Tricks to Optimize Your C Code for 8-bit AVR Microcontrollers 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yup it's good advice though in 3.5 __flash has now superceded PROGMEM.

 

I also cannot agree with 3.7, it's never wise to go hand coding Asm in C unless you have a VERY specific reason to do so (like video generation or something).

 

I also wonder about 4.3. These days the compiler will usually unroll a loop automatically if you optimize for speed not size.

 

The date on the document looks like 2011. Quite a lot has happened to the compiler in the intervening years and it may be worth retesting most things in 4.9.2 (the most recent build Atmel have made available).

Last Edited: Wed. Jul 15, 2015 - 12:10 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'd say 3.2 is a misinterpretation of the compiler optimizing out locals and not globals for some reason.

 

Or, at least, the failure of minimalistic example, where the compiler may be able to keep the local variable in registers.

 

JW

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A more general paper on the topic:

 

http://www.engbloms.se/publicati...

 

http://jakob.engbloms.se/archive...

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

wek3 wrote:

I'd say 3.2 is a misinterpretation of the compiler optimizing out locals and not globals for some reason.

 

Or, at least, the failure of minimalistic example, where the compiler may be able to keep the local variable in registers.

 

JW

LOL, that one caught my eye as well.  If we extrapolate to 2, 3, ..., 100, 200, ... variables, if we use that "tip" an AVR can have any number of variables and use no SRAM.  ;)  Ignore all the stack implications, as well as possible faster/smaller access to a global with LDS/STS vs. an indexing operation off the stack frame.

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.