Lesson 2


This lesson will take you through the steps of building a simple program in Xcode and downloading the hex firmware file to the STK500. Then, modifying the program and repeating the build and download process.



Example Project

Download the FirstLight Project disk image. Expand the disk image and drag copy the folder in the volume to your hard drive.

Within the folder is an Xcode project file, FirstLight.xcodeproj. Double click on the xcodeproj file, and Xcode should launch and display a window containing the FirstLight project files.

xcode

The shelf of the Xcode window contains a few UI elements that you will use. Leftmost on the shelf is a popup menu for selecting the Active Target. Below it, in the Group and Files outline view, you can find the actual targets. There are two targets in this project. One is called Make Executable and the other is called Clean. You can select either of these targets in the Active Target popup menu.

The active target is executed when you click on the Build button (the button in the shelf with the hammer icon) or by typing Command-B. You can inspect the target by double clicking on the target itself (the concentric red/white circles in Group and Files). Both of the targets in this project refer to the "makefile."

You can see what is in the project's makefile (a text file in the FirstLight folder) by clicking on the makefile item in Groups and Files. This file should then appear in the Editor of Xcode, at the lower right section of the window. You can click on the Editor button (in the window's shelf) to switch to a larger Editor view.

Notice that the makefile is set up so that the MCU variable refers to the AVR chip that we are compiling for.

# device
MCU = atmega168

The file names of files used in the project appear next.

# file names
MAIN = program
RESULT = firmware

The main program file is assumed to be "program." i.e., program.c is the C file, program.o is the compiled object file, program.map is the memory map. The result files will be called "firmware." The executable file will be named firmware.elf and the Intel format hex file will be called firmware.hex.

You can choose any other names that you wish. If your C program spreads over more than a single source file, you will need to add to the makefile. The creation of makefiles is beyond the scope of this tutorial; you can refer to this if you are interested in more advanced makefiles.

As mentioned, the program is in a file named program.c. Click on that file in the Group and Files view, and the program will appear in the editor, like so.

Picture 4

With Make Executable selected as the Active target, click on the Build button in the shelf. Xcode should end the program to the gcc AVR tools to compile, link and create a hex file to program the AVR chip. With such a small program, the process should be virtually instantaneous.

You should now find many more files in the original folder. Just for fun, select Clean to be your active target and click on the Build button again. You will find that all those extra files have been deleted. An inspection of the makefile will show that the clean target does just that.

Select Make Executable again as the active target and click on Build, to again build the hex file.

Notice that I have included an AVR Tools session file called firstlight.avr in the folder. Double click on firstlight.avr and set up the Program, Device, Programmer and Port tabs in AVR Tools as you had done in Lesson 1. For the Flash file in the Program tab, select the firmware.hex file that was just created with the Build command.

Save the session file so that you need not repeat all of that in the future.

Now switch to the Program tab and click Download. The text view in AVR Tools should show a successful download. But you should not notice any difference in the LED behavior from Lesson 1 since the firmware that you had downloaded in Lesson 1 was created by this same program.


Changing the Program

Click on program.c in Groups and Files to bring the program file into the Xcode editor. Notice that the main function looks like this:

while ( 1 ) {

PORTB =
1 ;
delay(
1000 ) ;
PORTB =
0 ;
delay(
1000 ) ;

}

There are two delay functions, each pausing for 1000 msec. In between the pauses, the least significant bit of Port B of the AVR is switched alternately between 1 and 0. The activity is repeated indefinitely within the while "loop." This is what causes LED0 to repeatedly flash on and off, staying in each state for 1 second.

Change the arguments in the delay functions now from 1000 to 500 each. Then type Command-S to save your edit.

Click on the Build button to recompile and build a new hex file. When that is completed, move over to AVR Tools and click on Download. When the download finishes, you will notice that the LED on the STK500 is now flashing at twice the rate as before.

You have just created a "new" program and downloaded it into mega168 AVR.

You can experiment further with changing the delay parameters, perhaps changing the duty cycle by making one delay longer than the other, etc.

The development cycle is really this simple -- edit the source file (program.c), save it (Command-S), compile and build the hex file (Build button or Command-B) and make any corrections if there are errors, move over to AVR Tools to download it.

When the compiler detects an error, Xcode will report it in the edit window.

The DDRB variable determines which Port B bits are used as output bits, so you will need to change it if you want to light up other LEDs. However, writing C code for the AVR is not within the scope of this tutorial. Please refer to the resources that was mentioned earlier.

Good luck!