C language delay function in single chip microcomputer

Publisher:范隆Latest update time:2016-05-23 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
MCU C language delay program calculation 2009-11-02 22:15 MCU C language delay program written in C language is very concise. It is a modular language, a higher-level language than assembly, but even such a language still has its shortcomings: its delay is very difficult to control. It is often difficult for us to know the exact delay of a delay program, which is incomparable to the assembly delay program. But sometimes the post-writing program has to use a more accurate delay. Although it can be solved by mixed programming, this method is not mastered by everyone and is troublesome to write. Therefore, through testing, I provide you with a delay subroutine module, and use it to give a data table of the delay we often use. (Note: The data in the table is only suitable for my delay module, not for other delay programs, and be careful! ! ! ! ! ! ! Don't scold me when the delay is wrong)

         Delay module: The question mark represents the number to be filled in. Find the data in the table and fill it in. Be careful not to reverse the order of the three FOR statements.

void Delay()
{
unsigned char a,b,c;
for(a=0;a for(b=0;b for(c=0;c

50us 1 1 61 0 0 50us 1 1 1 -0.5        20us 1 1 8 0   30us 1
1                                                          15 +0.5  40us 2 1 9 0   50us 1 1 28 0   60us 1 1 35 +0.5  70us 1 1 42 +1  80us 1 1 48 0  90us 1 1 55 +0.5   100us 1 1 61 -0.5   200us 1 1 128 0   300us 3 1 63 +1.5   400us 2 1 128 0   500us 5 1 64 +0.5   600us 6 1 65 0   700us 7 1 63 -0.5   800us 1 3 175 +0.5   900us 9 1 63 -1.5   1ms 1 3 219 -1.5 2ms 2 3 220 +3 3ms 3 3 220 +3 Xms X 3 220 +3                          (the range of X is 2 to 255)























         Basically, all the delays we usually use are here. The error of each delay is very small, and the maximum is no more than 3us. Some even have no error, which is already very precise. If you want to delay for 1 second, you can call the 250ms delay program 4 times in succession, with a total delay error of 12us. This error is no longer considered an error, and it cannot be achieved to this extent when written in assembly language.

         Now you no longer have to worry about inaccurate delays. Just write the delay according to the data in the table and you can set the delay as much as you want.

         Once again, I would like to reiterate: this table is written based on my delay module, which is also a module that most of my friends are accustomed to using. If you use other modules or modify my module, the delay will not be accurate. If you don’t believe it, you can try it!!! 
 

Keywords:MCU Reference address:C language delay function in single chip microcomputer

Previous article:A method of generating accurate delay in embedded single chip microcomputer
Next article:How to use the nop function and delay calculation in the C language of the microcontroller

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号