AVR Studio Tutorial
( for Version 3.56 )
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 program that increases the value of one of the
PORT registers, making a binary 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 led here, but this could of course
be an arbitrary name.
Next you'll have to select the project location. This is the location where AVR Studio
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. We'll use this
option in the following example.
- 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.
Select the "AVR Assembler" Project type 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.
Adding an Assembler File
We now have to add an assembly file to the project. This can be done in two different
ways, either by adding an existing assembly file, or by creating a new file. As
illustrated by the figures below this is how it is done:
- If you wish to add an existing assembly file to the project, right click the
"Assembler Files" folder in the Project window and select "Add File...". You will then
get the "Add Files to Project" dialog box. Navigate to the desired folder, select
the file to add and click the "Open" button.
- If you wish to create a new file, right click the "Assembler Files" folder in the
Project window and select "Create New File...". You will then get the "Create new file"
dialog box. Navigate to the desired folder, enter the name of the file to create and
click the "OK" button. The file will be created by AVR Studio. Note that you'll have
to manually enter the .asm extention for your file.
In this tutorial, you need to create a new file called led.asm.
This file is created by AVR Studio and placed under the "Other Files" folder in the Project
Window. You need to drag the icon for the file and place it in the "Assembler Files" folder in
the Project Window as shown in the figure below.
The new file is automatically marked as "Assembler Entry File". This indicates
that the assembler will start with this file when assembling the project. The
"Assembler Files" folder can only contain one file marked as an entry file. The current
entry-file is indicated by a red right-arrow on the file icon, all other files will be
indicated with blue down-arrows.
Editing the Assembler file
We have now added a new but empty file to our project. The next step is to fill this
file with our code. Open the led.asm file for edit by
double-clicking on it in the project window. It will open in the built-in editor. The
file is empty and you'll have to manually enter the following code: (or you may Copy
and Paste the code below directly into the editor window.)
;******************************************************
.include "8515def.inc"
rjmp RESET ;Reset Handle
;******************************************************
RESET:
.def temp =r16
ldi temp,low(RAMEND)
out SPL,temp
ldi temp,high(RAMEND)
out SPH,temp ;init Stack Pointer
ser temp
out DDRB,temp ;Set direction out
loop:
out PORTB,temp
inc temp
rjmp loop
|
The editor window should now look something like the following picture:
Assemble the Source Code
This example requires the file named
8515def.inc to be
placed in the c:\code folder. This file is the definition
file for the AT90S8515, and contains definitions for the microcontroller needed by the
assembler. Copy the file
8515def.inc from the
C:\Program Files\Atmel\AVR Studio\Appnotes
folder to the c:\code folder.
The next step now is to assemble the file. This is done by selecting "Assemble" from the
"Project" menu, or by pressing <F7>.
The "Project Output" window will now pop up, showing information from the assembler.
From this window we can see that the code is 10 words (20 bytes), and assembly was
completed with no errors.
We are now ready to advance to the next step, which is running the code in simulator mode.
Back to Contents
Simulating the Code
At this point we have generated the files needed to simulate the code. To start running
the code, select "Trace into" from the "Debug" menu, or press
<F11>.
Selecting the Device
You will now get the window shown below. Since we do not have an In-Circuit Emulator
attached, AVR Studio enters simulator mode. In the Simulator Options window you select
the device for which you want to simulate the code. Select AT90S8515 as the device.
Select the frequency of 4.000000 MHz and press "OK" to continue. The Memory and
Architecture is not used since we have selected a standard device. These options
are used when selecting "Custom" as the device.
Instruction Pointer
Now take a look in the editor view, you'll see that a yellow right-arrow has appeared
in the left margin of the code. This arrow indicates the position of the program counter.
In other words, it points to the next instruction to be executed. What we want to do now
is to set up the IO views so that we can have a closer look at what is happening on the
Port B registers during program execution.
Setting up the IO View
Open the IO window by selecting "New IO View" from the "View" menu.
Since we have selected the AT90S8515 during the Simulator options setting this IO view
will be opened automatically.
The IO Window is used to inspect and modify the contents of the I/O registers in the
execution target. The standard configuration gives you a quick overview of the hardware
with the ability to expand particular items for more information.
Open the "Port B" tree by double clicking on the icon for the port. It will now expand
and show all registers associated with Port B, these are: Port B Data register (PORTB),
Data Direction (DDRB) and Input Pins (PINB). As shown each bit in the registers are
represented by a checkbox. A logical 'zero' (0) is represented by a checkbox without a
tick and a logical 'one' (1) is represented by a checkbox with a tick.
These checkboxes will be updated during program execution, and show the current state of
every bit. You may also set and clear these bits by clicking on the appropriate checkbox
at any time during the program execution.
Single Stepping the Program
There are two commands to single step through the code. These are "Step Over"
<F10> and "Trace Into"
<F11>. The difference between these commands is
that <F10> do not trace into subroutines. Since
our example does not contain any subroutines, there is no difference between the operation
of these commands in this example.
Now single step down to the last line of code
(rjmp loop) by repeatedly pressing the
<F11> key or by selecting "Trace Into" from the
"Debug" menu. Notice how the colour changes from black to red on the registers that
change value. This makes it easier to identify which registers change value on each
instruction. Continue pressing the <F11> key and
see how the binary value in Port B is increased.
Setting Breakpoints
Breakpoints are a method of halting execution flow. By adding a breakpoint in the
assembly code we can run run the program at full speed, and it will be stopped at the
line with the breakpoint. By now you have noticed that you have to press
<F11> three times to go through the loop once. We
will add a breakpoint at the rjmp loop
instruction to show how this can be used to speed up the debug process. Place the
cursor on the rjmp loop instruction in
the source view window and press <F9> (or the
"Toggle Program Breakpoint" in the "Breakpoints" menu ). A red square will appear in
the left margin of the source view window as shown. By pressing
<F5> or "Go" from the "Debug" menu the program
will start running and break (stop) at the intruction with the breakpoint.
Modifying the Code
Now we want the program to count down instead of up. To make this change we'll
have to edit the source code. Place the cursor in the source view, and change the
inc to a
dec instruction. If you now press
<F5> (Go) the following dialog box will appear.
This box indicates that one of the source files has been changed, and that the project
should be rebuilt. Press "Yes".
The Project will now be rebuilt, and the instruction pointer will start at the first
line of code. Notice how the breakpoint is remembered.
Opening the Watch View
Open the Watch window by selecting "Watch" from the "View" menu.
Variables that are defined (by the .def directive)
can be placed in the Watch view. The only defined variable in this code is the
temp variable. Right-click the "Watches" window and
select "Add Watch". Type in the variable name temp at
the cursor and then press the Enter key. As we continue to run through the program the
temp variable will constantly be updated during
program execution.
Setting up the Processor View
Now we will set up the Processor view. Open this view by selecting "Processor" from
the "View" menu.
This view shows processor specific information like the state of the Flags register and
the current value of the different pointers. In this view you will also find a Cycle
Counter and a StopWatch. These are very useful if you wish to measure the length of a
loop or how much time a specific subroutine uses. We will not use this view directly
in this example, but it provides a lot of useful information during debugging of a
project.
Saving the Project
Before exiting AVR Studio we will save our project. AVR Studio will remember where the
views are placed and will use this setting when opening the project later. To save the
project select "Save" from the "Project" menu.
Back to Contents
Changing the Output File Format
When we have finished simulating the program, we need to download the program to the
hardware and run it there. To do this, we need to generate "Intel Hex Format Output".
Generating Intel Hex Format Output
We need to change the code generation output file format to "Intel Hex". So what we
will do is change the Project Settings. Right-click on "Target: Default" in the
Project view and then select "Project Settings...".
You will now get the window shown below. Go to "Code generation", select
"Intel Intellec 8/MDS (Intel Hex)" from the "Output file format:" drop-down and press
the "OK" button.
Assemble the file again by selecting "Assemble" from the "Project" menu, or by
pressing <F7>. The file
led.hex is created in the
c:\code folder.
Generating Object Format Output
When simulating the program in AVR Studio, the code generation output file format needs
to be changed back to "Object Format". Right-click on "Target: Default" in the
Project view and then select "Project Settings..." as before. Go to "Code generation",
select "Object format for AVR Studio" from the "Output file format:" drop-down and press
the "OK" button.
Assemble the file again by selecting "Assemble" from the "Project" menu, or by
pressing <F7>. Now, start simulating again.
Back to Contents