The program code of the microcontroller to assemble the delay: DELAY: MOV R7, #250; D1: MOV R6, #250; D2: DJNZ R6, D2; DJNZ R7, D1; RET If you use a high-level language to program, you only need to call the delay function to achieve it, but how does the computer implement it? If you want to know why, you have to start from the compilation. The "DELAY", "D1", and "D2" before the colon are the names of the statement lines, which are used for the conditional statement jump of the program. After the semicolon is a comment, this information will be filtered out when the computer is executed to minimize the code Length improves efficiency. DELAY: MOV R7, #250; The sentence named "DELAY": It means to fill in the R7 position of the internal memory RAM of the CPU as 250 (the original value is 0, why is it 0? Because any program must be reset before starting execution, just Just like we plan to reset the counter, or we use the sand table to write, it is similar to smooth the sand table) D1: MOV R6, #250; sentence with the name "D1": fill in the position of R6 as 250 D2: DJNZ R6, D2; The sentence named "D2": subtract 1 from 250 in R6 position, if it is 0, continue to execute the next one, if it is not 0, continue to execute the sentence D2, because R6=250, so this sentence Do it 250 times in place! DJNZ R7, D1; This sentence has no name, because there is no other sentence to jump to here, so it is omitted. R7 is also equal to 250, but instead of stepping in place, it jumps back to D1. Do this, D!, D2 and this sentence will be executed 250 times in a loop. What needs to be emphasized is: D2 statement itself has to be executed 250 each time It is executed 250*250=62500 times! RET; The end of the subroutine (because the delay program generally does not exist as an independent program, it is just a subroutine, which is a function in high-level language. Seeing this character, the subroutine will jump back to the parent program and proceed to the next step) . What's the point of this subroutine looping instructions so repeatedly? How is the delay program implemented? Speaking of the computer is really a bit stupid, it is * count the number of times the program is executed to accumulate time! In other words, the sentence itself is a waste of time! Haha, ridiculous, right? It's like you don't have a clock, but you know that it takes one minute for you to lap the playground, and then you ran around the playground 60 laps, exactly one hour later. If it were a human being, no one would be stupid enough to do such a tiring task to time the clock, but the computer is not the same. Whether it is stationary or in motion, the program always runs one beat after another, so it is not tired. So, how much time does the computer take to execute a statement? Take the 51-type single-chip microcomputer as an example: if a 12MHz crystal oscillator is used, one machine cycle is 1 microsecond, so why don't it matter for the time being. The instruction system of the single-chip microcomputer is divided into single-cycle, double-cycle and three-cycle instructions (three cycles actually occupy four cycles, and one more cycle is wasted). This is determined by the content of the instruction. A single cycle with a lot of content cannot be executed. It must be extended. The MOV in the subroutine is a single cycle, and DJNZ is a double cycle. In this way, we can calculate how many cycles this subroutine has carried out, and then multiply it by 1 microsecond to calculate the time it takes, that is, the length of the delay! The first sentence: DELAY: MOV R7, #250; Executed once, without any statement jumping to it, single cycle. 1 The second sentence: D1: MOV R6, #250; executed 250 times, all of which are skipped from the fourth sentence, single cycle. 250 The third sentence: D2: DJNZ R6, D2; executed 250 times in situ, postpone from the second sentence (after the second sentence is executed, the third sentence must be executed if there is no other jump) over 250 times, which is 250* 250=62500 times, double cycle*2. 125000 Fourth sentence: DJNZ R7, D1; postpone from the third sentence 250 times, double cycle*2.500 Total: 1+250+125000+500=1257501 times, multiplied by 1 microsecond, the conversion is about 0.13 seconds. We can adjust the length of the delay by adjusting the values ​​of R6 and R7. In fact, in an 8-bit computer, the address of a working register such as R6 can only hold a decimal value of 255, so it is impossible to achieve a longer delay through the above program. then what should we do? We can add another R4, so that the above program can loop up to 255 times. In this way, 200 times of 0.13 seconds is 30 seconds up and down. If it is not enough, increase the loop and expand it by more than 200 times. Of course, the number of working registers It is limited, so this delay program is also limited. If it is not enough, what should I do? As far as I have learned, I can only set up loops in the parent program, that is, let this subroutine continue in the parent program. cycle. gaming headset gaming headset Shenzhen Linx Technology Co., Ltd. , https://www.linxheadphone.com