Position: Index > Unclassified >


2017-12-22 22:04  
Declaration:We aim to transmit more information by carrying articles . We will delete it soon, if we are involved in the problems of article content ,copyright or other problems.

This time I decided to lay down some information on using AVR-GCC compiler. I am not going to rewrite whata€?s in documentation is already written, but just write down some issues, what can help you start using this amazing tool.


Probably the most AVR programmers had a question in their mind what compiler to use for their designs. The choice is really big. You can use commercial ones like IAR, CodeVision, ImageCraft. These compilers come with convenient GUIa€?s, code generators. But you have to buy license to use them. So if you are going to make low budget project, the best choice is to use open source compiler GCC . This is the most successful open source compiler in the world. For AVR platform it is called AVR-GCC. The compiler itself has no graphical interface everything can be done in MSDOS command line or in Linux command line. To make it easier to compile project there are makefiles used. GNU compilers as usually are using UNIX style make files. This file can be run with make.exe program. In reality you dona€?t have to write makefiles a€“ there are ready templates to use.
Everyone maybe agrees that use of compiler in command line is not convenient, or for somebody even may seem to be very complicated. Good thin is, that compiler can be used in any IDE. For example open source GUI and editor is Programmers Notepad. Other good choice is AVRStudio4. The creators of AVRStudio made possibility to integrate GCC compiler. Even makefiles are generated automatically. You can program without bothering about other processes that can be done automatically.
So in this article I will try to review following:
1. WINAVR a€“ AVR- GCC distribution (tool set) and usage of them;
2. Makefile basics;
3. AVR-GCC usage in AVRStudio4 environment;
4. AVRLib library usage in your projects;

5. Real example.


WINAVR is windows OS tool set for making AVR microcontroller based projects. This package includes AVR-GCC C/C compiler. Official site http://winavr.sourceforge.net/index.html . There you can download everything. For this article I will use WinAVR20050214 distribution as it is mostly tested and working version. Of course there is already newer one, but this is still fresh and not tested well. You can use it as it has few new features and fixes.The main tools and utilities in WINAVR are:
1. GNU Binutils 2.15 – Utilities (assembler, linker, make and other.)
1) ld – GNU linker;
2) as – GNU assmebler;
Also (seein http://sources.redhat.com/binutils/):
3) addr2line;
4) ar;
5) c filt
6) gprof;
7) nlmconv;
8) nm;
9) objcopy;
10) objdump;
11) ranlib;
12) readelf;
13) size;
14) strings;
15) strip;
16) windres.
2.< a xhref="http://www.gnu.org/software/gcc/" mce_href="http://www.gnu.org/software/gcc/" >GNU GCC 3.4.3 – C and C compiler for AVR.
3. avr-libc 1.2.3 – AVR standard function library.
4. avrdude 4.4.0 – open source program for flashing chips. You can configure or add new chip or programmer by configuring files.
5. uisp 20050207 – other opne source programmer software.
6. avrdude-gui 0.2.0 – avrdude GUI (alpha version).
7. GNU Debugger (GDB) 6.1 – command line debugger.
8. Insight 6.1 – GDB graphical interface.
9. avarice 2.3 – program which allows to interconnect Atmel JTAG ICE and GDB in order to enable debugging AVRa€?s.
10. simulavr – together with GDB it acts as simulator.
11. SRecord 1.21 – Toolset for manipulating EEPROM. Reads writes etc.
12. Mfile – automatic make file generator.
13. Programmers Notepad – editor and GUI for writing programs.
14. Other utilities (make, bach,…);
15 Good documentation in different formats.
16. Makefile template.

Installing WINAVR

Firs of all download WinAVR20050214. You can download newer version a€“ installation is the same. The instructions of installing the toolset is there. Lets say by default we are installing in C:\WinAVR directory. Then GNU Binutils will be in C:\WinAVR\Bin. Because GCC is adapted to AVR platform, then every utility name begins with avr-, for example avr-as.exe, avr-gcc.exe and so on.

Writing Program

It is convenient to use pre-configured Programmers Notepad (PN) to write programs. Of course you can use other GUIa€?s. PN can call any commands from which usually runs in command line and display results. It is convenient to call make utility, which of course calls the compiler. To set these tools go to Options->Tools. All tools you set will appear in Tools main menu:


But lets go from beginning. I thin the best point to start is by describing WINAVR install instruction and PN configuration and how to start create program. PN program can be lounched from c:\winavr\pn\pn.exe or in start menu.


In order to configure tools press Tools-Options menu. Youa€?ll se a window:


As you can see there are already predefined most common tools: Make All, Make Clean, Program. Bu to understand how this works lets go through it. Open Make All tool and press Edit.


Tool fields:
Name a€“ nay name for your tool;
Command a€“ executed command. In this case make.exe;
Folder a€“ directory where is makefile. %d- shows default project directory;
Parameters a€“ parameters needed to execute command. All means that the hole project will be compiled.
Shortcut- just leave it none;
Save a€“ it is good to choose all files. Then program before compiling will save all files, otherwise the compilation will start without saving current project files.
Other fields leave as they are.
Bellow there is a table of most common commands:


Write them all to Tools Options and you will se them in Tools menu.
After everything is configured lets start first project by pressing File->New-Project. Chose folder where it will be saved and name it.


Then create new C file by going to File->New C/C . Write program to project directory. After file is created then press right mouse button on project name in project tree and choose add files. In the file menu select your c file.
The last important step is creation of makefile. There are several ways to create makefile: you can write it by yourself, or you can use template and edit it to meet you project settings.
Lets say our program is:

int main (void)
unsigned char counter;
DDRB = 0xFF;
PORTB |=1<<2;
while (counter !=5)
counter  ;
PORTB &=~(1<<2);
while(counter !=5)
counter  ;
return 1;

You should see like this:


In order to compile program lets create makefile. Open Mfile program from Start->Programs:


In the text window you already see makefile template. If you want to compile project successfully a€“ you have to configure it. Press on menu item Makefile youa€?ll see the list of tools. There is a list of everyone:
1. Code generation a€“ not implemented;
2. Main file name... a€“ chose main program name without a€?.ca€? end;
3. MCU type a€“ select microcontroller type;
4. Output format a€“ select output format. By default use ihex (Intel hex);
5. Optimization level a€“ select optimization level. Just remember when optimization level is higher there is bigger probability for compilation errors to appear;
6. Debug format a€“ debug file format is chosen for debugging software you use;
7. C standard level a€“ chose c language standard. By default it is GNU99;
8. C/C source file(s) a€“ select all C and C source fiels included in project. If these files are in same project directory a€“ you just need to write their names. Otherwise you have to show full path to them;
9. Assembler source files(s) a€“ select all asm source files included in your project;
10. printf() a€“ ability to select printf() function type. You can chose with less resource demand or with floating pint support, which takes a lot of support of jour MCU;
11. scanf() a€“ the same as printf() but for number input;
12. External RAM options a€“ selected if there is external ram used;
13. AVRdude a€“ not implemented;
14. Programmer a€“ select programmer type;
15. Port a€“ select port;
16. Miscelaneous a€“ other not implemented;
17. Enable Editing of Makefile a€“ you can select this if you want manually edit makefile. You can enter all information manually.
When makefile is configured, just save it in project directory without extention. If you save it in different location, you will have to show it in PM Tools menu.
Now you can compile your project by pressing in PN Tools->Make All. If there is no errors you should see messages like this:

>"make.exe" all
-------- begin --------
avr-gcc (GCC) 3.4.3
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
Compiling: main.c
avr-gcc -c -mmcu=atmega8 -I. -gdwarf-2 -DF_CPU=8000000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=main.lst -std=gnu99 -MD -MP -MF .dep/main.o.d main.c -o main.o
Linking: main.elf
avr-gcc -mmcu=atmega8 -I. -gdwarf-2 -DF_CPU=8000000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=main.o -std=gnu99 -MD -MP -MF .dep/main.elf.d main.o --output main.elf -Wl,-Map=main.map,--cref -lm
Creating load file for Flash: main.hex
avr-objcopy -O ihex -R .eeprom main.elf main.hex
Creating load file for EEPROM: main.eep
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" \
--change-section-lma .eeprom=0 -O ihex main.elf main.eep
Creating Extended Listing: main.lss
avr-objdump -h -S main.elf >main.lss
Creating Symbol Table: main.sym
avr-nm -n main.elf >main.sym
Size after:
main.elf :
section size addr
.text 148 0
.data 0 8388704
.bss 0 8388704
.noinit 0 8388704
.eeprom 0 8454144
.debug_aranges 20 0
.debug_pubnames 27 0
.debug_info 214 0
.debug_abbrev 156 0
.debug_line 250 0
.debug_str 138 0
Total 953
AVR Memory Usage:
Device: atmega8
Program: 148 bytes (1.8% Full)
(.text .data .bootloader)
Data: 0 bytes (0.0% Full)
(.data .bss .noinit)
-------- end --------
>Process Exit Code: 0

There you can see results of compilation: size of binaries and so on.

Makefile principals

We ran through makefile creation in earlier paragraph. But this is very important topic to talk about. Many of you are using GUIa€?s without thinking how programs are compiled and how doesna€?t know, what files to include. If your project is very simple, you dona€?t need makefile, you just can run command like this a€?AVR-GCC myprj.ca€? and this will generate object files and hex. But when there are many source files included like 10 or 100 without makefile your life would be hard J. Makefiles are not only for convenient including multiple source files or additional libraries like AVRLIB, but it can help you to save tons of time. For example if you earlier source files were compiled, and you changed something in you main file, then when recompiling your project only your modified file will be recompiled. Imagine if your project has 1000 source files a€“ it can save tons of time. If you want to learn all about makefile, you should read very good tutorial about it here We just go through what is actual for as to compile AVR projects.
Makefile is a file containing all information how to compile your project. There can be variables and instructions who make this tool very powerful thing. As you noticed makefile has no extention and if its name is makefile, you even do not have to show to make.exe program, which file to use. So you can run make.exe command from project directory and all compilation is done. Makefile can have parameters, as you saw All and Clean when configuring PN. So you can run d:\myproj\make all .
Very often when working with projects where are many source files, and you will change something in one of them, but other were compiled in early stages, the make program will check if there were changes done to other files, if not, then compilation will be done only on changed files. But if you change such parameter as optimization level, then all files will be recompiled. As you already know, after compilation there is hex file created which is burned to flash. But there are other files generated like coff files needed for debugging with such programs like AVRStudio. For newer AVRStudio version there is used extended coff a€“ extcoff. You should read in documentation of debugging software about supported formats to select right one. Lets open example makefile from c:\winavr\sample folder.
It can be scaring at first time. But lets go through important parts for us:

# MCU name -(after sign # everything is comments);
MCU = atmega128

There is MCU selected;
Then we define main C file without extension:

TARGET = myproj

Then if there are more source files jus ad them:

SRC = $(TARGET).c otherfile.c

You see that instead of main file there is $(TARGET) variable which is equals to myproj. If there are more files in other directories a€“ you have to write full path to them. For example:

SRC = $(TARGET).c c:otherfolderothercource.c

If there are asm files used in your project they have to be defined too:

ASRC = asm_file.s

ASM files in AVR-GCC have .s extension.
Optimization level is defined as follows:


As you know that in projects often used header files, then they have to be defined as follows:

EXTRAINCDIRS=c:includes d:myprojinc A:includes

Dependencies of c files are generated automatically. If this doesna€?t help just find parameter CFLAGS and in new line type in:

CFLAGS  = $(patsubst %,-I%,$(EXTRAINCDIRS))

This will show to make program to use these directories. The same has to be done with assembler includes:

AFLAGS  = $(patsubst %,-I%,$(EXTRAINCDIRS))

I think it is enough to start your own projects with AVR-GCC. Of source you can define to flash your program after compilation or do other task. How to do this you can find in documentation.

Reprinted Url Of This Article: