Вы находитесь на странице: 1из 10

DEPARTAMENTO DE TECNOLOGÍA ELECTRÓNICA

ESCUELA TÉCNICA SUPERIOR DE INGENIERÍA INFORMÁTICA

Introduction to AVR-STUDIO

Computer Structure

1.Introduction and goals


The goals of this session are the following:

▪Using the AVR-STUDIO microcontroller programming and debugging environment from ATMEL1.

▪Writing assembler programs for the ATMEGA328P microcontroller and simulating its execution.

▪Debugging using the AVR Simulator.

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.

Filename Description Notes

A program to calculate the sum of


sum.asm You must complete this before the lab session.
numbers written in a table

A program to order from lowest to


order.asm highest numbers written in a table You must complete this before the lab session.
using 2's complement notation

A program to count pulsations unsing


median.asm You must complete this during the lab session.
BCD notation

Table 1. Files to be used during de lab session.

It is mandatory to prepare and bring the programs described in the previous work to use them during the lab session.

1 A microcontroller supplier, more info at http://www.atmel.com


Computer Structure 2

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

list 1. algorithm for sum.asm.

In order to write the program just complete the file sum.asm:

.include "m168def.inc" // Include IO address definitions, register alias, bits ...

.equ tablesize= ..... //table size

//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

suma: ldi xl,low(table)


ldi xh,high(table) // X ← table
............. //write here the missing code

end: jmp end

initab:
push r0
push temp
push yl
push yh
push zl
push zh

ldi zl, low(2*TablaEeprom)


ldi zh, high(2*TablaEeprom)
ldi yl, low(table)
ldi yh, high(table)
ldi temp,tablesize
loop: lpm r0,Z+
Computer Structure 3

st Y+,r0
subi temp,1
brne loop

pop zh
pop zl
pop yh
pop yl
pop temp
pop r0

ret

/*modify this list. Write any list of numbers*/


TablaEeprom: .db 10, 4, -1, 0 , 0, 6, 3, 10, 100, -100, 24, 23, -56, 4, 15, 16

.dseg
.org $100
table: .byte tablesize //space reserved for the table

list 2. file sum.asm

2.Program order: This program must order a table of numbers from least to greatest.

Num_Rep ←table size


do
X ← table address
Y ← X+1
Num_Rep ← Num_Rep -1
count ← Num_Rep
ordered ← true
do
a ← data_mem[X]
b ← data_mem[Y]
if (a > b)
data_mem[X] ← b
data_mem[Y] ← a
ordered ← false
end if
X ← X +1
Y ← Y +1
count ← count -1
while count > 0
while ordered = false

list 3. algorithm for order.asm

In order to write the program just complete the file order.asm:

.include "m168def.inc"

.equ tablesize=... //table size

.def Ordered=r17 //define variables here


..........

.cseg
.org 0

call initab //this will copy the table from EPROM to SRAM starting at memory position $100

//here the order algorithm


Order: ldi NumRep, tablesize // Num_Rep <- tablesize
Computer Structure 4

.............................

end: jmp end //to finish

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

list 4. file order.asm

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.

Make sure you have all the files listed in table 1.

3.1.An introduction to AVR-STUDIO


You will execute the programs from the previous work using the AVR-STUDIO development environment. The
process to create and carry out an AVR project is described in this section.

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.

Figure 1. Project Wizard window

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

Figure 2. Type and program name selection

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.

Figura 3. Device and platform selecton.

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.

Figure 4. Adding files to the project

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

F:\EdC\tema4\practicas\solsuma.asm(2): Including file 'C:\Archivos de programa\Atmel\AVR Tools\AvrAssembler2\Appnotes\m168def.inc'


F:\EdC\tema4\practicas\solsuma.asm(88): No EEPROM data, deleting C:\Documents and Settings\pdi\Mis documentos\edc\edc.eep

ATmega168 memory use summary [bytes]:


Segment Begin End Code Data Used Size Use%
---------------------------------------------------------------
[.cseg] 0x000000 0x00005e 78 16 94 16384 0.6%
[.dseg] 0x000100 0x000110 0 16 16 1024 1.6%
[.eseg] 0x000000 0x000000 0 0 0 512 0.0%

Assembly complete, 0 errors. 0 warnings

list 5. output after assembling the sum program

In case of syntax mistakes, the number of the line of the file containing the mistake will be displayed.

3.1.1.Using the AVR simulator

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

Figure 5. AVR-STUDIO debugging utilities

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.

▪Step Out: ( ) Execute till find the next return instruction.

▪Reset: ( ) Reset the simulation.

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

2. Which will be the result if table = {100, 200, 300}?


Computer Structure 8

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.

Table content sum


10, 4, -1, 0 , 0, 6, 3, 10, 100, -100, 24, 23, -56, 4, 15, 16 $3A
-12, 10, -14, 12 , 100, -98, -10, 0, 0 , 1, 1, 0 , -10 , 12, 0, 6 -2
100, 90, 80, 70 , 60, 50 ,40 ,30, 20, 10, 0 550

Table 2: Data sets to test the program sum

Figure 6. Debugging menu

3.2.Developing the program median


You must adapt the program order.asm to include it within the list shown bellow. Note that the code includes mistakes
that you must correct. To calculate the median, the program must do the following:

a) Order the table.

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"

.equ tablesize=..... //table size

.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

end: jmp end

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*/

.........

list 6. Program median.asm.

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.

00002c 920f push r0


00002d 930f push temp
00002e 93cf push yl
00002f 93df push yh
000030 93ef push zl
000031 93ff push zh

000032 e8e4 ldi zl, low(2*Tablaeeprom)


000033 e0f0 ldi zh, high(2*Tablaeeprom)
000034 e0c0 ldi yl, low(Tabla)
000035 e0d1 ldi yh, high(Tabla)
000036 e100 ldi temp,TAMTABLAadd
Computer Structure 10

Figure 7: Generating the .lst file

3. Which will be the stack content when the processor executes the first instruction of the order subroutine?

Вам также может понравиться