Delay Function in MCUXpresso (like Arduino)

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Delay Function in MCUXpresso (like Arduino)

跳至解决方案
9,032 次查看
caden013
Contributor III

Hi all,

I am just wondering if there is a delay function in the MCUXpresso IDE. For example, I am wanting to create a 1 second delay in my program, and I am wondering if there is a nice function to use instead of creating nested for loops. 

For example, in Arduino code, I would just use the function:

delay(1000);

Thanks in advance.

0 项奖励
回复
1 解答
9,027 次查看
frank_m
Senior Contributor III

To be honest, Arduino is not the best template for professional SW development, to say the least.

Delay functions (busy idling) are one of the worst choices, better use a timer peripheral.

I used to use the SysTick timer available in all Cortex M devices. Set it up at a proper cycle (e.g. 10ms), and handle your delay functionality in the respective interrupt handler.

This way, you can run multiple different delay in parallel without interference and very little performance impact.

在原帖中查看解决方案

8 回复数
9,028 次查看
frank_m
Senior Contributor III

To be honest, Arduino is not the best template for professional SW development, to say the least.

Delay functions (busy idling) are one of the worst choices, better use a timer peripheral.

I used to use the SysTick timer available in all Cortex M devices. Set it up at a proper cycle (e.g. 10ms), and handle your delay functionality in the respective interrupt handler.

This way, you can run multiple different delay in parallel without interference and very little performance impact.

7,972 次查看
SpoonMan
Contributor IV

To be honest, a knife may NOT always be the best option to split an object in two parts, for this reason humans introduced scissors, hacksaws, etc.

And for the exact same reason, busy idling may be the worst solution in some circumstances while may be the BEST solution in other circumstances. Your point is not a good reason for NXP to not provide sleep functions like Arduino does, nor just to discredit other platforms.

7,946 次查看
frank_m
Senior Contributor III

> Your point is not a good reason for NXP to not provide sleep functions like Arduino does, nor just to discredit other platforms.

All my project managers up to now are disagreeing with you.
Higher BOM costs for a more performant MCU just to safe an hour or two of development time is a no-go in commercial projects.

This might be different in private projects.
Arduino is a library that abstracts the hardware level to a certain degree, and lowers the entry barrier for beginners and hobbyists. Which is not a bad thing - unless you have a commercial project with strict BOM cost constraints.

By the way, one certain way to recognize Arduino users in MCU fora used to be the question "How fast can that MCU toggle a GPIO ?".

0 项奖励
回复
7,754 次查看
SpoonMan
Contributor IV
Hello Frank,
yes, I know it doesn't take so much time and effort to code a sleep function using busy wait, using a timer or whatever... what I meant was simply that, in my modest opinion (and all your project managers are better than me, for sure) to code a new sleep function each time I switch to a new platform is just a big annoyance as a developer...
Coding is fun, don't forget!
0 项奖励
回复
7,964 次查看
ErichStyger
Specialist I

If you are looking for 'busy wait' routines, then you might have a look at the McuWait (.c and .h) in https://github.com/ErichStyger/McuOnEclipseLibrary/tree/master/lib/src

This is what I use, and it provides busy waiting with cycles, milliseconds and microseconds. It uses 'burning nops' or hardware cycle counters if available.

So you can use things like McuWait_Waitms(100) or McuWait_Waitus(10) or even McuWait_Waitns(50).

Just keep in mind that the waiting time (of course) does not consider time spent in interrupts.

I hope this helps,

Erich

0 项奖励
回复
7,950 次查看
SpoonMan
Contributor IV

Hi Erich,

thanks for your suggestion, I just meant to emphasize the fact that often, if not always, there is no one-size-fits-all way to tell what is the best or worst option for doing something.

And that actively checking against systick timer current count IS busy waiting; and that NXP SDK is not providing a built-in function for this, while others do... YAWN!

 

0 项奖励
回复
8,738 次查看
auftrag2021
Contributor III

can someone explain the function principle of the SysTick from the Demo LED Blink example code?

What happen if I want to have a smaller delay, like us?

0 项奖励
回复
7,965 次查看
ErichStyger
Specialist I

It works that way:

typically the SysTick is configured for 1 ms. You set a counter value, and the SysTick counts that value down. You wait until that counter is down to zero.

Of course that might depend on the exact example, but this is how it is usually done.

If you want in the us range, you have to increase the speed of the SysTick (say to 100 us) and count it down the same way.

0 项奖励
回复