7402 views|7 replies

44

Posts

0

Resources
The OP
 

About the 1ms delay program for STC89C52 microcontroller? [Copy link]

Regarding the delay program of the STC89C52 microcontroller, the crystal oscillator is 12MHz, and the machine cycle is 1us. The program is as follows:

void Delay1ms() //@12.000MHz
{
unsigned char i, j;
i = 2; //1us
j = 239; //1us
do
{
while (--j);
} while (--i);
}
Delay time calculation: 497*2+1+1+2+2=1000us=1ms
The loop body is executed 497 times, which is 497 machine cycles. I don't understand why it needs to be multiplied by 2. Please tell me in more detail.
This post is from 51mcu

Latest reply

Compile it and look at the assembly code  Details Published on 2020-8-25 03:50
 

8

Posts

0

Resources
2
 

You can generate this directly using the software on the STC official website. No need to be so troublesome.

This post is from 51mcu

Comments

This is what the software generated, but I don't quite understand it.  Details Published on 2020-8-22 20:15
 
 
 

44

Posts

0

Resources
3
 
peq200 posted on 2020-8-22 10:33 You can generate this directly using the software on the STC official website. No need to be so troublesome.

This is what the software generated, but I don't quite understand it.

This post is from 51mcu
 
 
 

48

Posts

0

Resources
4
 

Looking at the assembly, you can see that 497 is the execution cycle of while(--j), and it is multiplied by 2 because of the two loops of while(--i).

This post is from 51mcu
 
 
 

7422

Posts

2

Resources
5
 

The instruction time is fixed, and the number of instructions occupied by one cycle or two cycles is also fixed. You can find out this number by calculating it based on the frequency set by your ISP.

This post is from 51mcu
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

6062

Posts

4

Resources
6
 
This post was last edited by damiaa on 2020-8-23 22:47
do
{
     while (--j);
}while (--i);
先看
do
{
     。。。。
}while (--i);

 其次while (--j);
do
{
     。。。。
}while (--i);只是里面的内容

其实这样也很好啊:
while (i--)
{
     while (--j);
};

void DelayXms(unsigned int t) //@12.000MHz
{
    for(k=0;i<t;k++)
    {
        unsigned char i=2, j=239;
        while (i--){ while (--j); };
    }
}

This post is from 51mcu
 
 
 

4005

Posts

0

Resources
7
 

Two cycle instructions

This post is from 51mcu
 
 
 

4005

Posts

0

Resources
8
 

Compile it and look at the assembly code

This post is from 51mcu
 
 
 

Guess Your Favourite
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list