GNU C Tutorial

( for the Win32 build of avr-gcc 3.3 23-4-2003 )
Updated - 4 August, 2003 Author: Len Payne

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:

Select "Generic 3rd party C compiler" and press the 'OK' button to continue.

fig2.jpg

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.

fig3.jpg

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".

fig4.jpg

fig5.jpg

The file count.c is now added to the project by AVR Studio and placed in the "Source Files" folder in the Project Window.

fig6.jpg

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".

fig7.jpg

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.

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:

So, the question is: How do we get AVR Studio to use avr-gcc?

What we have to do is:

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...".

fig8.jpg

You will then get the "Target Options" dialog box.

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".

fig10.jpg

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.

fig11.jpg


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...".

fig12.jpg

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.

fig13.jpg

The target "Clean" will now be available from the "Target" drop-down in the Project window. Select "Target: Clean".

fig14.jpg

We need to change the settings for this target. Right click "Target: Clean" in the Project window and select "Settings...".

fig15.jpg

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.

fig16.jpg

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.

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:

Back to Contents