# PIC32MZ Time delay interference using FreeRTOS

**URL:** <https://forums.freertos.org/t/pic32mz-time-delay-interference-using-freertos/10324>\
**Category:** Microchip\
**Created:** [July 21, 2020, 7:16pm UTC](https://forums.freertos.org/t/pic32mz-time-delay-interference-using-freertos/10324 "2020-07-21T19:16:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kenny](https://avatars.discourse-cdn.com/v4/letter/k/90db22/32.png) [@Kenny](https://forums.freertos.org/u/Kenny)\
**Post date:** [July 21, 2020, 7:16pm UTC](https://forums.freertos.org/t/pic32mz-time-delay-interference-using-freertos/10324/1 "2020-07-21T19:16:20Z")

</div>

I have written a function below which helps generate a time delay in micro-seconds. i plan to set time delays in the rage of 2-5 micro-seconds.  
void **Delay\_Us** (unsigned int Us)  
{

```
// Convert microseconds us into how many clock ticks it will take
Us *=(SYS_CLK_FREQ / 1000000) / 2; // Core Timer updates every 2 ticks

_CP0_SET_COUNT(0); // Set Core Timer count to 0

```

// Wait until Core Timer count reaches the number we calculated  
while (Us \> \_CP0\_GET\_COUNT());  
}  
I have FREERTOS configured in the code. I am wondering how the function defined will affect its total time duration if FreeRTOS functions vTaskStartScheduler() and xTaskCreate are executed independently during the small interval.  
Specifications:

Compiler: XC32,  
IDE: MPLAB X IDE v4.05  
Chip: PIC32MZ2048EFG144

I would appreciate some guidance.

Thank you,  
Kenny

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [July 22, 2020, 5:09am UTC](https://forums.freertos.org/t/pic32mz-time-delay-interference-using-freertos/10324/2 "2020-07-22T05:09:02Z")

</div>

I’m not sure how calling vTaskStartScheduler() could effect it because if that function has not been called then there will be no multithreading and calling Delay\_Us will result in the processor sitting in Delay\_Us until the delay is complete. In other words if the scheduler has not been started then FreeRTOS cannot impact the function.

After the scheduler has been started is a different matter because any task calling Delay\_Us can get preempted by any higher priority task, and therefore Delay\_Us will not be accurate - the delay time achieved will be either the desired microsecond delay if no context switches occur, or an unknown time if context switches do occur.
