Turn-key PCB assembly services in prototype quantities or low-volume to mid-volume production runs

Simple tricks that make AVR code more optimized

AVR like other small microcontrollers have very limited resources including speed, SRAM and Flash. Before you get to the middle of your program writing you may notice that you running out of one of those. You may be struggling with exceeding SRAM, or loosing execution speed where you need it most or simply run out of program space. There are times when there is nothing you can do except to look for bigger and faster MCU, but in some cases smart coding tricks may help. Atmel team have prepared pretty good application note where they take through various aspects of code optimization.


Following those advises they managed to reduce code size by 100%, SRAM by 56.5% and execution speed by 33%. Their example was about reading ADC and sending data via USART. Generally speaking they pointed variable sizes – don’t forget that AVR is 8-bit MCU, so using bigger variables always lead to bigger code size and slower processing. Also few tricks allows reduce loop size and speed. Sometimes simple loop enrolling may give a nice kick. Each optimization trick is followed by example with side by side code chunks and resulting compiler output. Might be few tricks you are already using, but when everything is nicely documented it is always worth to have in your library.

[..Source link..]

Bookmark the permalink.

One Comment

  1. Mikkel Holm Olsen

    Hehe, I had to read the app note, because reducing code size by 100% seemed too good to be true πŸ˜‰

    (You have code size and SRAM usage numbers mixed up)

    Also, although the table in the app note also uses the term “execution speed”, it is given in mSec (hence it would be more correct to call it “execution time”). A 33% reduction of “execution time” is an optimization, while a 33% reduction of execution speed is not something to brag about πŸ˜‰

Comments are closed