Delay and interrupt error, is a problem that MCU newbies often encounter in the process of MCU development and application. This paper summarizes and includes MCS-51 series MCU, MSP430 MCU, C51 MCU, 8051F MCU, avr MCU, STC89C52, PIC microcontrollers ..... inside the various microcontrollers common delay and interrupt problems and solutions, I hope to help newcomers to the microcontroller!

1. How is the delay time of the MCU delay program calculated?

A: If you use the loop statement to achieve the loop, you can't calculate it, but you can see the specific time through software simulation, but generally fine precision delay can't be realized with loop statement.

If you want precise delay, you usually need to use a timer. The delay time is related to the crystal oscillator. The microcontroller system usually uses 11.059 2 MHz, 12 MHz or 6 MHz crystal oscillator. The first is easier to produce a variety of standard baud rates, the latter two machine cycles are 1 μs and 2 μs, respectively, for precise delay. It is assumed in this program that a crystal with a frequency of 12 MHz is used. The longest delay time is 216 = 65 536 μs. If the timer works in mode 2, the precise delay can be realized in a very short time; if other timing methods are used, the time of reinstalling the initial value of the timing should be considered (the initial value of the reloading timer takes 2 machine cycles).

2, seeking a single-chip 89S51 12M crystal oscillator with a delay of 10 minutes, control 1 lamp can be

Answer: It can be set to interrupt once for 50ms, the initial value of timing, TH0=0x3c, TL0=0xb0. Interrupt 20 times for 1S, 10 minutes, it is necessary to interrupt 12000 times. After 12000 times, give a IO port a low level (if the power is not enough, you can expand it), you can control the lights.

But also depends on what language you use to calculate, assembly delay is accurate, know the microcontroller cycle and cycle times can be calculated, but not portable, in different types of microcontroller, assembly is not universal. With c, because the various software execution efficiency is not the same, it will not be too accurate, usually use the timer to delay or make an inaccurate delay. If the delay is short, use the assembled nop to delay in c.

3, 51 single-chip C language for loop delay program time calculation, set the crystal oscillator 12MHz, that is, a machine cycle is 1us.

For(i=0,i<100;i++)

For(j=0,j<100;j++)

I think the time is 100*100*1us=10ms, how can it be 100ms?

answer:

Impossible, is your compilation wrong?

I changed the crystal 12M, compiled in KEIL 4.0, the result for you is 40ms, which is the reason for the software.

It is impossible to have a gap of 100ms, which is the reason for your software.

Do not believe that you actually write a second, use the principle calculation to write a second program burned into the microcontroller and use the software test to burn into the microcontroller, you will find that the principle calculation program is correct

4, 51 single-chip c language _nop_ () is a null command? Short time delay? Empty several machine cycles?

A: This _nop_() is equivalent to the assembly, NOP instruction, that is, a machine cycle is empty. If it is a traditional 51 MCU, it is equal to 12 clock cycles [ie one machine cycle]

5, 51 single-chip delay 500ms How to calculate with the machine cycle superposition?

A: DELAY:

MOV R7, #4

D2: MOV R6, #250

D1: MOV R5, #250

DJNZ R5, $

DJNZ R6, D1

DJNZ R7, D2

RET

Assume that the crystal is 12MHz

The delay time is:

250*250*4*2=500MS

6. What is the principle of the delay function delay in the 51-chip M language program?

Now find two functions

the first:

Void delay(void)

{ unsigned int i,j;

For(i=0;i<500;i++)

{ for(j=0;j<121;j++)

{;}

}

}

second:

Void delay(unsigned int k)

{ unsigned int i,j;

For(i=0;i

{ for(j=0;j<121;j++)

{;}

}

}

There are several questions:

(1): The principle of the delay function?

(2): The role of two for loops?

(3): What is the law and basis of the value of i and j? Is it related to the crystal frequency of the single-chip microcomputer? How is the minimum unit time of the delay time calculated?

How to calculate the delay time! If you use the crystal oscillator AT89C51RC+11.0592M?

answer:

1: Principle: Only perform some so-called "meaningless instructions" that have no substantial impact, such as doing more than the size, doing some int self-addition operation, etc.

2: The role of the two for: In a nutshell, just like the "multiplication principle" in high school mathematics, this can easily and quickly increase the number of "meaningless instructions"

3: Regarding the value of the value: If this is changed under C, this value is not only related to the operation speed of the crystal oscillator and the MCU itself, but also related to the C compiler. Therefore, although this value can be accurately calculated, But in most cases, programmers use "experience values" - of course, if you use assembly programming, the situation is different, because the machine cycle used by each instruction is certain, you can of course follow all instructions Total time used, accurately calculate the total time of the specific delay

To sum up your question, I will give you some advice. When you first learn the MCU, you must still learn from assembly programming. This way, after you come into contact with C, you can understand that this is actually the middle. What kind of process has gone through, only then can you really understand the microcontroller. Of course, after you finally get a single MCU, try to use C programming, which is undoubtedly affirmed by history.

7, 51 single-chip, crystal oscillator is 6M, seeking a 10ms delay program

A: There are many ways to delay. One is to let the microcontroller do the boring cycle, and the other is to use the timer.

The first algorithm is:

The period of the crystal oscillator T1=1/f; where f=6MHz so T1=1/6 us; (microseconds)

The MCU takes 12 T1s to execute an instruction.

So one machine cycle is equal to 12 crystal cycles,

T2=12*T1=2us

10ms=1000 0us

So you have to get a 10ms delay to find a way to let the machine do 5000 "boring instructions"

and so

DEL: MOV R5, #05H

F1: MOV R6, #05H

F2: MOV R7, #32H

F3: DJNZ R7, F3

DJNZ R6, F2

DJNZ R5, F1

RET

This method is used for places where time is not high. I am talking about the idea, there may be some wrong places in the program.

I don't know how to use the timer. I added that this is written in assembly. You can delay the call with ACALL DEL in the main program.

8. Today, when I use the single-chip microcomputer to do the "Blinking LED" experiment, the program runs, and only the light is turned on or off every time, but there is no problem that the expected light is off when the delay is turned on. What is going on? ?

The hardware conditions of the experiment are: STC89C52, compilation environment: keil 3.

Here is the program I wrote, ask the master!!!

#include // File contains processing

#define uchar unsigned char // macro definition for future program writing

#define uint unsigned int

Sbit P1_0 = P1 ^ 0; //bit variable definition

Void Delay(uint t)

{

Uchar i;

While(--t)

{

For(i = 0; i < 125; i++) //delay 1MS, the crystal we use here is 12M, according to the calculation of the machine cycle, we

{;} / / can be calculated this cycle delay about 1MS

}

}

Void main(void)

{

While(1)

{

P1_0 = 0; //Light the LED light

Delay(1000); //The time for executing the program on a single chip is very fast, so it must be delayed, or you can't see the experimental phenomenon.

P1_0 = 1; //extinguished LED

}

Supplementary question: I let P1.0 be low first and then delay after high, that is, the light will be on and then off, then start to cycle.

A: It should be written like this

While(1)

{

P1_0 = 0; //Light the LED light

Delay(1000); //The time for executing the program on a single chip is very fast, so it must be delayed, or you can't see the experimental phenomenon.

P1_0 = 1; //extinguished LED

Delay(1000);

Supplementary question reply: The problem is just wrong. After the cycle is finished, there is no time delay from the light to the light. That is, the machine that has not come before the first cycle is extinguished, and it enters the second round of circulation. It’s bright, so the reason is here. This error is too low-level, so let’s take it for granted.

9, the problem of the microcontroller delay function

Void delay(uchar i)

{

Uchar j;

While(i--)

{

For(j=125;j>0;j--)

;

}

}

Is the size of i, j in this function **?

A: The size of j in this function is related to the data type you define. Because you define an unsigned character type, it is single-byte data, so the maximum is 255. .

If you need to increase, you can change the data type definition of j, such as unsigned int (2 bytes) can go to 65535; unsigned long integer unsigned long (4 bytes) can go to 4294967295. The above 256 is -1, and you define an unsigned character.

10, ask an AVR microcontroller delay problem

The external crystal oscillator is 8MHz, and the delay of 1 microsecond is as follows:

Void delay_us(unsigned int delay_counter)//delay 1us

{

Do

{

Delay_counter--;

}

While(delay_counter>1);

}

Excuse me, why can I delay 1 microsecond?

A: 8MHZ means that the operating cycle of the MCU is 1/8us, which is 0.125us.

You are using software delay

Then it takes time to include the extraction, execution, etc. of the program.

For example, if you extract this function, you might take a step, and now you are using 0.125us.

Then you execute this function. Inside the microcontroller, the operation is implemented by shifting the register.

It takes time, maybe you see a counter--this instruction, it may take several clock cycles to achieve

for example:

c=a+b, only one sentence, but the time it takes is not short

Mov a, #data1;//data1 is placed in a register

Mov b, #data2;//datadata2 put in b register

Add a,b;//The value of register a is added to b, and the result is put in a

Mov c,a;//put the value of a into c

This is the instruction that is actually executed inside the microcontroller, which takes at least 4 clock cycles instead of 1

As for the transistor level, I will not explain it. You have to learn the assembly to understand the operation of the microcontroller.

As for why this function can delay 1ms, this is judged by experience. The most direct method is to use an oscilloscope. All of the above are inferences.

11, PIC microcontroller delay problem crystal oscillator 4Mhz:

Void delay()

{

Unsigned int d=1000;

While(--d){;}

}

This function produces a delay of 10003us in a 4M crystal, which is 10MS.

Question: I just figured out that he should execute 999 instructions, 1 single-cycle instruction is only 1US, that is 999us, why is there a delay of 10ms?

1:for(x=100;--x;){;} : 2: for(x=0;x<100;x++){;} 2 sentences are the same

The first sentence: Is the value range of X 1~99? Why?

The second sentence: Is the range of X not 0~99? Why? I know the difference between the symbol before and after. 2 sentences should be different!

answer:

Question 1: "I just figured out that he should execute 999 instructions" because you are wrong. The delay time is determined by the generated assembly code. The C language statement is just an illusion. Don't assume that the C language line is an instruction! Here, because of the double-byte subtraction, there will be additional judgments. It is not surprising that each cycle takes dozens of cycles.

Question 2: The previous sentence x is decremented from 100, and is decremented to 1 to exit the loop. The next sentence x starts from 0 and exits the loop when it is incremented to 100. The so-called "2 sentences" is the same as merely referring to the same number of cycles of the two loop bodies. In fact, the execution of the two loops is completely different, and the time consumed may be different.

12, stc microcontroller delay problem, STC10F08XE microcontroller, crystal oscillator 22.1184M

Void delay(unsigned long uldata)

{

Unsigned int j = 0;

Unsigned int g = 0;

For (j=0;j<5;j++)

{

For (g=0;g

{

_nop_();

_nop_();

_nop_();

}

}

}

How many seconds does it delay when uldata=1?

Please give a specific algorithm............

A: Convert it to assembly statement with keil, then calculate it against the instruction list.

13, I want to use the MCU connection to continuously send the number to the computer, as follows:

While (1)

{

Send_char('9');

Delay(n);

}

For example, how many subtle delays should be given for each number sent? That is, how many micrometers can it be delayed in the shortest? If the delay is too long, does it take a long time to send a lot of data?

A: Don't do too much serial port processing analysis, just follow your question and your method:

First consider the serial port rate assumption 9600, then how long does it take to send a character?

(9600bit/S) / 10bit (one character 1+8+1) = 960 characters/second approx. 1ms/byte

In other words, if you send more than one character in 1ms, it doesn't make sense, and the hardware speed can't reach.

While(1)

{

Send_char('9');

Delay(n);

}

This loop is the delay of the execution cycle of ten microseconds + delay(), so any delay less than 1040 microseconds is meaningless to the serial hardware. The last one has not been processed yet, the next one can not be executed at all. .

If you have a while(!TI);TI = 0 in send_char(); such a statement or a serial interrupt TI processing, then your delay() is actually in the send function, while(!TI); Is this part of the delay waiting? That does not require the main function to delay, just send it directly.

14, a single-chip delay subroutine problem, in the delay subroutine, has been unable to understand, give r7 and r6 0, then the following djnz r7, delayloop does not have to cycle, then how to continue the program ?

Org 0000h

Ljmp start

Org 0030h

Start: mov a,#0feh

Mov r5, #8

Output: mov p1,a

Rl a

Call delay

Djnz r5,output

Ljmp start

Delay: mov r6,#0

Mov r7,#0

Delayloop:djnz r7,delayloop

Djnz r6, delayloop

Ret

End

A: Your delay program is not because the value is 0, but the jump position is incorrect, as follows:

Delay: mov r6,#0

Delayloop:mov r7,#0

:djnz r7,$

Djnz r6, delayloop

Ret

R7, R6 initial value is 0, but when DJNZ is executed, this instruction is first decremented by 1 and then judged, so 0-1=255, if it is judged, it is not 0, and it still loops 256 times.

Explanation of 0-1=255:

0000 0000

- 0000 0001

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

1111

15, I want to mention the problem of two MCU delays and buttons

1: If there is a delay and a button in a program, if the delay subroutine is long (if 2 seconds), how to ensure that the button can respond in time (if the PC is executing the delay subroutine, there is a key input at this time, not a response No)),,,, the premise is that you can't use the timer to scan regularly, and interrupts, because timers and interrupts have other uses.

2: The microcontroller does not have a serial port. How can I communicate with the 24C02 (24C02 is an EEPROM with 2K memory)

answer:

First of all, make it clear that you don't have a serial port for the microcontroller. It should mean that there is no I2C port.

1 Add the detection of the button in the delay program

2 IO port analog I2C timing read and write

16, 51 single-chip delay small program, seeking what the expert explains?

Delay200ms:

Mov r2, #82

L0: mov r1, #116

L1: mov r0, #9

Djnz r0,$

Djnz r1,l1

Djnz r2, l0

Ret

A: The following is the time of each instruction, T is a machine cycle

Delay200ms:

Mov r2,#82;1T

L0: mov r1, #116; 1T

L1: mov r0, #9; 1T

Djnz r0,$;2T

Djnz r1,l1;2T

Djnz r2,l0;2T

Ret;2T

The above three layers of loops, ignoring some of the instructions, the simplest algorithm is:

2*9*116*82=171216

The instructions are not ignored:

1+(1+(1+2*9+2)*116+2)*82+2=200001

So the delay time is about 200ms

17, the problem of 51 microcontroller delay time

Uchar i;i--;

Uint i;i--;

What is the running time of these two statements under the 12M crystal oscillator?

A: One clock cycle, 2us, a total of 4us

18, how to edit the subroutine with a delay of 10 seconds for the 6MHZ microcontroller?

answer:/*********************************************** *********************

* Name: Delay()

* Function: Delay, delay time is 10ms * del. This is a software delay with a certain amount of error.

* Enter: del

* Output: None

************************************************** *********************/

Void Delay(uint del)

{

Uint i,j;

For(i=0; i

For(j=0; j<1827; j++) //This is the number obtained by software simulation

;

}

This is a program with a crystal oscillator of 12mhz with a delay of 10ms. You only need to double it on this basis. Of course, as for the specific value, you have to debug it.

19, some programs of the tablet need to call the delay program, how can we reasonably arrange the number of loops and the number of empty operations?

A: Use assembly to calculate your instruction cycle based on your current crystal frequency, and then write the delay program in conjunction with the time you need to delay. If you use C, you still need to see what the resulting assembly code looks like. The easiest way is to write the program and then see the time in the soft simulation of the compiler.

20, single chip delay program problem

Delay program void delay(uint dt)

{

Uchar bt;

For(;dt;dt--);

For(bt=0;bt<255;bt++);

}

Compilation with warning C: DOCUMENTS AND SETTINGSADMINISTRATOR desktop word 310 dot matrix LED display. C (46): warning C235: parameter 1: different types

Why? Ask for heroes to give pointers

A: A function parameter type does not match the declaration type.

In addition, your for(;dt;dt--); does not play the role of the outer loop...

Indoor Optical Fiber Cable

Indoor Outdoor Fiber Optic Cable,Indoor Outdoor Fiber,Indoor Fiber Cable,Indoor Fiber Optic Cable Types

Zhejiang Wanma Tianyi Communication Wire & Cable Co., Ltd. , https://www.zjwmty.com