Professional Documents
Culture Documents
Introduction to AVR-STUDIO
Computer Structure
▪Using the AVR-STUDIO microcontroller programming and debugging environment from ATMEL1.
▪Writing assembler programs for the ATMEGA328P microcontroller and simulating its execution.
We will use the AVR-STUDIO development environment to edit, debug, and simulate code for the Atmel AVR8 processor.
AVR-STUDIO can be downloaded for free from the ATMEL web page (http://www.atmel.com).
We will use the files shown in table 1 during the lab session. Some of the files must be completed as a previous work.
Others will be completed during the session.
It is mandatory to prepare and bring the programs described in the previous work to use them during the lab session.
2.Previous work
You must write two assembler programs. The first one will sum numbers stored in a table. The second will order a list
of numbers (least to greatest). These programs are detailed bellow:
1.Program SUM: The program must sum a table of numbers starting at memory location table. Each number is coded in
a location using 2's complement notation. The result must be coded in a 16-bit 2's complement notation (the most
significant part in register R1 and the least significant part in register R0). The program must follow an algorithm similar
to the following:
X ← table address
count ← table size
result ← 0
while (count > 0)
temp ← data_mem[X]
X ← X+1
if ( temp>0)
sign_extension ← $00
else
sign_extension ← $FF
end if
result ← result + (sign_extension:temp)
count←count-1
end while
//variable definitions
.def temp=r16
.def count =r18
.........
//code segment
.cseg
.org 0 //The firs instruction will be stored at position 0
call initab //this will copy the table from EPROM to SRAM starting at memory position $100
initab:
push r0
push temp
push yl
push yh
push zl
push zh
st Y+,r0
subi temp,1
brne loop
pop zh
pop zl
pop yh
pop yl
pop temp
pop r0
ret
.dseg
.org $100
table: .byte tablesize //space reserved for the table
2.Program order: This program must order a table of numbers from least to greatest.
.include "m168def.inc"
.cseg
.org 0
call initab //this will copy the table from EPROM to SRAM starting at memory position $100
.............................
initab:
/*
This is the same function of the file sum.asm
*/
................
ret
TablaEeprom: .db 10, 4, -1, 0 , 0, 6, 3, 10, 100, -100, 24, 23, -56, 4, 15, 16 ;/*modify this list. Write any list of numbers*/
.dseg
.org $100
table: .byte tablesize //space reserved for the table
3.Lab work
First you must execute the program of the previous work using the AVR-STUDIO development environment. Then
you will have to complete and verify programs using some code from the programs of section 2.
Init AVR-STUDIO. The wizard depicted at fig 1 will pop up. It will let us create a project or open and existing one. If the
wizard does not appear open the Project menu and choose the Project Wizard option.
Push the New Project button and the window depicted at fig. 2 will pop up. Fill in the project name, the project directory
and enable the Atmel AVR Assembler option. If you enable the Create initial file option, an empty text file will be created
where you will have to write your program. If you do not enable it, no new file will be created (useful if the assembler
program file already exist).
Computer Structure 5
After pushing the Next button, the window depicted at fig. 3 will appear. In this last window you must choose the options
AVR Simulator and ATMega328P.
If you did not enable the Create initial file option, you will have to add the file with your program to the project. To do so
right-click on the root of the project tree. The floating menu depicted if fig. 4 will appear. Choose the Add files to project
option to add the desired assembler file.
After this the name of the added assembler file wile be shown at the project tree. You can edit this file by double-clicking
on the file name.
Once you have finished writing the program you will have to assemble the code. To do so choose the Build option from
the Build or clicking on the icon from the tool bar. If everything is right information about the compiled program will be
displayed at the lower part of the window:
Computer Structure 6
AVRASM: AVR macro assembler 2.1.42 (build 1796 Sep 15 2009 10:48:36)
Copyright (C) 1995-2009 ATMEL Corporation
In case of syntax mistakes, the number of the line of the file containing the mistake will be displayed.
AVR-STUDIO includes a simulator that can be used to watch the processor state during the program execution. It is
possible to execute the program instruction by instruction or execute it till reach the desired instruction.
To start the program simulation choose the Start Debugging option from the Debug menu. The windows displayed at fig.
5 will appear:
▪Processor window (on the left): It displays information such as cycle counter, frequency and the content of the registers
PC, SP, X, Y, Z, SREG as well as the general-purpose registers.
▪I/O window (on the right): It shows every device of the selected microcontroller.
▪Memory window (at the botton): It displays the memory content. You can choose the program memory, SRAM memory
and EEPROM memory. We will have to watch the memory content starting at address $100 (we have I/O devices
mapped till address $FF).
Computer Structure 7
The simulator makes it possible to execute the program instruction by instruction. The icon is on the left of the
next instruction that will be executed. The menu depicted at fig. 6 includes very helpful options to be used during the
simulation. When executing step by step, the most used are the following:
▪Step Over: ( ) Execute till the next line. If the current instruction is a subroutine call, the simulation will no stop till
the subroutine execution is completed.
▪Step Into: ( ) Execute just one instruction. If the current instruction is a subroutine call, the simulation will stop
before executing the first subroutine instruction.
▪Toogle Breakpoint: ( ) Set a breakpoint on a line. The option Run ( ) will execute the program till the instruction
marked with the breakpoint.
▪Run to Cursor: ( ) Execute till reach the instruction under the cursor.
Now start the simulation of the execution of the program sum.asm by clicking on Start Debuggin and watch it step by
step. To do so can use the F10 key. Check that the program works as expected and do the following:
1. Simulate the program execution using the data sets of table 2 and check the results.
3. Calculate the number of cycles the program will spend (give a formula in the form N + table length Χ M).
4. Simulate the execution of the program order.asm using the same data sets.
b) If the number of elements in the table is odd, the median is the value written in the middle of the table.
c) f the number of elements in the table is even, the median is the average of the two central elements.
.include "m168def.inc"
.def temp=r16
.def Ordered=r17
.def NumRep=r18
.def Cont=r19
.def Median=r20
.def a=r1
.def b=r2
.cseg
.org 0
call initab
call calculate_median
calculate_median:
Computer Structure 9
call Order
ldi xl,low(Table)
ldi xh,high(Table)
ldi Temp,tablesize
ror Temp
brcc is_even
is_odd:
adiw xh:xl, (tablesize/2 + 1)
ld Median, X
ret
is_even:
adiw xh:xl, (tablesize/2 )
ld Median, X
ld Temp,X
add Median,Temp
ror Median
ret
Order:
/*put here the your subroutine*/
.........
1. Correct the mistakes of the program and check that it works using the data sets of table 2.
2. Write the content of the first 10 program memory locations by generating the .lst file. To do so click on Project ->
Assembler Options (fig. 5) and check the option Create List File. You will have to compile the program again. A
portion of such a file is shown bellow. Note the pair of hexadecimal numerals on the left of each instruction. The first
one is the address of the memory location where the instruction will be stored. The second one is the content of that
memory location, i.e. the machine code of the instruction.
3. Which will be the stack content when the processor executes the first instruction of the order subroutine?