GNU C Tutorial

(for the Win32 build of avr-gcc 3.3.2 included with WinAVR 20040404)
Updated - 18 September, 2004 Author: Peter Sutton based on an earlier version by Len Payne.

Contents


Introduction & Setup

We will use Programmer's Notepad as the development environment for C programs for the AVR. 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. We will use a tool called make to compile the C program and create the necessary hex file to download to the AVR board. Make uses a file called makefile to control this build process. We will use the sample makefile supplied with WinAVR and make some changes to it to suit our purposes.

Create a new directory (folder) for this tutorial, e.g., H:\avr_c_tutorial. Copy (not move) the sample makefile from C:\WinAVR\sample\makefile to the folder you just created.

Back to Contents


Starting Programmer's Notepad

Start Programmers Notepad 2 by navigating as follows::

Start Menu -> Programs -> WinAVR -> Programmers Notepad [WinAVR]

Create a new C file:

Type (or cut and paste) the following program into the Programmer's Notepad window.

/*/*
* FILE: count.c - Binary down counter for the GNU C Tutorial.
*/

#include <avr/io.h>

int main(void)
{
unsigned char value;

/* Set PORT B direction to output*/
DDRB = 0xFF;

/* Initialise value */
value = 0xFF;

/* Run forever - "for(;;)" is the same as "while(1)" */
for (;;) {
/* Write value to Port B */
PORTB = value;

/* Decrement value */
value--;
}}

Save the file as count.c in the folder you created earlier (e.g. H:\avr_c_tutorial). Before we can compile it, we must make some changes to the makefile.

Back to Contents


Changing the Makefile

From within Programmer's Notepad open the makefile from your tutorial directory: (File menu -> Open then select the file.)

This is rather long file (396 lines) which specifies the compiler and other tools and directories used in the compilation process. You should make the following changes for this tutorial: (You can press <Ctrl-G> in Programmer's Notepad to go to a particular line number - or note the line number shown in the bottom left hand corner of the Programmer's Notepad window.)

Note that line 44 of the makefile defines the source file(s) which will be compiled in this program. In this case it is $(TARGET).c. When writing makefiles, $(NAME) results in a symbol substitution - it is replaced with whatever NAME is defined as. Because we have defined TARGET=count, $(TARGET).c will be replaced by count.c. When we're building a program which spans multiple C files, we will need to list all of the filenames here.

Save the makefile.

Back to Contents


Building the Program

We're now ready to build the program. The WinAVR version of Programmer's Notepad comes with a number of WinAVR tools predefined. Choose "[WinAVR] Make All" from the Tools menu:

This will result in various tools being executed - you will see the output within Programmer's Notepad. It should look something like the following: (It may take a minute or so for this to appear)

> "make.exe" all

-------- begin --------
avr-gcc (GCC) 3.3.2
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Compiling: count.c
avr-gcc -c -mmcu=at90s8515 -I. -g -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=count.lst -std=gnu99 -Wp,-M,-MP,-MT,count.o,-MF,.dep/count.o.d count.c -o count.o

Linking: count.elf
avr-gcc -mmcu=at90s8515 -I. -g -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=count.o -std=gnu99 -Wp,-M,-MP,-MT,count.o,-MF,.dep/count.elf.d count.o --output count.elf -Wl,-Map=count.map,--cref -lm

Creating load file for Flash: count.hex
avr-objcopy -O ihex -R .eeprom count.elf count.hex

Creating load file for EEPROM: count.eep
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" \
--change-section-lma .eeprom=0 -O ihex count.elf count.eep

Creating Extended Listing: count.lss
avr-objdump -h -S count.elf > count.lss

Creating Symbol Table: count.sym
avr-nm -n count.elf > count.sym

Converting to AVR Extended COFF: count.cof
avr-objcopy --debugging --change-section-address .data-0x800000 --change-section-address .bss-0x800000 --change-section-address .noinit-0x800000 --change-section-address .eeprom-0x810000 -O coff-ext-avr count.elf count.cof
Discarding local symbol outside any compilation unit: .do_copy_data_start
Discarding local symbol outside any compilation unit: .do_copy_data_loop
Discarding local symbol outside any compilation unit: .do_clear_bss_start
Discarding local symbol outside any compilation unit: .do_clear_bss_loop

Size after:
count.elf :
section size addr
.text 100 0
.data 0 8388704
.bss 0 8388704
.noinit 0 8388704
.eeprom 0 8454144
.stab 684 0
.stabstr 1387 0
Total 2171

Errors: none
-------- end --------


> Process Exit Code: 0

If you open the tutorial folder from Windows Explorer, you will notice that quite a number of files have been created as a result of this process:

The files of most interest to us are:

count.hex
This is the hex file we can download to the AVR device (as per the PonyProg tutorial)
count.cof
This is the file we can use to simulate the program within AVR Studio 4.

Make works on the basis that it only recompiles or runs those tools which are necessary to bring a project up to date. If you select "[WinAVR] Make All" from the Tools menu of Programmer's Notepad again, you will notice that the output this time is different - fewer tools are run.

If you want to rebuild the project from scratch, you can select "[WinAVR] Make Clean" from the Tools menu of Programmer's Notepad. This will delete all of the created files (not the source files) and you can select "[WinAVR] Make All" to rebuild the project.

Back to Contents


Debugging with Coff Files

Extended COFF files contain information to allow AVR Studio 4 to simulate the code and watch variables etc.

Start AVR Studio 4 as follows:

Start Menu -> Programs -> Atmel AVR Tools -> AVR Studio 4

Don't create a new project - just open the count.cof file:

From the following window select the debugging platform (AVR Simulator) and the target device (AT90S8515):

This will cause AVR Studio to start debugging the program. The source file (count.c) will be opened and the program position pointer (yellow arrow) will initially be pointing to the main() function:

As with the AVR Assembly Language tutorial, it is possible to step through the program and observe the values on various ports and in various registers. Expand the "I/O AT90S8515" item, then expand "PORTB". Step through the program (repeatedly press <F11>) and observe what happens on PORTB:

It is also possible to use AVR Studio 4 to watch C variables. Open the Watch window: View -> Watch and type the name "value" (without the quotes) into the first Name field:

The value of the "value" variable is shown. Step through the loop a few times and watch the value change. Note also the "Location" field - this variable has been allocated to a particular register on the AVR (R24 in the figure above).

It is also possible to set breakpoints just as with Assembly Language programs. Right click on the "value--;" statement and select "Toggle breakpoint" - you'll notice a small red circle appear next to that line of code. Repeatedly press <F5> ("Run program"). Each time, the program will run until it reaches the breakpoint then pause. Notice how the value changes in the watch window:

If you make changes to the source file (e.g. in Programmer's Notepad) and recompile (Make All), you need to stop debugging (Debug -> Stop Debugging) and then restart debugging (Debug -> Start Debugging) to cause AVR Studio to reload the new COFF file. (You do not need to close and reopen the file.)

Close AVR Studio and/or Programmer's Notepad when you're finished with them.

Back to Contents