Write a subroutine that provides a delay of exactly one mill

Write a subroutine that provides a delay of exactly one millisecond (ms) using a crystal speed of 24 MHz. Make sure that all used registers keep their original values that they had before the call.

Solution

In an 8051 microcontroller, it requires 12 cycles of the processor clock for executing a single instruction cycle. For an 8051 microcontroller clocked by a 24MHz crystal, the time taken for executing one instruction cycle is 1µS and it is according to the equation, Time for 1 instruction cycle= 12 /24MHz = 0.5µS. The shortest instructions will execute in 0.5 µS and other instructions will take 1 or more micro seconds depending up on the size of the instruction. Thus a time delay of any magnitude can be generated by looping suitable instructions a required number of time. Any way, keep one thing in mind that software delay is not very accurate because we cannot exactly predict how much time its takes for executing a single instruction. Generally an instruction will be executed in the theoretical amount of time but some times it may advance or retard due to other reasons. Therefore it is better to use 8051 Timer for generating delay in time critical applications. How ever software delay routines are very easy to develop and well enough for less critical and simple applications.

Program to delay 1 ms

The above program roughly produces a delay of 1mS. The instruction DJNZ Rx,LABEL is a two cycle instruction and it will take 1µS to execute. So repeating this instruction 1000 times will generate a delay of 1000 x 1µS = 1mS.

 Write a subroutine that provides a delay of exactly one millisecond (ms) using a crystal speed of 24 MHz. Make sure that all used registers keep their original

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site