# changing tick frequency

**URL:** <https://forums.freertos.org/t/changing-tick-frequency/5877>\
**Category:** Kernel\
**Created:** [March 30, 2016, 7:10am UTC](https://forums.freertos.org/t/changing-tick-frequency/5877 "2016-03-30T07:10:45Z")\
**Posts on this page:** 8\
**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:** [March 30, 2016, 7:10am UTC](https://forums.freertos.org/t/changing-tick-frequency/5877/1 "2016-03-30T07:10:45Z")

</div>

**[kyromaster](https://sourceforge.net/u/kyromaster/)** wrote on [Wednesday, March 30, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/88b29c03/#8ec8):

I’m rather new to FreeRTOS and I have a problem.  
I’m using an atmega328p with FreeRTOS 8.3.2. with 16MHz and 1000 ticks per second.  
All my delays are like (x / portTICK\_PERIOD\_MS) where x is the delay in ms.  
But when I lower the tick value, e.g. to 100 ticks per second, everything runs much faster.  
I’ve read some topics about tick timing but I’m still not sure what’s wrong here.  
Can you help me?  
Thanks a lot 🙂

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 30, 2016, 8:38am UTC](https://forums.freertos.org/t/changing-tick-frequency/5877/2 "2016-03-30T08:38:47Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Wednesday, March 30, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/88b29c03/#fb9e):

> 8.3.2

guess you mean 8.2.3

> (x / portTICK\_PERIOD\_MS)

Use pdMS\_TO\_TICKS(x) instead.

The tick frequency can only be changed at compile time. If you use the pdMS\_TO\_TICKS or portTICK\_PERIOD\_MS macros for all timing then you should be good for tick speeds up to 1KHz.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 30, 2016, 12:30pm UTC](https://forums.freertos.org/t/changing-tick-frequency/5877/3 "2016-03-30T12:30:05Z")

</div>

**[kyromaster](https://sourceforge.net/u/kyromaster/)** wrote on [Wednesday, March 30, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/88b29c03/#67bf):

I tried with a very small example:

```auto
#include "Arduino.h"

#include "arduinofreertoscompatibility.h"

#include "digitalflip.hpp"

#define LED_PIN 13

void blinkThread(void* arg) {

	while (true) {
		ARDUINO_START();
		digitalFlip(LED_PIN);
		ARDUINO_END();
		vTaskDelay(pdMS_TO_TICKS(1000));
	}
}

int main() {
	init();
	initVariant();
#if defined(USBCON)
	USBDevice.attach();
#endif
	Serial.begin(9600);
	pinMode(LED_PIN, OUTPUT);
	Serial.println(F("Creating blink thread"));
	if (xTaskCreate(blinkThread, NULL, configMINIMAL_STACK_SIZE * 2, NULL, 2, NULL) != pdPASS) {
		Serial.println(F("Couldn't create blink thread"));
		exit(-1);
	}
	Serial.println(F("Setup finished, starting FreeRTOS scheduler"));
	vTaskStartScheduler();
}

```

this should blink 1 time per second, but blinks much faster.

---

<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:** [March 30, 2016, 12:34pm UTC](https://forums.freertos.org/t/changing-tick-frequency/5877/4 "2016-03-30T12:34:19Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, March 30, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/88b29c03/#67bf/c3da):

I’m afraid I’m not familiar with using FreeRTOS in an Arduino  
environment, perhaps the following post will help?  
[http://interactive.freertos.org/entries/109124706-Arduino-AVR-FreeRTOS](http://interactive.freertos.org/entries/109124706-Arduino-AVR-FreeRTOS)

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 30, 2016, 2:51pm UTC](https://forums.freertos.org/t/changing-tick-frequency/5877/5 "2016-03-30T14:51:54Z")

</div>

**[kyromaster](https://sourceforge.net/u/kyromaster/)** wrote on [Wednesday, March 30, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/88b29c03/#ce8f):

the ARDUINO\_START() and ARDUINO\_END() macros disable and enable the scheduler. that’s what I found as recommendation.  
strange thing is that when I change to:

```auto
void blinkThread(void* arg) {

    while (true) {
        ARDUINO_START();
        digitalFlip(LED_PIN);
        ARDUINO_END();
        vTaskDelay(1000);
    }
}

```

everything works fine. so I guess there’s something wrong with the pdMS\_TO\_TICKS macro.  
I configured freertos with:

```auto
#define configCPU_CLOCK_HZ ( ( unsigned long ) F_CPU )
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 )

```

---

<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:** [March 30, 2016, 2:59pm UTC](https://forums.freertos.org/t/changing-tick-frequency/5877/6 "2016-03-30T14:59:13Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, March 30, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/88b29c03/#ce8f/d390):

Its possible you are simply getting a numeric overflow as it is likely  
your tick count value is only 16-bits. You could set  
configUSE\_16\_BIT\_TICKS to 0 in FreeRTOSConfig.h, which will then make  
the ticks 32-bit, at some processing overhead expense as you only have  
an 8-bit device.

Also, try declaring a const variable that has the delay you need, then  
you will see what the value actually is. Also it will be more efficient  
as the pdMS\_TO\_TICKS() macro will only evaluate once, rather than each  
time you call vTaskDelay().

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 30, 2016, 3:24pm UTC](https://forums.freertos.org/t/changing-tick-frequency/5877/7 "2016-03-30T15:24:42Z")

</div>

**[kyromaster](https://sourceforge.net/u/kyromaster/)** wrote on [Wednesday, March 30, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/88b29c03/#49ac):

I changed configUSE\_16\_BIT\_TICKS to 0.  
and the function to:

```auto
void blinkThread(void* arg) {

	while (true) {
		const TickType_t delayInTicks = pdMS_TO_TICKS(1000);
		Serial.println(delayInTicks);
		vTaskDelay(delayInTicks);
	}
}

```

I get the correct number, e.g. 1000 with 1000 ticks per second and 100 with 100 ticks per second. but the loop speed is still faster the less ticks I have per second.  
With 1000 ticks the timing is right, also in other projects.  
If it helps I can upload the project as an AVR Studio solution.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 31, 2016, 4:21pm UTC](https://forums.freertos.org/t/changing-tick-frequency/5877/8 "2016-03-31T16:21:53Z")

</div>

**[kyromaster](https://sourceforge.net/u/kyromaster/)** wrote on [Thursday, March 31, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/88b29c03/#31b8):

would be nice if someone could test the attached project
