3187 views|9 replies

11

Posts

0

Resources
The OP
 

Delay problem in single chip microcomputer [Copy link]

I am a novice in microcontrollers. When I was writing a program, my teacher mentioned that there were too many delays in my program and it was difficult to put it into practice. He recommended the vt_time method to me, which uses a timer to do timing.

I wrote a light program here

void main()

{

system_init();

while(1)

{

led_display(); //Light flip program

vtimer_set(&vt_led, 2000); //Set the interval time
while (!vtimer_expired(&vt_led)); //Judge whether the time is up

}

}

Is it okay to write like this? I feel that the idea I wrote is the same as the delay. Will there be any difference when the MCU processes it? When the MCU processes this timing, is it still in an idling state?

This post is from 51mcu

Latest reply

Can you tell me the method?   Details Published on 2020-3-25 13:01
 

1w

Posts

25

Resources
2
 

Delay uses a single-chip microcomputer loop waiting method, which is a blocking mechanism. The system cannot process other tasks in the delay state, so the efficiency is low.

By using RTOS or timer, the corresponding task will be executed only when the set time is reached, and multiple tasks can be executed in time.

This post is from 51mcu

Comments

So if I write it this way, is it still the same as using delay? The light is on, wait for the time, and then execute the next step when the time is up, and the light will flip.  Details Published on 2020-3-10 15:29
 
 
 

11

Posts

0

Resources
3
 
dcexpert published on 2020-3-10 15:23 Delay uses a single-chip microcomputer loop waiting method, which is a blocking mechanism. The system cannot process other tasks in the delay state, so the efficiency is low. &nbs ...

So if I write it this way, is it still the same as using delay? The light is on, wait for the time, and then execute the next step when the time is up, and the light will flip.

This post is from 51mcu

Comments

Yes, actually it is not much different from delay.  Details Published on 2020-3-10 17:10
 
 
 

1790

Posts

0

Resources
4
 
o景天o posted on 2020-3-10 15:29 So if I write it this way, is it still the same as using delay? The light is on, wait for the time, and then execute the next step when the time is up, and the light flips. .

Yes, actually it is not much different from delay.

This post is from 51mcu

Comments

I would like to ask, for example, how to remove the delay' (debounce) in the key detection. How to write it? And is it better if I write this LED like this? if ((vtimer_expired(&vt_led)) &nbs  Details Published on 2020-3-10 17:37
 
 
 

11

Posts

0

Resources
5
 
sint27 posted on 2020-3-10 17:10 Yes, in fact, it is not much different from delay

I would like to ask, for example, how to remove the delay' (debounce) in the key detection. How to write it? And is it better if I write this LED like this?
if ((vtimer_expired(&vt_led)) //Timeout returns 1

{

led_display(); //Light flip program

vtimer_set(&vt_led, 2000); //Set the interval time

}

This post is from 51mcu

Comments

This can  Details Published on 2020-3-11 07:59
This can  Details Published on 2020-3-10 21:13
 
 
 

1w

Posts

142

Resources
6
 

The Delay instruction is used for very short delays. Longer or very long delays should be done with a timer so that the MCU can still process other tasks or enter a low power state.

This post is from 51mcu
 
Personal signature上传了一些书籍资料,也许有你想要的:http://download.eeworld.com.cn/user/chunyang
 
 

4005

Posts

0

Resources
7
 
o景天o posted on 2020-3-10 17:37 Then I would like to ask, for example, in the key detection, how to remove the delay' (debounce). How to write it. And this LED I like this...

This can

This post is from 51mcu
 
 
 

1790

Posts

0

Resources
8
 
o景天o posted on 2020-3-10 17:37 Then I would like to ask, for example, in the key detection, how to remove the delay' (debounce). How to write it. And this LED I like this...

You can use a timer or time slice framework to do this. For example, you can detect a key press every 5ms and confirm that the key press is valid after several consecutive times.

This post is from 51mcu

Comments

Generally, I don't need to delay if the time exceeds 10 us.  Details Published on 2020-3-11 08:11
 
 
 

1632

Posts

4

Resources
9
 
sint27 posted on 2020-3-11 07:59 You can use a timer or time slice framework to do this, for example, detect a key press every 5ms, and confirm that the key press is valid after several consecutive presses

Generally, I don't need to delay if the time exceeds 10 us.

This post is from 51mcu
 
Personal signature科技改变生活
 
 

17

Posts

0

Resources
10
 
o景天o posted on 2020-3-10 17:37 Then I would like to ask, for example, in the key detection, how to remove the delay' (debounce). How to write it. And this LED I like this...

Can you tell me the method?

image-20200325130055-1.png (23.04 KB, downloads: 0)

image-20200325130055-1.png
This post is from 51mcu
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list