You are on page 1of 17

Department of Electrical Engineering

Faculty Member: Dated: 5/2/2022

Course/Section: BEE-13B Semester: 4th

EE-222 Microprocessors Systems


Lab #1 Introduction to AVR Programming: Simulation &
Hardware Implementation

PLO4 -CLO3 PLO5- PLO8-CLO5 PLO9-


CLO4 CLO6
Name Reg. No Viva / Analysis Modern Ethics and Individual
Quiz / of data Tool Safety and Team
Lab in Lab Usage Work
Perform Report
ance
5 Marks 5 Marks 5 Marks 5 Marks 5 Marks
Muhammad Sufiyan 368154
Sadiq

Abdul Hadi 374120

EE-233 Microprocessor Systems Page 1


Lab #1 Introduction to AVR Programming: Simulation &
Hardware Implementation

Objective:
The objectives of this lab include familiarization with

1. Create Project in Atmel Studio


2. Simulate Assembly code for AVR ATmega16A
3. Burn Hex file in ATmega16A using the Universal Programmer
4. Simulate and understand an Assembly code implementation
5. Qualitatively and quantitatively Compare similar implementations

Hardware Required:

1. ATmega16A Microcontroller Unit


2. Universal Programmer
3. LEDs (may use from trainer kit)
4. Power source with voltage regulator (may use from trainer kit)
5. Switch or button (may use from trainer kit)

Code: (for both lab 00 and 01)

; initial constant’s
ldi R16 , 0xFF
ldi R17 , 0xFF
; set DDRB as output
out DDRA, R17
; code to t o g g l e LEDs
toggler:
subi R16, 0xFF
out PORTA, R16
rjmp idle loop
; delay loop
idle loop:
ldi R19, 0xFF
ldi R20, 0x0F
ldi R21, 0x01
idle_loop_0:
idle_loop_1:
idle_loop_2:
dec R19
brne idle_loop_2
dec R20
brne idle_loop_1
dec R21
brne idle_loop_0

EE-233 Microprocessor Systems Page 2


rjmp toggler

Code for lab 01

; initial constants
ldi R16 , 0xFF
ldi R17 , 0xFF
; set DDRA as output
out DDRA, R17
; code to t o g g l e LEDs
toggler :
subi R16 , 0xFF
out PORTA, R16
rjmp idle loop
; delay loop
idle loop :
ldi R19 , 0xFF
ldi R20 , 0xFF
ldi R21 , 0x01
idle_loop_0 :
idle_loop_1 :
idle_loop_2 :
dec R19
brne idle_loop_2
dec R20
brne idle_loop_1
dec R21
brne idle_loop_0
rjmp toggler

The code is designed to count from 0x00 to 0xFF

EE-233 Microprocessor Systems Page 3


Explanation of Code: (for both lab 00 and 01)
In the lab what we have basically designed a count algorithm.

The typical count algorithm


In a typical count algorithm we would just load a number into register A of 0x00 and then save
the number of times it had been incremented by saving another value inside of another register
and decrementing that and when it would reach 0 we would go back to the start of the loop

The problem
The issue with that is that inside of the atmega16 microcontroller there is an oscillator that is
going at 1MHz and because of that every instruction would occur at 1us and the LED at the
output will flash so fast that we will not be able to see the result fast enough. This is why we
must find some sort of way to add delays to the circuit so that we can at will control the speed of
the output LED’s. So we have to find a way to increase the time delay of our circuit.

The Solution
There are three parts of the solution, first we will make a toggler and this portion of the program
will be responsible for the initialization of the R16 and setting the PORTA as an output and to
jump to the idle loop.

Idle loop part first loads R19 0xFF, R20 loads to 0x0F and loads R21 0x01. What we then do is
that we start decrementing R19 first and loop through it until it equals 0, if it does then we start
decrementing R20 and then through R21 and then jump to the toggler which then displays the
output at the LED’s.

The way it displays is that when we load R16 with 0xFF and then we subtract that with 0xFF so
at the first cycle it should have a value of 0x00 stored at the start.

Then when the toggler is called again it will display the result of 0x00 – 0xFF which If we do the
arithmetic becomes

0 0 0 0 0 0 0 0

- 1 1 1 1 1 1 1 1

____________________________________________

- 0 0 0 0 0 0 0 1

Note: This is done by taking borrow in and the rest is simple math

EE-233 Microprocessor Systems Page 4


Which is a negative number but in binary that doesn’t really matter since we are just interested in
the 1. Just to prove that the algorithm we made will work properly, here is the arithmetic for the
next operation when the value of R16 is 0x01

0 0 0 0 0 0 0 1

- 1 1 1 1 1 1 1 1

___________________________________________

- 0 0 0 0 0 0 1 0

Which is valid because what comes after 0x01 is 0x02.

If we take a look at the loops we have placed at the idle_loop_0 1 and 2, we can see that those
parts of the code will keep decrementing the values and the output will only be visible when the
value in registers R19, R20 and R21 are 0, so basically we can say that the output is visible after

𝑛19 x 𝑛20 x 𝑛21

Consequences
Amount of time, so with the values for lab00 the time it took was 255 * 15 * 1 = 3825us

This is a rough estimate because we have not yet taken into account the other set of instruction
that also take the time of 1us but as 1us is a very small value compared to that of 3825 we will
assume that to be negligible.

In lab01 however we had to change the value of R20 to contain the value of 0xFF which is 255
so now the time becomes

255 x 255 x 1 = 57375us

Ratio of change that takes place is 57375/3825 = 15, so our program is almost 15 times slower
(roughly) so the result of that should be that the LED’s change very slowly

EE-233 Microprocessor Systems Page 5


Simulated observations in a tabular form: (for both lab 00 and 01)

Observations for lab 00

Iteration 1
Cycle I T H S V N Z C
Counter 0 0 0 0 0 0 1 0
Cycles 4
Time 4.00us
R16 0x00
PORTA 0 0 0 0 0 0 0 0

Iteration 2
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 11561
Time 11,561.00us
R16 0x01
PORTA 0 0 0 0 0 0 0 0

Iteration 3
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 23118
Time 23,118.00us
R16 0x02
PORTA 0 0 0 0 0 0 0 1

Iteration 4
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 34675
Time 34,675.00us
R16 0x03
PORTA 0 0 0 0 0 0 1 0

EE-233 Microprocessor Systems Page 6


Iteration 5
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 46232
Time 46,232.00us
R16 0x04
PORTA 0 0 0 0 0 0 1 1

Iteration 6
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 57789
Time 57,789.00us
R16 0x05
PORTA 0 0 0 0 0 1 0 0

Iteration 7
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 69346
Time 69,346.00us
R16 0x06
PORTA 0 0 0 0 0 1 0 1

Iteration 8
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 80903
Time 80903.00us
R16 0x07
PORTA 0 0 0 0 0 1 1 0

EE-233 Microprocessor Systems Page 7


Iteration 9
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 92460
Time 92,460us
R16 0x08
PORTA 0 0 0 0 0 1 1 1

Iteration 10
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 104017
Time 10,401,7.00us
R16 0x09
PORTA 0 0 0 0 1 0 0 0

Iteration 11
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 115574
Time 115,574.00us
R16 0x0A
PORTA 0 0 0 0 1 0 0 1

Iteration 12
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 127131
Time 127,131.00us
R16 0x0B
PORTA 0 0 0 0 1 0 1 0

Iteration 13
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 138688
Time 138,688.00us
R16 0x0C
PORTA 0 0 0 0 1 0 1 1

EE-233 Microprocessor Systems Page 8


Iteration 14
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 150245
Time 150,245.00us
R16 0x0D
PORTA 0 0 0 0 1 1 0 0

Iteration 15
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 161802
Time 161,802.00us
R16 0x0E
PORTA 0 0 0 0 1 1 0 1

Iteration 16
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 173359
Time 173,359.00us
R16 0x0F
PORTA 0 0 0 0 1 1 1 0

Iteration 17
Cycle I T H S V N Z C
Counter 0 0 0 0 0 0 0 1
Cycles 184916
Time 184,916.00us
R16 0x10
PORTA 0 0 0 0 1 1 1 1

Iteration 18
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 196473
Time 196473
R16 0x11
PORTA 0 0 0 1 0 0 0 0

EE-233 Microprocessor Systems Page 9


Iteration 19
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 208030
Time 208,030.00us
R16 0x12
PORTA 0 0 0 1 0 0 0 1

Iteration 20
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 219587
Time 219,587.00us
R16 0x13
PORTA 0 0 0 1 0 0 1 0

Observation from lab01

Iteration 1
Cycle I T H S V N Z C
Counter 0 0 0 0 0 0 1 0
Cycles 4
Time 4.00us
R16 0x00
PORTA 0 0 0 0 0 0 0 0

Iteration 2
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 196361
Time 196361.00us
R16 0x01
PORTA 0 0 0 0 0 0 0 0

EE-233 Microprocessor Systems Page 10


Iteration 3
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 392718
Time 392718.00us
R16 0x02
PORTA 0 0 0 0 0 0 0 1

Iteration 4
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 589075
Time 589075.00us
R16 0x03
PORTA 0 0 0 0 0 0 1 0

Iteration 5
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 785432
Time 785432.00us
R16 0x04
PORTA 0 0 0 0 0 0 1 1

Iteration 6
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 981789
Time 981789.00us
R16 0x05
PORTA 0 0 0 0 0 1 0 0

EE-233 Microprocessor Systems Page 11


Iteration 7
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 1178146
Time 1178146.00us
R16 0x06
PORTA 0 0 0 0 0 1 0 1

Iteration 8
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 1374503
Time 1374503.00us
R16 0x07
PORTA 0 0 0 0 0 1 1 0

Iteration 9
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 1570860
Time 1570860.00us
R16 0x08
PORTA 0 0 0 0 0 1 1 1

Iteration 10
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 1767217
Time 1767217.00us
R16 0x09
PORTA 0 0 0 0 1 0 0 0
Iteration 11
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 1963574
Time 1963574.00us
R16 0x0A

EE-233 Microprocessor Systems Page 12


PORTA 0 0 0 0 1 0 0 1

Iteration 12
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 2159931
Time 2159931.00us
R16 0x0B
PORTA 0 0 0 0 1 0 1 0

Iteration 13
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 2356288
Time 2356288.00us
R16 0x0C
PORTA 0 0 0 0 1 0 1 1

Iteration 14
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 2552645
Time 2552645.00us
R16 0x0D
PORTA 0 0 0 0 1 1 0 0

Iteration 15
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 2749002
Time 2749002.00us
R16 0x0E
PORTA 0 0 0 0 1 1 0 1

EE-233 Microprocessor Systems Page 13


Iteration 16
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 2945359
Time 2945359.00us
R16 0x0F
PORTA 0 0 0 0 1 1 1 0

Iteration 17
Cycle I T H S V N Z C
Counter 0 0 0 0 0 0 0 1
Cycles 3141716
Time 3141716.00us
R16 0x10
PORTA 0 0 0 0 1 1 1 1

Iteration 18
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 3338073
Time 3338073.00us
R16 0x11
PORTA 0 0 0 1 0 0 0 0

Iteration 19
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 3534430
Time 3534430.00us
R16 0x12
PORTA 0 0 0 1 0 0 0 1

Iteration 20
Cycle I T H S V N Z C
Counter 0 0 1 0 0 0 0 1
Cycles 3730787
Time 3730787.00us
R16 0x13
PORTA 0 0 0 1 0 0 1 0

EE-233 Microprocessor Systems Page 14


Proteus Simulation: (for both lab 00 and 01)

Lab 00

The only thing that changed when uploading the code for lab00 and lab01 was the fact that the
rate of count was decreased and became more visible.

EE-233 Microprocessor Systems Page 15


Lab 01

Hardware Implementation: (for both lab 00 and 01)

Lab01:

EE-233 Microprocessor Systems Page 16


What changes you have Observed in lab01 by changing only one line in the code write
them in submission. (Qualitative and Quantitative Comparison both)

I have observed that the delay for a blinking LED is taking longer than it did previously meaning
previously where we could not see the individual states of the LED as they were going very fast
but now it is going at a much slower pace.

When we take a look at the amount of time it takes according to the simulation we saw that the
time it took for the same number of iteration

Conclusion:
The conclusion that we have drawn from this lab is that beginning this course of
microprocessors, how to write code in assembly language and how to simulate the circuits inside
of Atmel Studio and how to insert breakpoints and view the status of the memory of the
microcontroller. We also learnt how to check the value at the ports.

We learnt how to burn our tested assembly code by using chip max 2 and learnt how to hookup
the pins of an Atmega16 microcontroller to view the outputs.

EE-233 Microprocessor Systems Page 17

You might also like