0% found this document useful (0 votes)
28 views

EEE 432 Lecture 11

A very useful lecture in electrical drive

Uploaded by

AMOS MUSONDA
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)
28 views

EEE 432 Lecture 11

A very useful lecture in electrical drive

Uploaded by

AMOS MUSONDA
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/ 15

EEE 432

EMBEDDED SYSTEMS
Assembly Language
Programming (Cont.)
Lecture 11
1 Machine Cycle for the 8051
➢ The CPU takes a certain number of clock cycles to execute an instruction.
➢ In the 8051 family, these clock cycles are referred to as machine cycles.
➢ 8051 instructions are executed in a particular number of their machine cycles. To
calculate a time delay, we use these known machine cycles.
➢ In the 8051 family, the length of the machine cycle depends on the frequency of
the crystal oscillator connected to the 8051 system. The crystal oscillator, along
with on-chip circuitry, provide the clock source for the 8051 CPU.
➢ The frequency of the crystal connected to the 8051 family can vary from 4 MHz to
30 MHz, depending on the chip rating and manufacturer.
➢ In the original 8051, one machine cycle lasts 12 oscillator periods.
➢ Therefore, to calculate the machine cycle for the 8051, we take 1/12 of the crystal
frequency, then take its inverse, as shown in Example 1.

EEE 432 - Embedded Systems (2024) 4


Example 1
The following shows crystal frequency for three different 8051-based systems. Find
the period of the machine cycle in each case.
(a) 11.0592 MHz (b) 16 MHz (c) 20 MHz
Solution
(a) 11.0592 MHz/12 = 921.6 kHz; machine cycle = 1/921.6 kHz = 1.085 µs
(b) 16 MHz/12 = 1.333 MHz; machine cycle = 1/1.333 MHz = 0.75 µs
(c) 20 MHz/12 = 1.66 MHz; machine cycle = 1/1.66 MHz = 0.60 µs

EEE 432 - Embedded Systems (2024) 5


Example 2
For an 8051 system of 11.0592 MHz, find how long it takes to execute each of the
following instructions:
(a) MOV R3,#55 (b) DEC R3 (c) DJNZ R2,target
(d) LJMP (e) SJMP (f) NOP (no operation)
(g) MUL AB
Solution
The machine cycle for a system of 11.0592 MHz is 1.085 µs.
Instruction Machine Cycle Execution Time
(a) MOV R3,#55 1 1.085 µs
(b) DEC R3 1 1.085 µs
(c) DJNZ R2,target 2 2.17 µs

EEE 432 - Embedded Systems (2024) 6


Example 2 (Cont.)
(d) LJMP 2 2.17 µs
(e) SJMP 2 2.17 µs
(f) NOP 1 1.085 µs
(g) MUL AB 4 4.34 µs

EEE 432 - Embedded Systems (2024) 7


2 Delay Calculation for the 8051
➢ A delay subroutine consists of two parts:
❖ setting a counter, and
❖ a loop.
➢ Most of the time delay is performed by the body of the loop, as shown in Example
9.

EEE 432 - Embedded Systems (2024) 8


Example 3
Find the size of the delay in the following program, if the crystal frequency is 11.0592
MHz
MOV A,#55H ;load A with 55H
AGAIN: MOV P1,A ;issue value in reg A to port 1
ACALL DELAY ;time delay
CPL A ;complement reg A
SJMP AGAIN ;keep doing this indefinitely
;-------Time delay
DELAY: MOV R3,#200 ;load R3 with 200
HERE: DJNZ R3,HERE ;stay here until R3 become 0
RET ;return to caller
EEE 432 - Embedded Systems (2024) 9
Example 3 (Cont.)
Solution
We have the following machine cycles for each instruction of the DELAY subroutine:
Machine Cycle
DELAY : MOV R3,#200 1
HERE: DJNZ R3,HERE 2
RET 2
Therefore, we have a time delay of [(200 x 2) + 1 + 2] x 1.085 µs = 437.255 µs.

EEE 432 - Embedded Systems (2024) 10


3 Loop Inside Loop Delay
➢ Very often we calculate the time delay based on the instructions inside the loop and
ignore the clock cycles associated with the instructions outside the loop.
➢ In Example 3, the largest value the R3 register can take is 255; therefore, one way
to increase the delay is to use NOP instructions in the loop.
➢ NOP, which stands for “no operation,” simply wastes time. This is shown in
Example 4.

EEE 432 - Embedded Systems (2024) 11


Example 4
For an 8051 system of 11.0592 MHz, find the time delay for the following subroutine:
Machine Cycle
DELAY : MOV R3,#250 1

HERE: NOP 1
NOP 1
NOP 1
NOP 1
DJNZ R3,HERE 2

RET 2
EEE 432 - Embedded Systems (2024) 12
Example 4
Solution
➢ The time delay inside the HERE loop is
[250(1 + 1 + 1+ 1 + 2)] x 1.085 µs = 1500 x 1.085 µs = 1627.5 µs.
➢ Adding the two instructions outside the loop we have
1627.5 µs + 3 x 1.085 µs = 1630.755 µs
NOTE: If machine cycle timing is critical to your system design, make sure that you
check the manufacturer’s data sheets for the device specification. For example, the
DS89C420 has 3 machine cycles instead of 2 machine cycles for the RET instruction.

EEE 432 - Embedded Systems (2024) 13


Example 5
For a machine cycle of 1.085 µs, find the time delay in the following subroutine.
Machine Cycle
DELAY :
MOV R2,#200 1
AGAIN: MOV R3,#250 1
HERE: NOP 1
NOP 1
DJNZ R3,HERE 2
DJNZ R2,AGAIN 2
RET 2

EEE 432 - Embedded Systems (2024) 14


Example 5
Solution
➢ For the HERE loop, we have (4 x 250) xl.085 µs = 1085 µs.
➢ The AGAIN loop repeats the HERE loop 200 times; therefore, we have 200 × 1085
µs = 217000, if we do not include the overhead.
➢ However, the instructions "MOV R3, #250" and "DJNZ R2 ,AGAIN" at the
beginning and end of the AGAJN loop add (3 × 200 × 1.085 µs) = 651 µs to the
time delay. As a result we have 217000 + 651 = 217651 µs = 217.651 milliseconds
for total time delay associated with the above DELAY subroutine.
➢ Notice that in the case of a nested loop, as in all other time delay loops, the time is
approximate since we have ignored the first and last instructions in the subroutine.
➢ The exact time is 217651 + 3 × 1.085 = 217654.255 µs.

EEE 432 - Embedded Systems (2024) 15

You might also like