Professional Documents
Culture Documents
Objective:
The objectives of this lab include familiarization with
Hardware Required:
; 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
; 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 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
0 0 0 0 0 0 0 1
- 1 1 1 1 1 1 1 1
___________________________________________
- 0 0 0 0 0 0 1 0
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
Lab01:
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.