Delay calculation of 51 single chip microcomputer

Publisher:数字冒险Latest update time:2020-06-16 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Preface

I have been using 51, STC and other MCUs for control for several years. I have always used ready-made programs and modified them to achieve the required action. I never seek to understand them in depth. The idea is that I can use them, and what is the point of knowing so much?


I am working on a project that uses I2C communication. I just copied and pasted it, but I was interested in a small delay function. Since I have only 5 combat power in basic skills, I wrote and drew for a day before I got a general understanding.


I used to read other bloggers' articles and lurked quietly. However, this time, I suddenly couldn't help but want to write an article to publish my insignificant experience that I had put so much thought into.


text

void Delay10us() //@12.000MHz

{

unsigned char i;


_nop_();

_nop_();

i = 27;

while (--i);

}


The above code is given by the software delay calculator in the STC-ISP software, using the 8051 instruction set STC-Y5 and a delay of 10us.


I used it directly like this before, but today I suddenly wanted to figure out why the code should be written like this.


So I checked information from all sides.

Starting from the source of microcontroller timing, it consists of the following parts in sequence.


First is the clock cycle algorithm: clock cycle (T) = 1 (second) / crystal oscillator frequency.

(For example: the clock cycle of the above code is 1/12M (seconds)).

This is the basic time unit of the microcontroller. It is generated by the crystal oscillator and is also called the oscillation period.


The second is the machine cycle: The machine cycle is composed of clock cycles, and the machine cycle is the time required for the microcontroller to complete a basic operation.


Regarding the machine cycle, each microcontroller may be different. I have only used the traditional 51 and STC. Let's compare them.


1 Traditional 8051 microcontroller:

One of its machine cycles consists of 12 clock cycles.

Taking a 12M crystal oscillator as an example, one machine cycle is: 12 (clock cycles) * 1 (second) / 12MHz = 1 (us)


2 STC microcontroller:

Take the STC12C5A60S2 microcontroller that I often use. It has two modes to choose from.

One is 1T mode, in which one clock cycle of the STC microcontroller is one machine cycle;

The other is the 12T mode. In this mode, the STC chip is the same as the traditional 8051 microcontroller, and 12 clock cycles constitute one machine cycle.


It can be seen that the speed of 1T mode is 12 times that of 12T mode.


Taking a 12M crystal oscillator as an example, the machine cycle can be calculated in 1T mode as:

1 (clock cycle) * 1 (second) / 12Mhz = 1/12(us)


Finally, there is the instruction cycle: this is the time required for the microcontroller to execute an instruction, which is composed of machine cycles.

Now we can go back to the code at the beginning of the article. How is this 10us function derived?


I have looked up a lot of information before, such as how many machine cycles are needed to execute a while statement and how many cycles are needed to assign a value. This took up a lot of my time. It was not until I finally transferred the delay function above directly into the main function for debugging that I realized that the problem was actually very simple.


No matter what statement is executed, it will eventually return to the assembly. By single-stepping in debug, all instruction cycles will be clear.


I use the main function to directly call the delay function, as follows:


void Delay10us() //@12.000MHz

{

unsigned char i;


_nop_();

_nop_();

i = 27;

while (--i);

}

main

{

    Delay10us();

}


I use the keil software. After building the above, click debug to start debugging.

insert image description here

Look at the picture, start debugging, the program starts at C:0x0183 020171 LJMP Delay10us(C:0171),

There is a long transfer instruction LJMP here, which will transfer to line C:0171 to execute the Delay10us function.


How long does it take to execute the LJMP instruction? Looking up the STC data sheet, in 1T mode, this instruction takes 4 clock cycles to run on the microcontroller.


Next, press the F11 key for single-step debugging, as shown below:

insert image description here

The program successfully transferred to line C:0171 and jumped to the Delay10us function. This line of program executed the NOP instruction, which is a no-op. According to the STC data sheet, the NOP instruction takes 1 clock cycle.


Next, line C:0172 is still a NOP instruction, 1 clock cycle.


Next, line C:0173 executes MOV R7,#0x1B to put the immediate value into the register. This is to assign 27 to i. Still looking up the manual, this instruction takes 2 clock cycles.


continue:

insert image description here

At this time, the while statement is executed. The instruction executed here is DJNZ R7,C:0175, register minus 1 non-zero transfer. This instruction is executed once for 4 clock cycles.


The register has been filled with 27 above, so this instruction will be executed 27 times.

continue:

insert image description here

After 27 cycles, the number finally reaches 0, and the program continues to execute. This line of instruction RET returns the subroutine. This instruction takes 4 clock cycles.


continue:

insert image description here

The process is back to square one.

OK, now we can calculate the delay time: 1 LJMP, 4 clocks; 2 NOPs, 2 clocks; 1 MOV, 2 clocks; 27 DJNZs, 108 clocks; 1 RET, 4 clocks.


4+2+2+108+4=120.


The clock cycle of the microcontroller is: 1 (S) / 12MHz = 1/12 (us)

The delay time is: 120 × 1/12 (us) = 10 (us)


Summarize

In fact, there is no absolutely accurate delay. The above is just an idealized state. The interruption of the microcontroller or other events may affect the delay.

Reference address:Delay calculation of 51 single chip microcomputer

Previous article:How to define and use the delay function of 51 single-chip C language
Next article:Design of accurate delay of 51 single chip microcomputer

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号