AvrStudio Tutorial.pdf

(101 KB) Pobierz
316730475 UNPDF
Setting up AVRStudio for GCC
Projects
Tutorial (c) Dean Camera, 2006.
dean_camera@hotmail.com
While AVRStudio is NOT my personal preference for a front-end to AVR-GCC (that honor goes to
the Programmers Notepad project), it is for many. Unfortunately, many people seem to use mis-
configured project options with AVRStudio which result in less than optimal builds of their
projects. To try to rectify this, I'll try to add hints here to achieve a better programming experience
and end result.
Fonts: Out with Courier New!
Courier New is a horrible programming font - it's hard on the eyes and just seems so blocky. Having
a good programming font is the first step to a good programming environment; after all you'll
probably be staring at your code for many many hours. And who wants to read something that
makes your eyes bleed?
My personal preference is the Microsoft Consolas font, size 9. It's fixed width (a MUST for ALL
programming fonts) and is pleasantly styled. Most importantly, it scales well both upwards and
downwards and thus is great for those with poor eyesight.
Far more importantly however, is to turn on Cleartype if available on LCD monitors. It will reduce
eyestrain and make everything easier to read.
Optimization: Choose what's best for YOU
AVR-GCC has quite a sophisticated optimizer under its hood - this allows it to compile your code
in a manner best for either size, speed or a combination of the two. Optimization is very powerful
and extreamly useful, however AVRStudio defaults to it being turned off.
To turn on the optimizer, choose Project->Configuration Options from the top menu. Change the
"Optimization" combo box from it's initial setting (-O0, off) to the setting you desire and click OK.
The most common optimizer setting is -Os, which compiles for the smallest resulting code size
possible. This produces quite fast code too, so it's a good choice. If speed is of the essence, choose
-O3.
For more information on the different settings, check out the GCC manual online. DON'T FORGET
TO READ THE AVRLIBC FAQ - there are some things you need to be aware of when writing
code that will be optimized!
You should also set your AVR clock's frequency in the appropriate box on this screen; this value is
used by several library routines to determine delay lengths.
Main compile flags: At no extra cost!
Also in the same Configuration Options screen are several checkboxes to the right of the optimizer
combobox. I recommend checking them all.
Unsigned Chars - This makes all "char" variables unsigned by default unless explicitly declared as
signed. Unsigned chars are used far more than signed, so this is a good idea.
Unsigned Bitfields - This also defaults bitfields to unsigned unless explicitly declared as signed.
Useful for projects which use bitfields to manage flags in registers and variables.
Pack Structure Members - Choosing this will cause GCC to pack structure members as close
together as possible (to save on RAM).
Short enums - Enums in GCC-C are ints by default (16 bits wide). This switch forces all enums to
become unsigned chars in size instead.
File generation: Help at hand
A final thing to look at on the first tab of the Configuration Options screen is the three checkboxes
along the bottom of the window.
Create HEX File - Ticked by default. This causes GCC to both compile your project and produce a
resulting HEX binary which you can use to program into an AVR.
Generate MAP file - When ticked GCC will also create a .map file in your project's directory. This
file contains information on where your global variables and functions are located in your project's
binary. Useful for debugging.
Generate list file - A BRILLIANT option which many miss. This causes GCC to make a .lss file in
your current directory containing both your entire C code and linked AVRLibC functions as well as
the resultant assembly code after each C line. This allows you to see just what functions are taking
up all your room, as well as see what exactly your lines of C are producing once compiled.
Libraries: Don't re-invent the wheel!
Another feature overlooked by many. AVRLibC, the C library that comes with GCC, has several
library files which should be linked with your project so that their hand-optimized routines are used
in place of GCC's generic routines.
Failing to link with the correct libraries will NOT produce any GCC errors in most cases - but your
resultant binary will be larger and slower than it needs to be.
From the PROJECT->CONFIGURATION OPTIONS screen, select the "Libraries" tab. Libc and
Libm should be linked to all your projects, so select them both and click the "Add Library" button.
If your project makes used of the printf or scanf functions, you should link against the appropriate
libraries from this screen too. The "_min" variants of each library are the minimal versions forgoing
some of the advanced features available in the larger versions.
Zgłoś jeśli naruszono regulamin