How to calculate the delay time in the microcontroller

Publisher:幸福满溢Latest update time:2015-12-25 Source: eefocusKeywords:MCU 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--);

}

Calculation:

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--);

}

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--);

 

}


Keywords:MCU Reference address:How to calculate the delay time in the microcontroller

Previous article:MSP430 Tutorial 14: MSP430 MCU ADC12 Module
Next article:Common methods for hardware anti-interference of single-chip microcomputer system

Recommended ReadingLatest update time:2024-11-15 15:54

Understanding of counter/timer interrupts in 51 MCU learning
For microcontroller beginners, in order to achieve the purpose of delay control time, the microcontroller is often used to calculate the for loop function. As learning deepens, it is inevitable that we gradually come into contact with timer/counter interrupts to control time. Here, the following discussion is specific
[Microcontroller]
Understanding of counter/timer interrupts in 51 MCU learning
RS485 interface on the microcontroller
Although RS-232 is widely used, it has obvious shortcomings in modern network communications due to its early introduction. For example: 1)  Slow data transmission rate. Although the 20KB/s transmission rate specified by RS-232 can meet the requirements of asynchronous communication, the asynchronous communication rat
[Microcontroller]
STM8 MCU CAN filter settings
Hello everyone, I am Xiaoshu. Today I will share with you some information about CAN filters. The filter is mainly used to set the receiving ID of CAN. When there are many nodes connected to the CAN bus and the amount of data is large, the reception judgment of a single node may cause greater pressure and occupy more
[Microcontroller]
Anti-lock Braking System (ABS) Solution Based on Freescale MCU
Description of the program: Anti-lock Braking Systems ( ABS ) assist the driver with control and vehicle stability in emergency braking situations. Locking the wheels slows the vehicle down, but it results in a loss of steering flexibility. ABS prevents 'steering loss' while applying maximum braking force. ABS does
[Automotive Electronics]
Anti-lock Braking System (ABS) Solution Based on Freescale MCU
High-performance DC regulated power supply based on single chip microcomputer
introduction As we all know, many scientific experiments are inseparable from electricity, and in these experiments, there are often special requirements for power-on time, voltage level, current size and dynamic indicators. Therefore, if the experimental power supply not only has good output quality but also has mul
[Microcontroller]
High-performance DC regulated power supply based on single chip microcomputer
Microcontroller buzzer control program and drive circuit
Buzzers are divided into piezoelectric buzzers and electromagnetic buzzers based on their structure. Piezoelectric buzzers use piezoelectric ceramics to make sounds, and the current is relatively small, while electromagnetic buzzers use coils to vibrate and make sounds, and are relatively small in size. According to
[Microcontroller]
Microcontroller buzzer control program and drive circuit
The "Soul" of the Microcontroller - The Use of Data Memory
We can say that the program memory stores the soul of the microcontroller, which is the working program. The smallest one may be only 1KB (it can only hold 1024 8-bit data at most, because there are many 2-byte and 3-byte instructions in the actual instructions, so it cannot hold 1024 instructions) and the largest one
[Power Management]
There are tips for selecting microcontrollers: choose low-power MCUs based on numerical values
Comparing and selecting low-power microcontrollers (MCUs) based on the current consumption specifications listed in the data sheet is a difficult task. In most cases, developers selecting an MCU will first take a preliminary look at the first page of the data sheet as a reference point to quickly obtain device infor
[Microcontroller]
There are tips for selecting microcontrollers: choose low-power MCUs based on numerical values
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号