SECTION - III TIME DELAY CALCULATION IN 8051

SECTION - III TIME DELAY CALCULATION IN 8051:

What is Machine Cycle in 8051 microcontrollers?:

The CPU takes a certain number of clock cycles to execute an instruction. These clock cycles are referred to as machine cycles. The length of the machine cycle depends on the frequency of the crystal oscillator connected to the 8051 system. The crystal oscillator, along with the on-chip circuitry, provide the clock source for the 8051 CPU. The crystal oscillator frequency can vary from 4MHz to 30MHz. To make the 8051 system compatible with the serial port of the personal computer PC, 11.0592MHz crystal oscillators is used.

In the 8051, one machine cycle lasts 12 oscillator periods. So to calculate the machine cycle, we take 1/12 of the crystal frequency, then take the inverse of it results in time period. i.e frequency = 1/time period.

Example # 1:

Lets find the time period of the machine cycle in each case for the following crystal frequency of different 8051 based systems: 11.0592 MHz, 16 MHz, 20 MHz

Answer:

11.0592 MHz:

11.0592/12 = 921.6 KHz

Machine cycle = 1/921.6 KHz = 1.085us [us=microsecond]

16 MHz:

16MHz/12 = 1.333 MHz

Machine cycle = 1/1.333 MHz = 0.75us [us=microsecond]

20MHz:

20MHz/12 = 1.66 MHz

Machine Cycle = 1/1.66 MHz = 0.60us [us=microsecond]

Example # 2:

Lets find how long it takes to execute each of the following instructions, for a crystal frequency of 11.0592 MHz. The machine cycle of a system of 11.0592 MHz is 1.085 us.

INSTRUCTIONMACHINE CYCLETIME TO EXECUTE
MOV R2,#55H11x1.085 us = 1.085 us
DEC R211x1.085 us = 1.085 us
DJNZ R2,target22x1.085 us = 2.17 us
LJMP22x1.085 us = 2.17 us
SJMP22x1.085 us = 2.17 us
NOP11x1.085 us = 1.085 us
MUL AB44x1.085 us = 4.34 us

 

How to Calculate Exact Time Delay in 8051 microcontroller?

The delay subroutine consists of 2 parts: Setting a counter and creating a loop.

Example # 3:

Lets find the size of the delay if the crystal frequency of 11.0592 MHz is connected.

MOV A,#55H
AGAIN: MOV P1,A
ACALL DELAY
CPL A
SJMP AGAIN
;-----------------Time Delay
DELAY: MOV R3,#225
HERE: DJNZ R3,HERE
RET

Answer:

We have the following machine cycles for each instruction of the DELAY subroutine.

DELAY: MOV R2,#255Machine Cycle = 1
HERE: DJNZ R2,HEREMachine Cycle = 2
RETMachine Cycle = 1

Therefore, we have a time delay of [(255 x 2) + 1 + 1] x 1.085 us = 555.52 us

Very often we used to calculate the time delay based on the instructions inside the loop and ignore the clock cycles associated with the instructions outside the loop.

NOP Instruction:

NOP instruction is used to increase the delay in the loop. NOP means "No Operation" simply wastes time.

Loop Inside a Loop Delay:

This method is used to get a large delay i.e. is used to loop inside a loop, which is also called a nested loop.

Example # 4:

Lets find the time delay for the following subroutine with 11.0592 MHz crystal frequency is connected to the 8051 system.

DELAY: MOV R2,#255Machine Cycle = 1
HERE: NOPMachine Cycle = 1
NOPMachine Cycle = 1
NOPMachine Cycle = 1
NOPMachine Cycle = 1
DJNZ R2,HEREMachine Cycle = 2
RET1
  • The time delay inside the HERE loop is [255(1+1+1+1+2)] x 1.085 us = 1660.05 us
  • The time delay of the two instructions outside the loop is: [1660.05 us+1+1)]x1.085 us = 1803.32425 us

Example # 5:

For a crystal frequency of 11.0592 MHz, lets find the time delay in the following subroutine. The machine cycle is 1.085 us.

Answer:

DELAY: MOV R2,#200Machine Cycle = 1
AGAIN: MOV R3,#250Machine Cycle = 1
HERE: NOPMachine Cycle = 1
NOPMachine Cycle = 1
DJNZ R3,HEREMachine Cycle = 2
DJNZ R2,AGAINMachine Cycle = 2
RETMachine Cycle = 1

'HERE' Loop Calculations: 1+1+2, so [(1+1+2)x250] x 1.085 us = 1085 us.

'AGAIN' Loop Calculations: In this loop "MOV R3,#250" and "DJNZ R2,AGAIN" at the begining and end of the AGAIN loop add [(1+2)x200]x1.085 us = 651us to the time delay. The AGAIN loop repeats the HERE loop 200 times so 200x1085 us = 217000 us. As a result the total time delay will be  217000 us + 651 us = 217651 us or 217.651 milliseconds. The time is approximate as we have ignored the first and the last instructions in the subroutine i.e. DELAY: "MOV R2,#200" and "RET".

More From Iamtechnical.com

Advertisement: