Problems needing attention in C language delay of single-chip microcomputer

Source: Internet
Author: User
Tags define local

There are no empty statements in the standard C language. But in the MCU programming of C language, it is often necessary to use a few empty instructions to produce short delay effect. This is easy to achieve in assembly language, write a few nop on the line.

In Keil C51, call the library function directly:

#include//declared void _nop_ (void);

_nop_ (); Produce a NOP instruction

Function: For the delay is very short, requirements in the US level, the use of "_nop_" function, this function quite assembly NOP instruction, delay a few microseconds. NOP instruction is a single-cycle instruction, can be calculated by the crystal frequency delay time, for 12M crystal oscillator, delay 1uS. For the longer delay, the requirement is greater than 10US, the use of C51 in the loop statement to achieve.

There are a few issues to be aware of when looping statements in select C51

First, the definition of the C51 in the loop variable, as far as possible the use of unsigned character variables.

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

Thirdly, in the Do...while,while statement, the variable in the loop body also adopts the method of reducing and reducing.

This is because in the C51 compiler, different methods of looping are used to accomplish the different instructions.

The following examples illustrate:

unsigned char i;

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

unsigned char i;

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

Where the second loop statement C51 compiled, it is done with the djnz instruction, which is equivalent to the following instruction:

MOV 09H, #0FFH

LOOP:DJNZ 09h,loop

The instructions are fairly concise and good for calculating the exact delay time.

The same is true for Do...while,while loop statements.

Cases:

unsigned char n;

n=255;

do{n--}

while (n);

Or

n=255;

while (n)

{n--};

These two loop statements are C51 compiled to form a djnz to complete the method,

Therefore, the calculation of the precise time is also very convenient.

Third: For the requirements of the precise delay time is longer, then the loop nested method is used to achieve, therefore, the loop nesting method is often used to achieve MS-level delay. For loop statements, the same can be done with the for,do...while,while structure, and the variables in each loop body still use 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--;

}

These three methods are implemented using the DJNZ instruction nested implementation loop, by the C51 compiler with the following combination of instructions to complete the

MOV R7, #0FFH

Loop2:mov R6, #0FFH

LOOP1:DJNZ R6,LOOP1

DJNZ R7,LOOP2

The combination of these instructions in assembly language using DJNZ instruction to do delay, so its time to accurately calculate is also very simple, false above the initial value of the variable i is m, the initial value of the variable j is n, then the total delay time is: MX (nxt+t), where T is djnz instruction execution time (DJNZ instruction is a double-cycle instruction). The +t here is the time used by this instruction of Mov. The same is true for longer delays, which can be done with multiple loops.

Just pay attention to the above issues when designing a looping statement.

Here are some questions to be aware of when delaying subroutine programming in C51

1, in the C51 in the precise time-delay subroutine programming, try not to or less in the delay subroutine to define local variables, all the delay subroutine in the variable through the function of the parameter passed.

2, in the time-delay sub-program design, the use of do...while, the structure of the loop body than for the structure to do a good loop.

3, in the time-delay sub-program design, to carry out the loop body nesting, the use of first internal circulation, and then reduce and reduce the ratio of first reduction, and then the internal circulation is better.

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 to have the following command combination complete

The delay latency 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 the parameter variable i is m, the initial value of the parameter variable j is n, the initial value of the parameter variable K is L, then the total delay time is: LX (NX (MXT+2T) +2t) +3t, where T is the time of djnz and MOV instruction execution. When m=n=l, the precision delay is 9 T, the shortest, when m=n=l=256, accurate delay to 16908803T, the longest.

Invitation Letter Http://www.biyinjishi.com/products/a10-b1030/
Invitation http://www.biyinjishi.com/products/a10-b1030/
Card http://www.biyinjishi.com/products/a10-b1035/
label http://www.biyinjishi.com/products/a10-b1035/
promotional material http://www.biyinjishi.com/products/a20/
single page http://www.biyinjishi.com/products/a20-b2010/
Folding http://www.biyinjishi.com/products/a20-b2010/
Leaflet Page http://www.biyinjishi.com/products/a20-b2010/d100015/
Propaganda Hinge http://www.biyinjishi.com/products/a20-b2010/d100016/
Poster http://www.biyinjishi.com/products/a20-b2030/d100020/
Display Board http://www.biyinjishi.com/products/a20-b2030/d100021/
Exhibition stand http://www.biyinjishi.com/products/a20-b2035/d100021
X-Show Stand http://www.biyinjishi.com/products/a20-b2035/d100023/
Easy Paula http://www.biyinjishi.com/products/a20-b2035/d100024/
Tote Bag http://www.biyinjishi.com/products/a20-b2050/d100024

Problems needing attention in C language delay of single-chip microcomputer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.