51 single-chip assembly delay program algorithm detailed

Taking the 12MHZ crystal oscillator as an example, the precise algorithm of the assembler delay in the MCS-51 microcontroller will be explained in detail.

Instruction cycle, machine cycle and clock cycle

Instruction cycle: The time required for the CPU to execute an instruction is called the instruction cycle. It is in machine cycle units. The instructions are different and the required machine cycles are different.

Clock cycle: Also known as the oscillation cycle, one clock cycle = the reciprocal of the crystal.

One machine cycle of the MCS-51 microcontroller = 6 state cycles = 12 clock cycles.

The instructions of the MCS-51 MCU are single-byte, double-byte and three-byte. Their instruction cycles are different. A single-cycle instruction contains one machine cycle, that is, 12 clock cycles, so a single-cycle instruction is executed. The time taken is 12*(1/12000000)=1μs. Extended reading: What are the delay methods of the MCU?

Program analysis

Example 1 50ms delay subroutine:

DEL: MOV R7, #200 1

DEL1: MOV R6, #125 2

DEL2: DJNZ R6, DEL2 3

DJNZ R7, DEL1 4

RET 5

The exact delay time is: 1+(1*200)+(2*125*200)+(2*200)+2

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

=50603μs

≈50ms

Formulated by 6 (only for the above) Delay time = (2 * inner loop + 3) * outer loop + 3 7

Detailed explanation: The DEL subroutine has five instructions, and now analyzes the number of times each instruction is executed and the time it takes.

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

The second sentence: MOV R6, #125 See 2 from 2 as long as R7-1 is not 0, it will return to this sentence, a total of R7 times, a total of 200μs

The third sentence: DJNZ R6, DEL2 As long as R6-1 is not 0, this sentence is executed repeatedly (internal loop R6 times), and is controlled by the outer loop R7, so R6*R7 times are executed, because it is a two-cycle instruction, so It takes 2*R6*R7μs.

Example 2 1 second delay subroutine:

DEL: MOV R7, #10 1

DEL1: MOV R6, #200 2

DEL2: MOV R5, #248 3

DJNZ R5, $ 4

DJNZ R6, DEL2 5

DJNZ R7, DEL1 6

RET 7

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 8

=998033μs≈1s

Finished by 8: delay time = [(2 * first layer loop + 3) * second layer loop + 3] * third layer loop + 3 9

This formula applies to the program within the three-layer loop, and also confirms the establishment of Equation 7 (the third layer loop is equivalent to 1) in Example 1.

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

Example 3 still takes a 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

RET 2

Delay time=1+10+2550+326400+652800+5110+20+2=986893μs is about 1s

Finished: delay time = [(3 * first layer loop + 3) * second layer loop + 3] * third layer loop + 3 10

Conclusion: For the confusion of beginners, the delay algorithm of the assembler is explained step by step, and the corresponding calculation formulas are summarized separately for several different writing methods. Just read the detailed explanation in Example 1 and use case 2 and example 3. To deepen understanding, we will master the algorithms of various types of programs and apply them.

Outdoor Optical Fiber Cable

Outdoor Fiber Cable,Outdoor Fiber,2 Core Outdoor Fiber Optic Cable,2 Core Outdoor Cable

Zhejiang Wanma Tianyi Communication Wire & Cable Co., Ltd. , https://www.zjwmty.com