GNU C Tutorial
( for the Win32 build of avr-gcc 3.3 23-4-2003 )
Contents
Starting AVR Studio
Once the program has started, you will be looking at a screen like this.
Back to Contents
Creating a New Project
In this tutorial we will make a simple C program that decreases the value of one of the
PORT registers, making a binary down counter.
To create a new project, go to the "Project" menu and select "New". The dialog box shown
in the next figure appears. In this dialog box you should enter the project name. We
choose the name count here, but this could of course
be an arbitrary name.
Next you'll have to select the project location. This is is the location where AVR Studio
and avr-gcc will store all files associated with the project. We have used the location
c:\code as the folder. If the folder does not exist, AVR Studio will automatically create it without any further notification.
NOTE: If c:\code already exists, delete this
folder and its contents.
Now we have to select the Project type:
- AVR Assembler: This informs AVR Studio that is should use the built-in Assembler when
compiling the project. No further configuration is required by the user.
- Generic 3rd party C compiler: This option enables the user to manually configure
AVR Studio to use an external compiler when compiling and linking the project. We'll use
this option in the following example.
Select "Generic 3rd party C compiler" and press the 'OK' button to continue.
The Project manager will now appear and look as shown in the figure below. In this view,
you'll see all files associated with your project. It will be empty at this point.
The Project Concept and the GNU make Utility
We will work with the concept of using a new makefile for each project, and hence,
stay with using AVR Studio's "projects" for each project.
All of the project files should be kept in the same folder. In this tutorial, this will
be the c:\code folder. This is necessary for AVR
Studio’s "Generic 3rd party C compiler" support to work properly.
The GNU tool make that comes with the avr-gcc distribution will use the
makefile’s rules to compile and link our project into a
.hex file ready to program into the device.
This is a neat, clean and comprehensible way of organizing your work.
Adding Files to the Project
We now have to add a C file to the project. Download and save
count.c to the
c:\code folder.
Add the file by right clicking the "Source Files" folder in the Project window
and select "Add File...". You will then get the "Add Files to Project" dialog box.
Navigate to the c:\code folder, click on
count.c and then press "Open".
The file count.c is now added to the project by
AVR Studio and placed in the "Source Files" folder in the Project Window.
Now add the makefile for this project. A template for your makefile is supplied with
the avrgcc distribution. Copy the file
makefile from the
c:\WinAVR\sample folder to the
c:\code folder.
Right click the "Other Files" folder in the Project window and select "Add File...".
You will then get the "Add Files to Project" dialog box. Navigate to the
c:\code folder, select the file
makefile and then press "Open".
The file makefile is now added to the project by
AVR Studio and placed in the "Other Files" folder in the Project window.
The Makefile
Open the file makefile for edit by double-clicking on it
in the project window. It will open in the built-in editor. The WinAVR Sample makefile is
too long to reproduce here. So, find the following lines in the file, make the specified
changes and then save the file.
- Line 16: Change the target micrcontroller to at90s8515.
MCU = at90s8515
- Line 22: Change the target to count
TARGET = count
- Line 30: Delete the trailing backslash.
SRC = $(TARGET).c
- Line 31: Delete foo.c bar.c from the line.
- Line 61: Insert a '#' at the beginning of the line.
#LDFLAGS += -Wl,-u,vfprintf -lprintf_flt
- Line 64: Insert a '#' at the beginning of the line.
#LDFLAGS += -lm
- Line 114: Insert $(TARGET).cof into the line.
all: begin gccversion sizebefore $(TARGET).elf $(TARGET).cof $(TARGET).hex $(TARGET).eep \
Tying it all Together
We now have an open project, a source file and a makefile ready to go. But some work
still remains to get all of these things working together. When we have finished these
steps, you will have a method that you can use for other projects and should be fairly
simple to maintain.
There are a few things that we have to realize at this point:
- When we started out, AVR Studio knew nothing about avr-gcc, what it is or where it is.
- Selecting "Generic 3rd party C compiler" as the project type did not initiate any
magic event in your computer. AVR Studio did not suddenly know which compiler to use or
how to use it.
- Avr-gcc is run from the command-line, as well as the other GNU avr tools like
the assembler, linker and the unix tools. They don’t have nice user interfaces. All of
these are most effectively controlled by the program make and are run from the
command line.
So, the question is: How do we get AVR Studio to use avr-gcc?
What we have to do is:
- Get AVR Studio to become aware of the path to the GNU tools via environment
parameters.
- Run make and feed it with the right makefile.
The procedure will differ slightly for Windows 2000 / XP/ NT and Windows 95 / 98 users.
Setting the Target Options
Set the target options by right clicking "Target: Debug" in the Project window
and select "Settings...".
You will then get the "Target Options" dialog box.
- Uncheck the "Run 'compile' on each file in Source Files group" checkbox.
- Check the "Run linker/build stage tools" checkbox.
- Go to the "Run Stage Settings" and select "Run code".
- In the first edit box, type "Errors: none" and in the second edit box,
type "obj" for the extension of the object file if the text is not already there.
- In the "Command line:" edit box:
For Windows 2000 / XP/ NT Users
Type: gcc.bat all.
For Windows 95 / 98 Users
Type: make all
Press the 'OK' button to continue.
Important: Save your project now by selecting "Save" from the "Project" menu.
Building the Project with AVR Studio and avr-gcc
Right click "Target: Debug" in the Project window and select "Build".
The "Project Output" window will now pop up, showing information from the make
output. If all goes well, this project should build with no errors.
Cleaning Up the Project
When we want to rebuild the entire project, we must delete the project output files
from the previous build. Otherwise, make will decide that a rebuild is not
necessary. We will not delete these files manually, instead we will add a new target
in AVR Studio that performs the cleaning up for us.
Right click "Target: Debug" in the Project window and select "Targets" then "Add...".
The "Add New Target" dialog box appears. Enter the name "Clean" for the new target name
and select "Debug" from the "Copy settings from:" drop-down. Press the 'OK' button to
continue.
The target "Clean" will now be available from the "Target" drop-down in the Project
window. Select "Target: Clean".
We need to change the settings for this target. Right click "Target: Clean" in
the Project window and select "Settings...".
You will then get the "Target Options" dialog box. You can see that it has inherited
the settings from the "Debug" target. Replace the word all
with the word clean after
gcc.bat ( or after make for
Windows 95 / 98 Users ) in the "Command line:" edit box.
Press the 'OK' button to continue.
Important: You have just made changes to your project so you will need to save
the project again.
Now, build the "Clean" target by right clicking "Target: Clean" in the Project window
and selecting "Build". This will clean out the products from the make process. Why does
this work? "Clean" is a defined target in the makefile.
Change the target back to "Target: Debug" and build the project again. If you now try
to build the "Debug" project one more time without making any changes, make
doesn't do anything. This is because the makefile, which make uses, is set up
to only rebuild the parts of the project that need to be rebuilt at any given time.
When make determines that the source file has a later timestamp than the project
output files, as a result of editing the source file, it decides to rebuild the project.
The makefile file itself is not included as one of the
dependencies. So a change in this file will not automatically initiate a rebuild.
Debugging with Coff Files
The .o object file that avr-gcc generates does not
contain the necessary information to let AVR Studio watch variables during debugging.
The standard gcc .elf file does include this information
but AVR Studio does not support the ELF format. It does, however, support the COFF
format with variable watch.
If you have just built the project with "Target: Clean" as the target, you will have
to change the project target back to "Target: Debug".
After successfully building the "Target: Debug" project target, we will open the
count.cof file which is created in the project directory.
- Select "Open..." from the "File" menu. You will then get the "Open" dialog box.
Navigate to the c:\code folder, click on
count.cof and press "Open".
- The project window disappears and the "Simulator Options" dialog box appears. Select
AT90S8515 as the device, select the frequency of 4.000000 MHz and press "OK" to continue.
- A window opens showing the C source code from the file
count.c.
- Place the cursor on line 12 at outp(0xFF, DDRB);
and press <F9> to set a breakpoint.
- Open the "Watches" window by selecting "Watch" from the "View" menu. Right-click the
"Watches" window and select "Add Watch". Type in the variable name
value at the cursor and then press the Enter key.
- Reset the program simulation by pressing <Shift+F5> or "Reset" from the "Debug" menu.
- Press <F5> or "Go" from the "Debug" menu to start the program running. Focus
changes to the count.c window and the program breaks
(stops) at the line with the breakpoint.
- Single-step the program by pressing <F10> and watch
value decrement in the "Watches" window.
Back to Contents
Project Development
Development of the project is a cycle of making changes to the source code, building
the project again and debugging the code. These are the steps that you should follow:
- Close the source document count.c in the debugging session.
- Open the project count.apr and make changes to
the source document count.c.
- Ensure that any changes that have been made to the source code have been saved.
- Build the project target "Target: Debug".
- Open the count.cof file that is created in your project
folder. If the source document count.c is still open, you
will get the "AvrDebug" message dialog box asking you to close all source documents in the
project. Click the "Yes" button to continue.
- Start debugging again.
Back to Contents