Electronic – Software delay vs hardware timers

delaymicrocontrollertimer

Working with a micro controller, on which exact conditions should we choose between Hardware timers and Software delays on an Embedded Controller?

I have seen articles which do emphasize on use of timers.

If timers are so good then why is Software delays needed?

This is the link that describes the usage of s/w or hardware timers.

http://betterembsw.blogspot.in/2012/12/software-timing-loops.html

But this does not emphasize on the case when h/w timers are available on the controller that we are using.

Best Answer

By hardware delays I meant 'Timers'.

The advantage of using timers to realize a delay is that they provide a way to allow async counting. Using a "Software delay" you force the controller to put all its resources into processing some kind of loop (incrementing a variable until a given value) and thus blocking the rest of the code execution path.

If hardware delay is so good then why is Software delays needed?

A software delay is easier to implement and may be sufficient if its just a very short delay which is not significantly interrupting any other task in the main sequential code processing path. Furthermore, the timers may be in use for some other hardware related tasks like PWM generation and may not be "free" to be configured according to your delay requirements.

Another use case would be some initial delay that is required before the main loop is running. There would be no need to use a hardware delay in that case.

One last thing that comes to mind is that a software delay doesn't require interrupts to be globally enabled, while its a requirement for timer based delays (at least for the common use case).