delay function in for PIC18F4550

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

Upvotes: 0

Views: 1243

Answers (1)

Lundin
Lundin

Reputation: 214810

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.

Upvotes: 1

Related Questions