# Creating tasks

**URL:** <https://forums.freertos.org/t/creating-tasks/6175>\
**Category:** Kernel\
**Created:** [September 28, 2016, 6:25am UTC](https://forums.freertos.org/t/creating-tasks/6175 "2016-09-28T06:25:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 28, 2016, 6:25am UTC](https://forums.freertos.org/t/creating-tasks/6175/1 "2016-09-28T06:25:11Z")

</div>

**[rclub123](https://sourceforge.net/u/rclub123/)** wrote on [Wednesday, September 28, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/3cb92f7b/#85f8):

How to create two task such that one task runs at 0.5millisecond and another task runs at 100 millisecond using RTOS?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 28, 2016, 9:17am UTC](https://forums.freertos.org/t/creating-tasks/6175/2 "2016-09-28T09:17:27Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Wednesday, September 28, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/3cb92f7b/#cfb0):

Use vTaskDelayUntil() [http://www.freertos.org/vtaskdelayuntil.html](http://www.freertos.org/vtaskdelayuntil.html) see the example on the page. 0.5ms is very fast though best to trigger from an interrupt.[http://www.freertos.org/vTaskNotifyGiveFromISR.html](http://www.freertos.org/vTaskNotifyGiveFromISR.html)

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 28, 2016, 10:59am UTC](https://forums.freertos.org/t/creating-tasks/6175/3 "2016-09-28T10:59:28Z")

</div>

**[rclub123](https://sourceforge.net/u/rclub123/)** wrote on [Wednesday, September 28, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/3cb92f7b/#cb9c):

Thanks for your reply… i followed that link you provided… it is in the ticks… means it perfoms the task after 10 ticks… but i want this in milliseconds… i want task to execute after 0.5 milliseconds and 100 millisecond… how could i achieve that?  
My clock freq and tick rate is as below

```auto
#define configCPU_CLOCK_HZ ((unsigned long)96000000)

#define configTICK_RATE_HZ ((portTickType)1000)

```

edit:  
After reading some forums i understood about tick… so configTICK\_RATE\_HZ=1000 means in 1 second OS will generate 1000 ticks… but i dont know how to get 0.5 milliseconds (because there is nothing like 0.5 ticks for 1000kHZ)… i think i need to change configTICK\_RATE\_HZ… Am i right?

---

<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:** [September 28, 2016, 12:02pm UTC](https://forums.freertos.org/t/creating-tasks/6175/4 "2016-09-28T12:02:55Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, September 28, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/3cb92f7b/#cb9c/b9bf):

If configTICK\_RATE\_HZ is 1000, then the tick frequency will be  
1000Hz/1KHz. So one tick every ms, and a timing granularity of 1ms (so  
specifying a 1ms delay will give you between 0.0001ms and 0.9999ms).  
You cannot specify a 0.5ms delay time with a 1KHz tick rate, and it is  
not recommended to go over 1KHz. You can however trigger tasks from an  
interrupt, and a link to the recommended way of doing that has already  
been provided.

Normally you would use the pdMS\_TO\_TICKS() macro to convert milliseconds  
into tick periods, but that won’t work if the tick rate is greater than  
1KHz.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 29, 2016, 4:38am UTC](https://forums.freertos.org/t/creating-tasks/6175/5 "2016-09-29T04:38:54Z")

</div>

**[rclub123](https://sourceforge.net/u/rclub123/)** wrote on [Thursday, September 29, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/3cb92f7b/#a7ff):

Thank you for your valuable suggestions… i have 1 last question…  
i have created 2 tasks like below

```auto
void vTask0(void *pvParameters)
{
	TickType_t xNextWakeTime;
	const TickType_t xFrequency = 1;
	xNextWakeTime = xTaskGetTickCount();

	while(1)
	{
		if (xSemaphoreTake(xGPIOmutex, (portTickType)10) == pdTRUE)
		{
          adc_val[0]=ADC_read();
	      xSemaphoreGive(xGPIOmutex);
	      vTaskDelayUntil(&xNextWakeTime, xFrequency);
		}
        printf("channel 0 = %d \n", adc_val[0]);
	}
}

```

```auto
void vTask1(void *pvParameters)
{
	TickType_t xNextWakeTime;
	const TickType_t xFrequency = 10;

	xNextWakeTime = xTaskGetTickCount();

	  while (1)
	  {

		  if (xSemaphoreTake(xGPIOmutex, (portTickType)10) == pdTRUE)
		  {
            adc_val[1]=ADC1_read();
	        xSemaphoreGive(xGPIOmutex);
	        vTaskDelayUntil(&xNextWakeTime, xFrequency);
		  }
          printf("channel 1 = %d \n", adc_val[1]);
	  }
}

```

if i set xFrequency=1 in task 1, the above code(both task 1 and task 2) will execute only once… if i put xFrequency=5 in task1 then both will work fine… what might be the problem why it is not working if set xFrequency=1?

---

<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:** [September 29, 2016, 5:54am UTC](https://forums.freertos.org/t/creating-tasks/6175/6 "2016-09-29T05:54:24Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, September 29, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/3cb92f7b/#a7ff/3ab4):

I would expect it to work with xFrequency set to 1. What happens if you  
take out the call to printf()? Without printf() you might not be able  
to tell if it is working or not, so you can replace the printf() with a  
pin toggle that you can then view on a scope, or replace the printf()  
with a simple variable increment so when you pause the debugger you can  
view the variable’s value (use a different variable for each task and  
ensure the variable is declared volatile).

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 29, 2016, 12:02pm UTC](https://forums.freertos.org/t/creating-tasks/6175/7 "2016-09-29T12:02:13Z")

</div>

**[rclub123](https://sourceforge.net/u/rclub123/)** wrote on [Thursday, September 29, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/3cb92f7b/#b626):

I tried what you said… i took variable ‘a’ for task1 and ‘b’ for task1 i add these two variables to watch list… as per as my understanding of the code value of ‘b’ should be 10 multiple of ‘a’… i.e. if b=1 then ‘a’ should be 10, if b=2, ‘a’ should be 20… but in my case it is random… or may be “almost 5 multiple”(hope you understood)…
