Analysis of the Accurate Delay Program of 51 Single Chip Microcomputer

Publisher:LuckyDaisyLatest update time:2016-10-04 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1. C51 Program

When the crystal oscillator is 11.0592, the following program can achieve a relatively accurate delay at the ms level:

1 void Delayms(unsigned int n)

2 {

3     unsigned int i,j;

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

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

6 }

Use keil to see the delay time. We first delay for 1ms, that is, Delayms(1).

Before entering Delayms, sec=0.00042209s

Analysis of the precise delay program of 51 single-chip microcomputer (C language and assembly language program) - Star - StarAfter delay, sec=0.00142253s

Analysis of the precise delay program of 51 single-chip microcomputer (C language and assembly language program) - Star - StarIt can be known that the actual delay of Delayms (1) is 0.00142253s-0.00042209s=0.00100044s≈1ms

Similarly, if you want to delay for 15ms, use Delayms(15). The actual delay is 0.01480903s≈15ms, which is quite accurate.

Reference: www.jiangx.net/post/214.html

The above examples give us the inspiration that if we are not sure how much the delay program will delay, we can detect it through debugging.

--------------------------------------------------------------------------------

2. Assembly program (500ms):

C:0x0800      7F0F        MOV         R7,#0x0F

C:0x0802      7ECA       MOV          R6,#0xCA

C:0x0804      7D51        MOV         R5,#0x51

C:0x0806 DDFE DJNZ R5,C:0806

C:0x0808 DEFA DJNZ R6,C:0804

C:0x080A     DFF6        DJNZ         R7,C:0802

C:0x080C     22            RET     

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

Assembly instruction quick reference table: http://www.cainiao8.com/embedded/51danpianji/danpianji03_zhiling.html

--------------------------------------------------------------------------------

3. Delay Program Analysis

When writing microcontroller programs in assembly language, this problem is relatively easy to solve. For example, if you use a 51 with a 12MHz crystal oscillator and want to delay for 20us, you can use the following code to meet general needs:

mov     r0, #09h

loop:       djnz    r0, loop

The machine cycle of the 51 MCU is 1/12 of the crystal frequency, which is 1us per cycle. mov r0, #09h requires 2 machine cycles, and djnz also requires 2 machine cycles. Then the number stored in r0 is (20-2)/2. With this method, it is very convenient to achieve a delay of less than 256us. If a longer time is required, two layers of nesting can be used. And the accuracy can reach 2us, which is generally enough.

Nowadays, the most widely used one is undoubtedly Keil's C compiler. Compared with assembly, C has many advantages, such as easy program maintenance, easy to understand, and suitable for large projects. But the disadvantage is that the real-time performance is not guaranteed, and the instruction cycle of code execution cannot be predicted. Therefore, in situations where real-time performance is high, the joint application of assembly and C is also required. But does such a delay program also need to be implemented in assembly? In order to find the answer, I did an experiment.

When implementing a delay program in C language, the first thing that comes to mind is the commonly used loop statement in C. The following code is what I often see on the Internet:

void delay2(unsigned char i)

{

for(; i != 0; i--);

}

How high of an accuracy can this code achieve? In order to directly measure the effect of this code, I found the assembly code generated by Keil C based on this code:

; FUNCTION _delay2 (BEGIN)

; SOURCE LINE # 18

;---- Variable 'i' assigned to Register 'R7' ----

; SOURCE LINE # 19

; SOURCE LINE # 20

0000 ?C0007:

0000 EF MOV A,R7

0001     6003             JZ         ?C0010

0003     1F                DEC      R7

0004 80FA SJMP ?C0007

; SOURCE LINE # 21

0006 ?C0010:

0006 22 COURT

; FUNCTION _delay2 (END)

You really don't know until you see it~~~ You will know how inaccurate this delay program is~~~ Just looking at the four main statements, it takes 6 machine cycles. In other words, its accuracy is at most 6us, and this does not include an lcall and a ret. If we list the value of i assigned when calling the function and the delay length in a table, it is:

i    delay time/us

0    6

1    12

2    18

...

If you change (unsigned char i) in the program to (unsigned int i), the assembly program will become even more inconsistent with your intention.

Reference address:Analysis of the Accurate Delay Program of 51 Single Chip Microcomputer

Previous article:51 MCU and Mitsubishi PLC communicate successfully without protocol
Next article:Communication and control between PC and single chip microcomputer RS-232 serial port

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号