51 Single chip microcomputer delay program

Source: Internet
Author: User

  The delay program is widely used in SCM programming, but some readers do not know how to program the time delay programs in the study, and do not know the machine

The difference between the period and the instruction period, do not know the usage of the delay program instruction, this article from the time delay procedure basic concept, the machine cycle and the reference

To give a detailed answer to the reader in the form of a graphic method, such as the difference and connection of the cycle and the use of relevant instructions.

We know that program design is the most important work of single-chip microcomputer development, and the program often needs to complete the function of delay in the process of execution. For example

In the Traffic light control program, the need for controlling the red light duration lasts 30 seconds, it can be completed by the delay program. How is the delay program

Implementation of it. Let's take a look at some of the relevant concepts first.

One, machine cycle and instruction cycle

1. Machine cycle refers to a single-chip microcomputer to complete a basic operation of the time spent, generally using microseconds to measure the speed of single-chip microcomputer,

51 single-Chip machine cycle includes 12 clock oscillation cycle, that is, if the 51 microcontroller using 12MHz crystal, then the implementation

A machine cycle requires only 1μs, and if you are using a 6MHz crystal, it takes 2μs to perform a machine cycle.

2. Instruction cycle refers to the time required for a single-chip microcomputer to execute an instruction, generally using a single-chip machine cycle to measure the instruction cycle.

There is a single-cycle instruction in the 51 microcontroller (only one machine cycle is required to execute this instruction), and the dual-cycle instruction (which requires only two

Machine cycle), four-cycle instruction (requires four machine cycles to execute this instruction). In addition to the multiply, except two instructions are four-cycle instructions, the rest are

is a single-cycle or dual-cycle instruction. That is, if the 51 microcontroller uses 12MHz crystal oscillator, then it executes an instruction generally only need

A microsecond of time; If a 6MH oscillator is used, executing an instruction typically takes up to three microseconds.

Now there are many types of MCU, but in each model of the microcontroller device manual will be detailed instructions for the implementation of the various instructions required by the machine

The above concepts can be completed according to the instruction execution cycle in the microcontroller device manual and the crystal Oscillator frequency of the microcontroller.

A delay program that requires a precise delay time.

Second, delay instruction

In the microcontroller programming there is no real delay instruction, from the concept of the above we know that the single-chip microcomputer each execution of an instruction requires a

Time, so to achieve the effect of delay, only need to let the single-chip microcomputer continuously carry out the instructions without specific practical significance, thus reaching the delay

The effect.

1. Data transfer Instructions MOV

The function of data transfer instruction is to copy and copy data from one place to another.

such as: MOV R7, #80H; send data 80H to register R7, then register R7 inside Store 80H, alone this article

Directive does not have any practical significance, and executing the instruction requires a machine cycle.

2. Null operation instruction NOP

The null operation instruction function only lets the monolithic computer perform the meaningless operation, consumes one machine cycle.

3. Cyclic transfer instruction DJNZ

The function of the cyclic transfer instruction is to subtract 1 from the first number and determine whether it is 0, not 0 to the specified place, and 0 to go down.

such as: DJNZ r7,kk; Reduce the contents of the register R7 by 1 and determine if the contents of the register R7 are 0 after 1 is lost, if

No 0 is transferred to the location labeled KK, and if 0 executes the next instruction. This command requires 2 machine cycles.

Using the combination of the above three instructions, you can write the required delay program more precisely.

Three, 1 seconds delay subroutine, flow chart and time calculation (with single chip microcomputer crystal oscillator 12MHz For example, 1 machine cycle need 1μs)

Understand the above content, now let's take a look at

Total time required for the program: 1+10+2560+330240+660480+5120+20+2=998433μs≈1s

Here to run this program altogether need 998433μs, still poor 1567μs to reach 1S, so want to achieve the perfect 1S delay, need

To add some instructions before returning to the command ret, let it finish the 1567μs delay. Interested readers can try to add their own to complete.

Finally, add that the program is generally programmed to write a delay program as a separate subroutine, and so-called subroutine is a function to implement a

of small modules. In this way, it is convenient to repeatedly invoke the programmed delay subroutine in the main program.

Tip: Circular transfer instruction (DJNZ) in addition to the given address designator to jump outside, you can also change the address label to $, so

The program jumps back to this instruction execution. For example:

DJNZ r7,$; R7 content minus 1 is 0, then executes this instruction again; 0 is executed down, when the value of R7 is changed to 10

, the time required to complete the program is 2*10=20μs.

51 single-chip microcomputer assembly delay program algorithm detailed

In this paper, 12MHZ crystal oscillator is used as an example to explain in detail the precise algorithm of assembler delay in MCS-51 MCU.

instruction cycle, machine cycle, and clock cycle

Instruction Cycle: The time required for the CPU to execute an instruction is called the instruction cycle, which is based on the machine cycle, with different instructions and different machine cycles required.

Clock cycle: Also known as oscillation period, a clock period = the reciprocal of the crystal oscillator.

MCS-51 single-chip machine cycle = 6 status cycles = 12 clock cycles.

MCS-51 MCU instructions are single-byte, double-byte and three-byte, their instruction cycle is not the same, a single-cycle instruction contains a machine cycle, that is, 12 clock cycles, so a single-cycle instruction is executed for a time of 12* (1/12000000) =1μs.

Program Analysis

Example 1 50ms Delay subroutine:

Del:mov R7, #200 ①

Del1:mov R6, #125 ②

DEL2:DJNZ R6,del2③

DJNZ R7,del1④


Precise delay Time: 1+ (1*200) + (2*125*200) + (2*200) +2

= (2*125+3) *200+3⑥



The formula is arranged by ⑥ (only above) delay time = (+3) * Outer cycle +3⑦

Description: Del This subroutine has five instructions, and now analyzes the number of times each instruction is executed and the elapsed time.

The first sentence: MOV R7, #200 is executed only once in the entire subroutine, and is a single-cycle instruction, so it takes time 1μs

The second sentence: MOV R6, #125 from ② see ④ as long as r7-1 not 0, will return to this sentence, a total of R7 times, a total time-consuming 200μs

The third sentence: DJNZ R6,del2 as long as r6-1 not 0, repeated execution of this sentence (inner loop R6 times), but also by the external loop R7 control, so a total of r6*r7 times, because it is a double-cycle instruction, so time-consuming 2*r6*r7μs.

Example 2 1-second delay subroutine:

Del:mov R7, #10 ①

Del1:mov R6, #200 ②

Del2:mov R5, #248 ③

DJNZ R5,$④

DJNZ R6,del2⑤

DJNZ R7,del1⑥


The exact delay time is calculated for each instruction:

1+ (1*10) + (1*200*10) + (2*248*200*10) + (2*200*10) + (2*10) +2

=[(2*248+3) *200+3]*10+3⑧


Organized by ⑧: Delay time =[(first layer cycle +3) * Second layer loop +3]* third layer loop +3⑨

This formula is applicable to the procedure within the three-layer cycle, and also verifies the establishment of Example 1 Chinese ⑦ (the third-tier cycle equivalent to 1).

Note that to achieve a longer time delay, the general use of multiple loops, sometimes in the program to add NOP instructions, then the formula ⑨ no longer applicable, the following example analysis.

Example 3 still takes 1 second delay as an example

Del:mov R7, #10 1 instruction Cycle 1

Del1:mov R6, #0FFH 1 instruction Cycle 10

Del2:mov R5, #80H 1 instruction Cycle 255*10=2550

Kong:nop 1 Instruction Cycle 128*255*10=326400

DJNZ r5,$ 2 instruction Cycle 2*128*255*10=652800

DJNZ r6,del2 2 instruction Cycle 2*255*10=5110

DJNZ r7,del1 2 instruction Cycle 2*10=20


Delay Time =1+10+2550+326400+652800+5110+20+2 =986893μs approx. 1s

Finishing: Delay time =[(first layer cycle +3) * Second layer cycle +3]* the third layer loop +3⑩

Conclusion: According to the confusion of beginners, the time-delay algorithm of assembler is explained in detail, and the corresponding formulas are summed up in several different ways, so long as we read the detailed explanation of example 1 carefully and use example 2 and example 3 to deepen understanding, we will grasp the algorithms of various types of programs and apply them.

Single Chip microcomputer Delay subroutine

1) Delay: 20ms Crystal oscillator 12M

1+ (1+2*248+2) *4+1+1+1=20000us=20ms

With the assembly. The advantage is precision ...

The disadvantage is that it's a little complicated.


MOV R7, #4


MOV R6, #248

DJNZ r6,$





2) Some soft-delay subroutines obtained by calculating the 51 assembly instructions


; Delay 10uS


Time10us:mov R5, #05h; 11us

DJNZ r5,$



; Delay 50uS


Time50us:mov R5, #19h; 51us

DJNZ r5,$



; Delay 100uS


Time100us:mov R5, #31h; 99.6us

DJNZ r5,$



; Delay 200uS


Time200us:mov R5, #64h; 201us

DJNZ r5,$



; Delay 250uS


Time250us:mov R5, #7ch; 249.6us

DJNZ r5,$



; Delay 350uS


Time350us:mov R5, #0afh; 351us

TIME350US_1:DJNZ R5,time350us_1



; Delay 500uS


Time500us:mov R5, #0fah; 501us

TIME500US_1:DJNZ R5,time500us_1



; Delay 1mS


Time1ms:mov R5, #0fah; 1001us



DJNZ r5,time1ms_1



; Delay 2.5mS


Time2_5ms:mov R5, #05h; 2.496ms

Time2_5ms_1:mov R6, #0f8h; 497us

DJNZ r6,$

DJNZ r5,time2_5ms_1



; Delay 10mS


Time10ms:mov R5, #14h; 10.262ms

Time10ms_1:mov R6, #0ffh; 511us

DJNZ r6,$

DJNZ r5,time10ms_1



; Delay 50mS


Time50ms:mov R5, #63h; 49.996ms

Time50ms_1:mov R6, #0fbh; 503us

DJNZ r6,$

DJNZ r5,time50ms_1



; Delay 100mS


Time100ms:mov R5, #0c3h; 100.036ms

Time100ms_1:mov R6, #0ffh; 511us

DJNZ r6,$

DJNZ r5,time100ms_1



; Delay 200mS


Time200ms:mov R5, #02h; 250.351ms

Time200ms_1:mov R6, #0f4h; 125.173ms

Time200ms_2:mov R7, #0ffh; 511us

DJNZ r7,$

DJNZ r6,time200ms_2

DJNZ r5,time200ms_1



; Delay 500mS


Time500ms:mov R5, #04h; 500.701ms

Time500ms_1:mov R6, #0f4h; 125.173ms

Time500ms_2:mov R7, #0ffh; 511us

DJNZ r7,$

DJNZ r6,time500ms_2

DJNZ r5,time500ms_1



; Delay 1S


Time1s:mov R5, #08h; 1001.401ms

Time1s_1:mov R6, #0f4h; 125.173ms

Time1s_2:mov R7, #0ffh; 511us

DJNZ r7,$

DJNZ r6,time1s_2

DJNZ r5,time1s_1


The 12M oscillator machine cycle is 1US NOP for single cycle instruction djnz for dual cycle instruction.


;; Crystal oscillator 12MHZ, delay 1 seconds

Delay:mov 72H, #100

Loop3:mov 71H, #100

Loop1:mov 70H, #47

LOOP0:DJNZ 70h,loop0



MOV 70H, #46




MOV 70H, #48


4) Delay 1 minutes subroutine, F=6mhz

, the program has been measured, the delay time 60,000,000.0US

Delay60s:mov R3, #228

mov R2, #253

mov r1, #219






5) The computer repeatedly executes a program to achieve the purpose of delay is called software delay, microcontroller program often requires a short time delay, but a considerable part of the delay program is very vague, the algorithm of delay program is not enough to understand, here I take 12MHz crystal oscillator and two classical delay subroutine for example, Detailed analysis of single-chip microcomputer assembly delay program.

What are clock cycles, machine cycles, and instruction cycles.

Clock cycle: That is, the oscillation period, with 12MHz clock pulse for example, that clock cycle is (1/12000000) s= (1/12) US;

Machine cycle: one machine cycle = 6 state cycles = 12 clock cycles =1us;

Instruction Cycle: The time required for the CPU to execute an instruction is called the instruction cycle, and the instruction period is in machine cycle, and the machine cycles required by different instructions are not necessarily the same, so refer to the 51 single-chip microcomputer command speed check table.

From the upper available: The CPU executes a single-cycle instruction, requires 1us, the execution of a dual-cycle instruction requires 2us.

The following is a specific delay subroutine analysis:

0.1s Delay subroutine (12MHz crystal oscillator):

MOV R7, #200; single-cycle instruction (1US)

D1:mov R6, #250; single-cycle instruction (1US)

DJNZ r6,$; Double cycle Instruction (2us)//The instruction itself executes R6 times

DJNZ r7,d1; dual-cycle Instruction (2us)//d1 perform R7 times

RET; dual-cycle instruction (2us)

t=1+ (1+2*r6+2) *r7+2



0.5s Delay subroutine (12MHz crystal oscillator):

MOV R7, #5; single-cycle instruction (1US)

D1:mov R6, #200; single-cycle instruction (1US)

D2:mov R5, #250; single-cycle instruction (1us

DJNZ r5,$; Double cycle Instruction (2us)//The instruction itself executes R5 times

DJNZ r6,d2; dual-cycle Instruction (2us)//d2 perform R6 times

DJNZ r7,d1; dual-cycle Instruction (2us)//d1 perform R7 times

RET; dual-cycle instruction (2us)

t=1+[1+ (1+2*r5+2) *r6+2]*r7+2



6) 51 single-chip computer classic running light program, in 51 single-chip computer P2 mouth connected with 8 light-emitting diodes, resulting in the movement of running lights.

ORG 0; program starts from 0 address

Start:mov A, #0FEH; let ACC content be 11111110

Loop:mov p2,a; Let the P2 port output ACC content

RR A; Move the contents of ACC to the left

Call delay; invoke Delay subroutine

LJMP loop; jump to loop execution

; 0.1-second delay subroutine (12MHz crystal oscillator) ===================

Delay:mov R7, #200; R7 Register load 200 number of times

D1:mov R6, #250; R6 Register load 250 number of times

DJNZ r6,$; The bank executes R6 times

DJNZ r7,d1;D 1 loop execution R7 times

RET; return to Main program

End Program


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: