51 MCU C language delay function

Publisher:HuanleLatest update time:2013-01-30 Source: 21IC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Different types of variables can be used in C programs for delay design. Experimental tests show that using unsigned char type has more optimized code than unsigned int. Unsigned char should be used as a delay variable when used. Take a microcontroller with a crystal oscillator of 12MHz as an example. The crystal oscillator is 12MHz, which means that one machine cycle is 1us.

1. 500ms delay subroutine

program:

void delay500ms(void)

{

unsigned char i,j,k;

for(i=15;i>0;i--)

for(j=202;j>0;j--)

for(k=81;k>0;k--);

}

Computational analysis:

The program has three layers of loops

One layer of loop n: R5*2 = 81*2 = 162us DJNZ 2us

Second layer loop m: R6*(n+3) = 202*165 = 33330us DJNZ 2us + R5 assignment 1us = 3us

Three-layer loop: R7*(m+3) = 15*33333 = 499995us DJNZ 2us + R6 assignment 1us = 3us

Outside the loop: 5us subroutine call 2us + subroutine return 2us + R7 assignment 1us = 5us

Total delay time = three-layer loop + outside loop = 499995+5 = 500000us = 500ms

Calculation formula: Delay time = [(2*R5+3)*R6+3]*R7+5

2. 200ms delay subroutine

program:

void delay200ms(void)

{

unsigned char i,j,k;

for(i=5;i>0;i--)

for(j=132;j>0;j--)

for(k=150;k>0;k--);

}

3. 10ms delay subroutine

program:

void delay10ms(void)

{

unsigned char i,j,k;

for(i=5;i>0;i--)

for(j=4;j>0;j--)

for(k=248;k>0;k--);

}[page]

4. 1s delay subroutine

program:

void delay1s(void)

{

unsigned char h,i,j,k;

for(h=5;h>0;h--)

for(i=4;i>0;i--)

for(j=116;j>0;j--)

for(k=214;k>0;k--);

}

Regarding the precise delay of the microcontroller C language, many functions on the Internet only give an approximate delay value without an exact value, and thus fail to meet the high precision requirements. This function overcomes the above shortcomings and can accurately count the delay value to within 1us. The CPU used in this example is the STC12C5412 series 12x speed microcontroller. It can be used in other series of microcontrollers as long as the parameter value is modified, and has a wide range of applications.

There are three delay functions as follows:

Function calls are divided into two levels: the first level is a delay of less than 10US, and the second level is a delay of more than 10US.

//===================Less than 10US【use 1US level delay】====================

//----------Microsecond delay---------

for(i=X;i>X;i--) delay time=(3+5*X)/12 Tips (unit: us, X cannot be greater than 255)

//================Greater than 10US and less than 21.9955Ms can use [10US level delay function]===========

void Delay10us(uchar Ms)

{

uchar data i;

for(;Ms>0;Ms--)

for(i=26;i>0;i--);

}

i=[(delay value-1.75)*12/Ms-15]/4

If you want to delay for 60US, then i=[(60-1.75)*12/6-15]/4=25.375≈26; modify the value of i = 26, and then call the above [10US level delay function] Delay10us(6); then the delay will be exactly 60US;

If you want to delay 64us, you can use these two functions in combination: Delay10us(6); for(i=9;i>X;i--) Total delay 64us

Reference address:51 MCU C language delay function

Previous article:Detailed explanation of 51 MCU assembly delay program algorithm
Next article:51 MCU Programming Skills Summary

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号