0

Is there any delay function can be used to PIC18F4550 in C programming, similar to delay() and delayMicroseconds() in Arduino?

The delay functions that can find are Delay10KTCYx(), Delay10TCYx() and etc which is very difficult to generate the delay that we desired, and the lowest delay is not even in milliseconds.

Kindly seek your assists, please. Thank you

2
  • 3
    thinkinbinary.blogspot.nl/2013/04/generating-time-delays.html Commented Aug 10, 2017 at 11:21
  • 2
    the best way is to use the timer. All other methods works fine on a very simple uC but fail on more decent ones. hardcoded delays should be avoided anyway as a very very bad programming habit. Commented Aug 10, 2017 at 12:14

1 Answer 1

1

When doing microcontroller programming, you should always use the on-chip hardware timers if possible. There are typically several of those and perhaps a real-time clock as well. Rather than looking for some busy-delay function, you should look for a driver or HAL around those hardware timers present in your MCU.

In addition, if you need better than 1ms resolution then note that "delay" functions tend to be inaccurate.

Busy-delay() functions/loops are mostly a quick & dirty amateur solution. They are bad because:

  • They consume 100% CPU and thereby 100% power.
  • They have a tight coupling against the compiler and its settings. Different optimization levels might break such delays.
  • They have a tight coupling to the system clock, whereas on-chip timer drivers usually specify which clock to use as a parameter and adjust pre-scaling accordingly.
  • They are typically not very accurate.
  • Overall they do not necessarily have deterministic behavior.
Sign up to request clarification or add additional context in comments.

6 Comments

I think this is a bad advice for this small micro (PIC18F4550; 32K Flash, 2K SRAM, not something which would normally be used with a higher level OS). At bootup when the program is setting up hardware, delays may be used as the particular hardware demands, during program execution, for a couple of microseconds also delays are the best practice, not needing any "external" resource (such as a timer). The base frequency of the timers is usually also the system clock, so no win there either. Of course the delay needs to be an assembler routine (possibly generated).
@Jubatian No, this advise applies perfectly for a bare metal MCU. For any MCU program of any kind, you are going to need some manner of generic timer. This is best implemented through a RTC or one of the timer peripherals. Once you have this you can easily implement a function like delay(ms). For shorter delays with us resolution, you'll want to use a dedicated on-chip timer. And yes the source will be the system clock, but if your timer driver knows the system clock and the desired frequency, it can pick pre-scaler settings accordingly - which is a big win.
Same for a properly generated delay code (both a prescaler setting and generated code is compile time!), without using a precious timer (which are also scarce on such small micros!). The delay code produces a guaranteed minimum delay (an IT interrupting could lengthen it, but if the overall process was sensitive to it, then it doesn't matter whether it fires in the delay or somewhere else). What you suggest is good for process control, there it is indeed greatly recommended on anything, but what the OP asks is more like for special HW interfacing at microsecond range.
And you should still correct the part of the answer where you mention why busy-delay functions are bad. If you delay using a timer, that's still a busy delay, just polling a timer until it hits a target, so those points are wrong there without clarification.
@Jubatian Another major difference is that a hardware timer will keep running. A "burn-away" amateur thing will freeze and wait for interrupts to execute. Meaning that the hardware timer version will at worst have the inaccuracy of 1 ISR call, while the amateur thing will have the inaccuracy of n ISR calls. The typical case where the amateur thing breaks, is LCD initialization code over SPI, where you need delays but the SPI/DMA generates interrupts.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.