0% found this document useful (0 votes)
17 views12 pages

LAB 7 Delay Calculation Using Loops

Uploaded by

tanveer1111110
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)
17 views12 pages

LAB 7 Delay Calculation Using Loops

Uploaded by

tanveer1111110
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/ 12

LAB 7

DELAY CALCULATIONS
USING LOOPS
CLOCK CYCLE VS MACHINE CYCLE
To execute instruction, CPU takes certain number of
machine cycles. In 8051, one machine cycle is equal to
12 clock cycles.

1 machine cycle = 12 Oscillator cycles


1mc(freq) = crystal freq(MHz) / 12
1mc(freq) = 11.0592 MHz / 12
1mc(freq) = 921.6 KHz
Time = 1/freq
1mc = 1.085us
 Every instruction of 8051 takes some machine
cycles to execute.
 Instructions may takes 1,2 or 4 cycles to execute
© Dr J.Iqbal
DELAY CALCULATION

Delay: mov r3,#200 ;1mc instruction


djnz r3,$ ;2mc instruction
nop ;1mc instruction
END

1
1+(2 * 200)
1+(2 * 200) +1
(1+(2 * 200) + 1)*1.085 = 436.17 µ Sec
NESTED LOOPS- DELAY
ORG 00H
MOV P0, #0 ; Initialize port 0 and accumulator
MOV A, #0
START:
INC A ; Increment A
MOV P0, A ; Send contents of A to port 0
DELAY:
MOV R0, #255 ; initialize R0 and R1 with 255
LOOP1:
MOV R1, #255
LOOP2:
DJNZ R1, LOOP2 ; decrement R1 and jump loop2 if
; not zero
DJNZ R0, LOOP1 ; decrement R0 and jump loop1 if
; not zero
JMP START
END ; Delay = ?
DELAY CALCULATION
DELAY:
MOV R0, #255 ; 1mc instruction
LOOP1:
MOV R1, #255 ; 1mc instruction
LOOP2:
DJNZ R1, LOOP2 ; 2mc instruction

DJNZ R0, LOOP1 ; 2mc instruction

1
1+1*255
1+1*255 +2*255*255
1+1*255 +2*255*255+2*255
(1+1*255 +2*255*255+2*255)*1.085 = 141,935.36 µ Sec
141,935.36 µ Sec = 141.96 mSec
NESTED LOOPS- EXAMPLE
Write a program to toggle all the bits of P1 every 200
ms (using nested loops). Assume that the crystal
frequency is 11.0592 MHz
200ms = 200 x 1000 = 200,000 µ Sec

As 1mc = 1.085 µ Sec


Therefore we need 200000/1.085 = 184,332mc

As DJNZ instruction take 2mc so we need a loop to


repeat 184332/2 = 92,166 times

As 92166 > 255 so we need nested loops


92166/255 = 361.4
As 361.4 > 255 so we need 3rd loop
361.4/2 = 180.7 ~ 180
NESTED LOOPS- EXAMPLE CONT…
Write a program to toggle all the bits of P1 every 200
ms (using nested loops). Assume that the crystal
frequency is 11.0592 MHz
ORG 0
MOV A, #55h
AGAIN:
MOV P1, A
MOV R5, #2
L1: MOV R4, #180
L2: MOV R3, #255
L3: DJNZ R3, L3
DJNZ R4, L2
DJNZ R5, L1
CPL A
SJMP AGAIN
NESTED LOOPS- EXAMPLE
Write a program to generate a square wave of 20Hz
frequency on P1.1 (using nested loops). Assume that
the crystal frequency is 11.0592 MHz

Time period = 1/freq = 1/20 = 0.05 sec


0.05s = 50 ms = 50 x 1000 = 50,000 µ Sec
On-time/ off-time = 50,000/2 = 25,000 µ Sec
As 1mc = 1.085 µ Sec
Therefore we need 25,000/1.085 = 23,041mc

As DJNZ instruction take 2mc so we need a loop to


repeat 23041/2 = 11,521 times

As 11,521 > 255 so we need nested loops


11,521 /255 = 45
NESTED LOOPS- EXAMPLE CONT…
Write a program to generate a square wave of 20Hz
frequency on P1.1 (using nested loops). Assume that
the crystal frequency is 11.0592 MHz
ORG 0
AGAIN:
CPL P1.1
MOV R5, #45
L1: MOV R4, #255
L2: DJNZ R4, L2
DJNZ R5, L1
SJMP AGAIN
END
NESTED LOOPS- EXERCISES

1. 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
2. Write a program to generate a frequency of 4Hz
on P1.1. Crystal Frequency is 11.0592MHz
3. Write a program to generate a frequency of
80Hz on P1.6. Crystal frequency is 12MHz

You might also like