Issues that need attention in single chip C language delay

Publisher:温暖微风Latest update time:2016-03-10 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
There is no empty statement in the standard C language. However, in C language programming of microcontrollers, it is often necessary to use several empty instructions to produce a short delay effect. This is easy to achieve in assembly language, just write a few nops.

In Keil C51, call the library function directly:

#include // declares void _nop_(void);

_nop_(); // Generate a NOP instruction

Function: For very short delays, which are required to be at the us level, use the "_nop_" function, which is equivalent to the assembly NOP instruction, with a delay of several microseconds. The NOP instruction is a single-cycle instruction, and the delay time can be calculated from the crystal frequency. For a 12M crystal, the delay is 1uS. For longer delays, which are required to be greater than 10us, use the loop statement in C51 to achieve it.

When choosing a loop statement in C51, pay attention to the following issues

First, when defining loop variables in C51, try to use unsigned character variables.

Second, in the FOR loop statement, try to use variable subtraction to loop.

Third, in the do...while and while statements, the variables in the loop body are also reduced.

This is because in the C51 compiler, different instructions are used to implement different loop methods.

Here are some examples:

unsigned char i;

for(i=0;i<255;i++);

unsigned char i;

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

Among them, after the second loop statement C51 is compiled, it is completed with the DJNZ instruction, which is equivalent to the following instruction:

MOV 09H, #0FFH

LOOP: DJNZ 09H, LOOP

The instructions are quite concise and it is also easy to calculate the precise delay time.

The same is true for do...while and while loop statements.

example:

unsigned char n;

n=255;

do{n--}

while(n);

or

n=255;

while(n)

{n--};

After these two loop statements are compiled by C51, they form the method completed by DJNZ.

Therefore, it is also very convenient to calculate the exact time.

Third: For those who require a longer delay time, the nested loop method should be used. Therefore, the nested loop method is often used to achieve a delay of ms. The loop statement can also be completed using the for, do...while, while structure, and the variables in each loop body are still unsigned character variables.

unsigned char i,j

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

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

or

unsigned char i,j

i=255;

do{j=255;

do{j--}

while(j);

i--;

}

while(i);

or

unsigned char i,j

i=255;

while(i)

{j=255;

while(j)

{j--};

i--;

}

All three methods use DJNZ instructions to nest loops, which are implemented by the C51 compiler using the following instruction combination:

MOV R7, #0FFH

LOOP2: MOV R6, #0FFH

LOOP1: DJNZ R6, LOOP1

DJNZ R7, LOOP2

The combination of these instructions uses the DJNZ instruction in assembly language to delay, so its accurate time calculation is also very simple. Assuming that the initial value of the variable i is m and the initial value of the variable j is n, the total delay time is: m×(n×T+T), where T is the execution time of the DJNZ instruction (DJNZ instruction is a two-cycle instruction). Here +T is the time used by the MOV instruction. Similarly, for longer delays, multiple cycles can be used to complete it.

Just pay attention to the above issues when programming loop statements.

The following are some issues to pay attention to when designing a delay subroutine in C51:

1. When designing precise delay subroutines in C51, try not to define local variables in the delay subroutines or define as few local variables as possible in the delay subroutines. All variables in the delay subroutines are passed through parameterized functions.

2. When designing a delay subroutine, it is better to use the do...while structure as the loop body rather than the for structure.

3. When designing a delay subroutine, if you want to nest the loop body, it is better to use the inner loop first and then reduce it rather than reduce it first and then the inner loop.

unsigned char delay(unsigned char i,unsigned char j,unsigned char k)

{unsigned char b,c;

b="j";

c="k";

do{

do{

do{k--};

while(k);

k="c";

j--;};

while(j);

j=b;

i--;};

while(i);

}

This precise delay subroutine is compiled by C51 into the following instruction combination

The delay subroutine is as follows:

MOV R6,05H

MOV R4,03H

C0012: DJNZ R3, C0012

MOV R3,04H

DJNZ R5, C0012

MOV R5,06H

DJNZ R7, C0012

RET

Assuming that the initial value of parameter variable i is m, the initial value of parameter variable j is n, and the initial value of parameter variable k is l, the total delay time is: l×(n×(m×T+2T)+2T)+3T, where T is the execution time of DJNZ and MOV instructions. When m=n=l, the precise delay is 9T, the shortest; when m=n=l=256, the precise delay is 16908803T, the longest.

Keywords:MCU Reference address:Issues that need attention in single chip C language delay

Previous article:Summary of single chip microcomputer delay method
Next article:How to write efficient and beautiful C language code for microcontrollers?

Recommended ReadingLatest update time:2024-11-17 07:17

Single chip LCD12864 with font library can call display function can reverse display source program
When I used LCD12864, I used JLX12864G-086-pc, which is an LCD12864 screen with font library. I adopted the SPI serial port method and modified it according to the standard routines provided by the manufacturer. It was made into an independent function module that can be called, and the displayed text can be highlight
[Microcontroller]
Serial port interrupt setting of 51 single chip microcomputer
First, you need to enable interrupts using software. That is, in C language, EA = 1; enable total interrupt ES = 1; //Enable serial port interrupt assembly SETB EA ; Enable general interrupt SETB ES ; Enable serial port interrupt When the microcontroller receives a frame of data, RI will be set to 1 and an i
[Microcontroller]
Design of remote control switch system based on single chip microcomputer
1 Introduction With the rapid development of electronic technology, new large-scale remote control integrated circuits have emerged, and remote control technology has developed rapidly. The central control components of remote control devices have gradually developed from early discrete components and integrated cir
[Microcontroller]
Design of remote control switch system based on single chip microcomputer
Renovation design of old residential heating metering system based on 89C51 microcontroller and CAN controller
1 Introduction CAN bus technology was born in the field of automotive control. As its technology continues to improve, its application areas continue to expand. Since CAN bus technology entered our country, it has gone through the introduction and use stages. Since 2000, many relevant domestic manufacturers have condu
[Microcontroller]
Renovation design of old residential heating metering system based on 89C51 microcontroller and CAN controller
Single-key circuit design and corresponding program based on single-chip microcomputer
Single-key means that each key occupies a pin of a single-chip microcomputer. The characteristics of this connection method are that the circuit and program are very simple, but the disadvantage is that it occupies many pins of the single-chip microcomputer and consumes a lot of hardware resources. This connection m
[Microcontroller]
Design of wireless identification device system based on 51 single chip microcomputer
    introduction   Radio frequency identification is a contactless automatic identification technology that automatically identifies the target object and obtains relevant data through radio frequency signals. Radio frequency identification does not require human intervention, is contactless, has a fast reading speed
[Microcontroller]
Design of wireless identification device system based on 51 single chip microcomputer
Application of microcontroller in baud rate converter
In some complex systems, there are data transmission problems between systems and subsystems, subsystems and equipment, etc., which are often solved by communication. Due to the different communication baud rates among subsystems and devices, especially the existence of some special baud rate devices, it is difficult t
[Microcontroller]
Design of electronic tour guide MP3 based on single chip microcomputer
introduction MP3 players have developed rapidly in recent years, and a large number of products have been launched. Tourism, an emerging industry, has been booming in recent years. Compared with manual tour guides, electronic tour guides can avoid the instability of service quality caused by personal factors of tour
[Microcontroller]
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号