Sunteți pe pagina 1din 9

ECTE333 Microcontroller AVR lab report

Experiment 2 & 3
Amish Suchak
4364508
University of Wollongong in Dubai

Experiment 2 Familiarization with the AVR internals


Objective
In this lab you will be shown an example of the operations a microcontroller may
perform. There are three parts to this laboratory. The first part will require you to execute code,
which reads two BCD numbers from a port. Then output the addition and product of the two
numbers to other ports. The second part will require you to analyze the operation of the code.
The final part will require you to modify the code so that the product function is replaced by a
subtraction.

Task 1
Examine the following assembly code.

My observations:
The following code that was given in task 1 had quite a number of instructions that I was
not previously aware of that I was able to understand for my implementation. At first I started off
with understanding what each instruction did and tried to explain it in my own words.

.def BCD1 = r16

This instruction was used to define the BCD1 as a value in register number 16. It was used so
that when we were to enter it in the body of the code, I would be able to use BCD1 and the
programmer would know that its value would be stored in r16.

ldi temp, 0b11111111


out DDRB, temp
out DDRC, temp

This instruction was used in order to define all the pins in port B and port C as the outputs. It can
be seen in the simulation and also on the hardware. Whatever value it is that we would want to
display as LEDs or another form through the two ports. In this scenario, the LDI command was
used to store the binary value 11111111, I know this because of the 0b that was written before the
8 digits. It would have however been better to bitmask and enter these values into the ports rather
than doing so directly.

ldi temp, 0b00000000


out DDRA, temp

This instruction was used to define all the slots in the port A as the inputs. I noticed how the out
command was used because we are not dealing with registers, but with the ports.

in temp, PINA
com temp
mov BCD1, temp
andi BCD1, 0b00001111

For this set of instructions, we started off by saying that the input values are to be taken in from
the PINA. This could be done with the help of a lot of factors but for our case, we would be
inputting with switches. We then use the com instruction to complement the values that are taken
because of the hardware instructions. We then move the values that are taken from the temp into
BCD1 which is basically register 16. Lastly, we use an and gate to mask of the least significant 4
bits and perform the similar operation for BCD2.

mov temp, BCD1 ; temp = BCD1


add temp, BCD2 ; temp = temp + BCD2
com temp ; Invert the bits for use on hardware
out PORTB, temp

In these instructions, we begin with moving the value from BCD1 into the temp register. We then
add temp and BCD2 which stores the value of the addition into the temp register. Then, the bits
are complemented due to the hardware of the AVR m8515 microcontroller and lastly, we output
the results into the PORTB. We then perform the same instruction for multiplication and display
the results in PORTC.

TASK 2
For task 2 of the experiment, I basically had to simulate the program and check the boxes
near PIN A in the I/O view. This is equivalent to inputting 10010110 into port A. This is also
the BCD numbers 0110 and 1001 or in decimal 6 and 9. Once that was done, I was able
to realize that the result of the addition of both the numbers was 15 and the multiplication of both
the numbers came out to be a value of 54 which is what we had initially expected. I also tried
running the code with different values other than 6 and 9 and found out that the code was able to
work properly.

Task 3
Modify the code so that PORTC outputs the difference between the two BCD
values instead of the multiplication. The instruction for subtraction is SUB.
Build and simulate the code. Ensure that it operates as expected
For task 3 of the experiment, it was quite simple to change the code to fit in the subtraction instead of the
addition. The main thing that had needed to be done was to delete the part of the code that said ADD and
changed it to SUB.

Task 4
Answer the following questions

What does the code from task 1 do, step-by-step?


The answer to this question was presented in TASK 1 of this section of the lab report.

What should 10100110 on PORTA give you on PORTB and PORTC if using the code
from task 1?
1010 0110 is binary 10 and 6. PORTB which is the addition of 10 and 6 would show a
value of 16. PORTC which is the multiplication of 10 and 6 would show a value of 60.

What do the swap and com instructions do?


The swap basically swaps the nibbles so that the bits are in the required position. The
com is used to complement the values of the bits such that the 1 becomes 0 and 0
becomes 1.

Where is the result of mul stored in the code from task 1?


According to the code it should be stored in the temp. The value is extracted from r0 back
into the temp and complemented and displayed in PORTC.

Manually work through the code with input 11001000.


1100 1000 is decimal 12 and 8. The output in PORTB is displayed as 20 and the output
in PORTC is displayed as 96.

What changes did you make in the code in task 3?


We copied the same code as the one for the addition but we changed the ADD to SUB.

Assuming the input is 00111011. What should the output on PORTC be for the code in
task 3?

Experiment 3 Writing your first Assembler programs


Objectives
In this lab you will learn to load and read from registers and memory. There are two parts
to this lab. The first part requires you to code a program that loads two variables in a register and
performs a number of arithmetic and logical operations. The second part requires you to perform
the same operations but instead of using the registers, the operations should be performed on two
values stored in memory. The results are also written to memory. The AVR instruction set comes
in very handy in this lab be sure to keep it close at hand. Also understanding the code syntax
provided in lab 1 and lab 2 will aid you in this lab.
Task 1
Write a program that loads two values into registers, r16 and r17. Then perform a number of
operations, storing the results in subsequent registers and memory locations. Ensure that the
values are stored into the correct registers and address locations in each case. Note that the term
mem[0060] denotes the memory location referenced by address 0x0060 (in hexadecimal).
Follow this pseudo-code:

Answer:

.include"m8515def.inc"
LDI r16, 4
LDI r17, 9
STS 0x0060, r16
STS 0x0061 ,r17
ADD r17, r16
MOV r18,r17
STS 0x0062,r18
LDS r17,0x0061
SUB r17 , r16
MOV r19,r17
STS 0x0063, r19
LDS r17,0x0061
MUL r17, r16
MOV r20, r17
STS 0x0064,r17
LDS r17,0x0061
AND r17, r16
MOV r21, r17
STS 0x0065, r21
LDS r17,0x0061
OR r17, r16
MOV r22, r17
STS 0x0066,r22
LDS r17,0x0061

Analysis:
For the entirety of this code, I used mainly the STS and the LDS functions. The STS is used to
take a value from the register in order to store it into the dataspace. The LDS is used to retrieve
the value from the data space back into the register. In retrospect, what I could have alternatively
done is that instead of keeping on retrieving the value of r17 from 0x0061, I could have just
pushed the value of r17 into the stack and then continually popped it after every operation. Also,
I could have avoided the step where I move the value of the operation into another register by
storing the value directly into the data space.

Answer:

.include"m8515def.inc"
ldi r16,2

Main:
CPI r16,1
BREQ LABL1

CPI r16,2
BREQ LABL2

CPI r16,8
BREQ LABL3

LDI r17, 8

LABL1:
ldi r17,5
rjmp main

LABL2:
ldi r17,6
rjmp main

LABL3:
ldi r17,7
rjmp main

My analysis:

For this code, I focused on mainly the implementation of labels and how useful labels are.
Compared to C, labels act as functions. The new tool that I learnt in this project is how to use the
BREQ function. The BREQ literally translates to branch if greater than or equal to, meaning that
if the value is greater than or equal to the proposed value, branch to a label.

Task 5:
Answer the following questions in your lab book:

What values do you expect to be stored in each of registers r18 through r22 for the first
program?

I would expect the values of the various operations between the numbers 4 and 9. For
example, for ADD function, I would expect the value of 13, the SUB function would get the
value of 5, the MUL function would get the value of 36. For the AND and OR, it would take
the value into the binary form and perform an AND gate and an OR gate operation.

Did you obtain your expected values?

Yes

Why were the registers used, starting from r16?

Not all registers are necessarily available for use. Registers 26 to 31 are reserved for index
registers. Additionally, some instructions can only be used on the upper half of the register
file (registers 16+).

Was there any Carry, Negative or Overflow issues observed in the operations?

Yes

Where were these reflected?

I could see if they were by looking at the SRAM.

In Task 3, what instructions were used to skip over the unneeded branches of the code?

BREQ was used to skip the remaining of the code and skip to that particular label.

S-ar putea să vă placă și