0% found this document useful (0 votes)
22 views13 pages

Microcontrollers

Uploaded by

Shaheer Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views13 pages

Microcontrollers

Uploaded by

Shaheer Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Microcontrollers

& Embedded Systems


(MTS -331)
DE-44 Mechatronics
Syndicate – B
Lab Report/Week # 5

Lab Title: Loops and lookup tables and Delay in Assembly

Names of Members:
• Muhammad Hamza Zahoor Reg # 398659
• Hammad Asim Reg # 432331

Submitted to: LE Muhammad Qasim


Abstract and Objectives:
To learn about assembly programming to perform basic tasks. The lab also focused on the purpose of
loops and lookup tables in assembly language which are the basis of performing tasks with your
controller. Delays play a crucial role in controlling the timing of a program's execution. Delays are essential
for tasks such as interfacing with external devices, generating accurate time intervals, and ensuring proper
synchronization in embedded systems. One commonly used technique for implementing delays is through the
use of the DJNZ (Decrement and Jump if Not Zero) command.

Loops:
Loops are one of the building blocks of logical circuits. They are imperative to reduce the complexity
of any code. Rather than rewriting a block of code, we can put it in a loop that will execute that piece
of code an number of times we tell it to.
In C++ language if we want to rewrite any piece of code we use for, while, do while, etc. meanwhile
in assembly language we use jump instruction, we have two types of jumps, conditional (a condition
being true, we jump) and unconditional (regardless of the state we will jump) jumps, where we jump
to a specific label from defined in the code.

Instructions Actions
JZ Jump if byte is zero
JNZ Jump if byte is not zero
DJNZ Decrement byte and jump if byte is not zero
CJNE A, Byte Jump if A ≠ Byte
CJNE reg, #data Jump if Byte ≠ #data
JC Jump if CY = 1
JNC Jump if CY = 0
JB Jump if bit = 1
JNB Jump if bit = 0
JBC Jump if bit = 1 clear bit
Figure 1: All the Jump instructions on the AT89C51

Nested loops:
Nested loops are also an important topic under loops and are necessary sometimes as you may need
to loop a block more than 256 times (max number that can be counted from 8 bits). Hence, we use
nested loops that can count to 256*256 times (65,536).
It is normal that we don’t normally want to repeat a block of code that many times but rather we focus
on implementing a delay with the help of these nested loops. As the clock runs at 12Mhz, we can
calculate how much time it takes for the loop to complete and using that time to do nothing, we have
successfully implemented a delay.
A point to note is that we need to put the initialization for the counter register (of the nested loop) in
the first loop, so the register can be re-initialized every time the first loop is re-run.
Look up tables:
Look up tables are a good way of reducing complexity for the controller to do, for example computing
the factorial of a given number on the ports. In order to do that, will have to traditionally have to find
all the numbers before that number and then multiply all the number together often in large lines of
code, but by utilizing the ROM, we can store the answers to the data on a chip and the number given
on the port will just act as an index to the right answer which can then be outputted.
This data is written in the code with the prefix of ORG and DB. ORG setting the data location being
used and DB being the set-byte.

CLOCK CYCLE:
DEFINATION:
In 8051-based system, the crystal oscillator has a frequency of 11.0592 MHz when C/T bit of TMOD is 0.
Each machine cycle is made up of 12 clock cycles. Hence for a single machine cycle, the frequency becomes
1/12 × 11.0529 MHz = 921.6 KHz

MACHINE CYCLE:
DEFINATION:
Machine cycle refers to a sequence of steps that a computer's central processing unit (CPU) goes through in
order to execute a single machine language instruction. It is also known as the instruction cycle.

Calculating Number of Machine Cycles:


To determine the number of machine cycles required for a delay, one must consider the frequency of
the 8051 microcontrollers. The frequency (f) represents the number of oscillations per second,
measured in Hertz (Hz). The formula to calculate the time period (T) is given by T = 1/f.
The 8051 microcontrollers typically require 1 to 2 machine cycles for each instruction. With this
information, the total number of machine cycles for a delay can be calculated using the formula:
Number of Machine cycles = T / instruction execution time
DELAY CALCULATION:
DEFINATION:
Generation of time delay is most important concept in embedded systems. Most of the times, we need to
generate precise time delay between two actions in any microcontroller applications. We can generate the
time delay using the techniques like LOOPs or by using in built delay functions.

How Delays Using Nested Loops Work:


Here's a breakdown of how it works:

➢ Outer Loop: This loop runs for a specific number of iterations.


➢ Middle Loop: For each iteration of the outer loop, this middle loop runs for a certain
number of iterations.
➢ Inner Loop: For each iteration of the middle loop, the inner loop runs for a specified
number of iterations. This is the loop that actually generates the delay.
Calculation with the 8051 Microcontroller at 11.0592 MHz:

1. Clock Frequency:
The clock frequency for the 8051 microcontroller is given as 11.0592 MHz
This means that the microcontroller executes approximately 11,059,200 cycles per second.
2. Total Clock Cycles:

From the previous example, we calculated the total number of clock cycles for the nested loops:
o Innermost Loop (R0 = 255):
Each DJNZ instruction takes 2 clock cycles.

Total cycles for R0=255×2=510 cycles

o Middle Loop (R1 = 200):

Total cycles for R1=200×510=102,000 cycles

o Outer Loop (R2 = 5):

Total cycles for R2=5×102,000=510,000 cycles

Calculate Total Delay in Seconds:

To convert the total clock cycles into seconds, multiply the total cycles by the time per cycle (90.5
ns):

Total Delay (in seconds) = Total Cycles × Clock Period

Substituting the values:

Total Delay=510,000×90.5×10−9 seconds

Total Delay≈0.046155 seconds≈46.16 ms

So, the total delay generated by the nested loops is approximately 46.16 ms.

Lab Tasks
Task 1
The first task was to Transfer the block of data from 20h to 30h to location 40h to 50h using loops.
Here we initialized the data ourselves between 20H to 30H, you can see the output on the memory
section on figure 2.

Task 2
The second task was to continuously scan port P0. If data is other, then 0FFh multiply it with 10 and
send it to port P1 and P2. We will need to use both a Conditional Jump (CJNE) and an unconditional
jump (SJMP). The output of this can be seen in figure 3.

Figure 2: Transfer data form 20H-30H to 40H-50H

Figure 3: Scanning P0 & Sending to P1 & P2


Task 3
The 3rd task was to write a program that searches a letter ‘g’ in a string [“hello gee”,0]. String is NULL
terminated. If g is in the string, program should exit with “g” in accumulator ‘A’ otherwise zero in
Accu A. we should copy all the contents of A to R1 at the end. The assembly code implementation of
this task is in figure 4.

Figure 4: Look for "g" in a look up table.

Task 4
Finally, the last task was to write a program to add the following numbers and send the result to P1
and P2. The data is stored at ROM addresses starting from 250H. Here in order to move the data from
the ROM we will use MOVC instruction, in combination with A (index) and DPTR (where our data
is in ROM). This task can be seen in figure 5 (with the answers highlighted.

(53, 49, 94, 56, 92, 65, 43, 83)


Figure 5: Add numbers in ROM and send it to P0, P1.

Task 5
Write a program to toggle all the bits of P1 every 150 ms (using nested loops).
Assume that the crystal frequency is 11.0592 MHz
Code:

Figure 6: toggle all the bits of P1 every 150 ms


Output:

Figure 7: output

Task 6
Write a program to generate a frequency of 4Hz on P1.1. Crystal Frequency is
11.0592MHz
CODE:

Figure 8: generate a frequency of 4Hz on P1.1


Output:

Figure 9: output

Task7
Write a program to generate a frequency of 80Hz on P1.6. Crystal frequency is
12MHz
CODE:

Figure 10: generate a frequency of 80Hz on P1.6


Output:

Figure 10: output

Proteus Simulation:

Figure 11: Proteus Simulation


Figure11: Proteus Simulation Waveform

Conclusion:
This lab clarified some of the basic concepts of loops and lookup tables and delays in assembly
language programming, especially regarding the 8051 microcontrollers. It successfully explained the
concept and usage of these instructions using examples and explained how they can be used to reduce
computational complexity.
Page 13 of 13

You might also like