# vTaskDelay help 

**URL:** <https://forums.freertos.org/t/vtaskdelay-help/1934>\
**Category:** Kernel\
**Created:** [October 15, 2014, 10:22am UTC](https://forums.freertos.org/t/vtaskdelay-help/1934 "2014-10-15T10:22:39Z")\
**Posts on this page:** 18\
**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:** [October 15, 2014, 10:22am UTC](https://forums.freertos.org/t/vtaskdelay-help/1934/1 "2014-10-15T10:22:39Z")

</div>

**[tomirtos](https://sourceforge.net/u/tomirtos/)** wrote on [Wednesday, October 15, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/586db12f/#f9a8):

Hello Everyone!

I’m probably missing something very obvious but I don’t understand that when I use the vTaskDelay function the delay is so short, no matter how large the number I put in the argument.

I have my internal oscillator set up to 80MHz and because dspic33fj128mc802 microcontroller needs two clock cycles for an instruction I define:

```auto
#define configCPU_CLOCK_HZ ( ( unsigned long ) 40000000 ) /* Fosc / 2 */

```

I also define the tick rate to be 100 Hz.

```auto
#define configTICK_RATE_HZ ( ( TickType_t ) 100 )

```

I use vTaskDelay like this:

```auto
void send_task(void *p)
{
while (1)
{

   //A=A+1; 
   unsigned char data='k';
   UART1PutChar(data);
    vTaskDelay(5000/portTICK_PERIOD_MS);
}
}

```

According to what I’ve read this should send a character in 5 second intervals. On the serial monitor I get a constant flood of the letter k, definately not 5 seconds apart.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 15, 2014, 11:24am UTC](https://forums.freertos.org/t/vtaskdelay-help/1934/2 "2014-10-15T11:24:49Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Wednesday, October 15, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/586db12f/#13e4):

First thing to see is if it thinks its sleeping for 500 ticks. If you call xTaskGetTickCount() before and after the call to vTaskDelay() is there a difference of 500 between the two returned values?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 15, 2014, 11:55am UTC](https://forums.freertos.org/t/vtaskdelay-help/1934/3 "2014-10-15T11:55:47Z")

</div>

**[tomirtos](https://sourceforge.net/u/tomirtos/)** wrote on [Wednesday, October 15, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/586db12f/#a2b7):

Thank you for the reply.  
I’ve checked and the difference is exactly 500 between the two values.

I get C=500 and B=0;

Also whats strange is that if I count in my head to 5 thats when the next halt will occur inside the debugger. So to me it seems that in the debugger it is working correctly, but when its not in debugger I get a constant flood of letters…

```auto
void send_task(void *p)
{
while (1)
{

   
   unsigned char data='k';
   UART1PutChar(data);

  B= xTaskGetTickCount();
    vTaskDelay(5000/portTICK_PERIOD_MS);
C=xTaskGetTickCount();
A=A+1;
}
}

```

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 15, 2014, 11:58am UTC](https://forums.freertos.org/t/vtaskdelay-help/1934/4 "2014-10-15T11:58:25Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Wednesday, October 15, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/586db12f/#98e1):

So the FreeRTOS code is working, it thinks its sleeping for the right time. Next I would try toggling a pin from the tick interrupt and looking at the toggle frequency on a scope to see how quickly the tick interrupt is actually running. You can use a tick hook function for that. So set configUSE\_TICK\_HOOK to 1 in FreeRTOSConfig.h, then write a function call vApplicationTickHook() that toggle the pin.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 15, 2014, 12:19pm UTC](https://forums.freertos.org/t/vtaskdelay-help/1934/5 "2014-10-15T12:19:59Z")

</div>

**[tomirtos](https://sourceforge.net/u/tomirtos/)** wrote on [Wednesday, October 15, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/586db12f/#8b4d):

I made the function so it toggles a led. I see the led is always red. In debug mode when stepping through the code I see the led beeing toggled when xTaskGetTickCoung(); gets called. I’ll check it with an oscillator soon.

```auto
void vApplicationTickHook(void)
{

    LATAbits.LATA0 ^=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:** [October 15, 2014, 12:30pm UTC](https://forums.freertos.org/t/vtaskdelay-help/1934/6 "2014-10-15T12:30:07Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, October 15, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/586db12f/#8b4d/5cd1):

Note that, because the LED is being toggled each time it is called, the  
frequency you see on the scope will be half the actual tick frequency  
because the scope is counting complete cycles, not just edges.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 15, 2014, 12:39pm UTC](https://forums.freertos.org/t/vtaskdelay-help/1934/7 "2014-10-15T12:39:28Z")

</div>

**[tomirtos](https://sourceforge.net/u/tomirtos/)** wrote on [Wednesday, October 15, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/586db12f/#5055):

I see very stange things on the oscilloscope…

I have impulses coming at about 17 ms intervals. Each impulse lasts 3.82 microsec with 3.313 Volts and then a very fast decrease to 0 Volts.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 15, 2014, 12:43pm UTC](https://forums.freertos.org/t/vtaskdelay-help/1934/8 "2014-10-15T12:43:48Z")

</div>

**[tomirtos](https://sourceforge.net/u/tomirtos/)** wrote on [Wednesday, October 15, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/586db12f/#1329):

When in debug mode and I have a break point set at A=A+1. Before the program hits the breakpoint I see a very nice square wave with 50Hz and 3.250 Volts

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 15, 2014, 12:48pm UTC](https://forums.freertos.org/t/vtaskdelay-help/1934/9 "2014-10-15T12:48:55Z")

</div>

**[tomirtos](https://sourceforge.net/u/tomirtos/)** wrote on [Wednesday, October 15, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/586db12f/#d538):

If I open the serial monitor while in debugger mode I receive the letter ‘k’ in the expected 5 second intervals. I have no idea what could be going on. After a few cycles the dspic resets itself.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 15, 2014, 12:50pm UTC](https://forums.freertos.org/t/vtaskdelay-help/1934/10 "2014-10-15T12:50:45Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Wednesday, October 15, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/586db12f/#bee0):

So the frequency is right (50Hz = 100Hz toggle) until the break point is hit? So it sounds like the first sleep is working ok, but sleeps after that (after the a=a+1 line) are not? Very strange.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 15, 2014, 12:56pm UTC](https://forums.freertos.org/t/vtaskdelay-help/1934/11 "2014-10-15T12:56:51Z")

</div>

**[tomirtos](https://sourceforge.net/u/tomirtos/)** wrote on [Wednesday, October 15, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/586db12f/#9c46):

Sorry I couldnt really understand your last sentence.

I’ll rephrase what I wrote. The thing is if I’m in debugging mode and measuring without any breakpoints I see the 50Hz wave all the time and receive the letters 5 seconds apart on my serial monitor.

The dspic reset itself because my watchdog timer wasnt turned off.

The strange phenomena still prevails though, that the whole thing only works while in debug mode.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 15, 2014, 4:26pm UTC](https://forums.freertos.org/t/vtaskdelay-help/1934/12 "2014-10-15T16:26:50Z")

</div>

**[tomirtos](https://sourceforge.net/u/tomirtos/)** wrote on [Wednesday, October 15, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/586db12f/#f82f):

I tried it with a very simple led\_task:

```auto

void led_task (void *p)
{

while (1)
    {
        LATAbits.LATA0 ^= 1;
        
        vTaskDelay(2000/portTICK_PERIOD_MS);

    }

}

```

The same thing is happening. The led is constantly turned on if not in debugger mode. When I am using debugger mode it stays on for two second and off for two seconds. If I measure the pin with the oscilloscope I get a nice square wave.

Can anybody tell me whats the difference between the debug mode and the normal running mode?

---

<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:** [October 15, 2014, 4:41pm UTC](https://forums.freertos.org/t/vtaskdelay-help/1934/13 "2014-10-15T16:41:04Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, October 15, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/586db12f/#f82f/8de4):

As far as FreeRTOS is concerned, there is no difference.

I think there is a difference to the Microchip tools though, as I  
believe when creating a debug build it compiles a debug stub into your  
code - but I don’t know the details. I do know that if you switch from  
a stand alone download to a debug download (without changing the  
compiler options) it does a clean build of the code so it is obviously  
doing something.

Assuming you have checked for the normal stack overflows, etc. I would  
look at the build options. Is the correct chip selected in the compiler  
tools (probably as I think you would get a warning when downloading  
otherwise)?

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 16, 2014, 12:01pm UTC](https://forums.freertos.org/t/vtaskdelay-help/1934/14 "2014-10-16T12:01:22Z")

</div>

**[tomirtos](https://sourceforge.net/u/tomirtos/)** wrote on [Thursday, October 16, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/586db12f/#e87b):

On a different forum others have pointed out that for example the debugger sets all the pins to digital. So there are some difference between debug and normal run.

Some other pin function might be in the way. For the UART module I dont use any pins that can be analog.

I have checked, the device is the correct device. I think this might help:

When I have this in the beginning of my main function :

```auto
  TRISAbits.TRISA0=0;
   LATAbits.LATA0=0;

```

On the oscilloscope I get spikes and a very fast damping. If in debug mode I get a 50Hz sqaure wave.

If I change LATA0 to 1.

```auto
  TRISAbits.TRISA0=0;
   LATAbits.LATA0=1;

```

This time when in normal run mode I see on the oscilloscope a 58 Hz square wave with a dutycycle of about 75%. The debug mode gives me again the desired 50Hz

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 16, 2014, 9:52pm UTC](https://forums.freertos.org/t/vtaskdelay-help/1934/15 "2014-10-16T21:52:56Z")

</div>

**[tomirtos](https://sourceforge.net/u/tomirtos/)** wrote on [Thursday, October 16, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/586db12f/#955f):

I’ve tried my code on a different computer. Also when I install the xc16 compiler for mplabx I installed the pro evaluation version of the compiler so it can compile with optimization 2 enabled.

This time I got an error for the previously working project: (working in debugger mode)

This project has \_\_ICD2RAM defined as a symbol to the linker."  
"Please go to the project properties dialog and in the x16-ld node, select the Symbols and Macros category. Then remove the \_\_ICD2RAM symbol

If I delete this the project compiles, but it wont be working.

So my question is what is \_\_ICD2RAM and what should I define instead of it?

---

<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:** [October 17, 2014, 7:56am UTC](https://forums.freertos.org/t/vtaskdelay-help/1934/16 "2014-10-17T07:56:15Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, October 17, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/586db12f/#955f/eb6e):

No idea I’m afraid - it is a question for a Microchip forum - but it  
does seem to point to a project configuration issue.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 17, 2014, 5:49pm UTC](https://forums.freertos.org/t/vtaskdelay-help/1934/17 "2014-10-17T17:49:34Z")

</div>

**[tomirtos](https://sourceforge.net/u/tomirtos/)** wrote on [Friday, October 17, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/586db12f/#c7b8):

My debugger problem got solved.

I actually took it to one of the teachers at Budapest University of Technology, so someone could look at it in person.

After a fair amount of time we realised if we turn the code optimization down to 0, the debugger mode will also reset itself periodically.

The function called applicationidlehook and in that it another function called vCoRoutineSchedule(); was the cause of the problem. These remained in my project because of the demo application also had these in them and I was using the demo setup as an example for my own projects. I thought I had to include these in order to make my projects work.

The microcontroller reset itself for some reason when entering vCoRoutineSchedule. We disabled configUSE\_IDLE\_HOOK and configUSE\_CO\_ROUTINES. After that my application was working in release mode and in debug mode.

This also left us with a question but it doesn’t matter now because the problem is solved, but it is still interesting.

Why did the debugger mode only reset when compiled in 0 optimization? When in optimization 2, why did the debugger solve the problem that the microcontroller couldn’t?

Thank you for all your help!!!

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [October 19, 2014, 3:59pm UTC](https://forums.freertos.org/t/vtaskdelay-help/1934/18 "2014-10-19T15:59:16Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Sunday, October 19, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/586db12f/#e71b):

Hi Heri,

> Why did the debugger mode only reset when compiled in 0 optimization?

configUSE\_IDLE\_HOOK : your routine will be called from the idle task which  
has a minimum of stack space. When code is not being optimised, every local  
variable and return address will be declared on stack. Could that be an  
explanation?  
It has been said that the Linux kernel can’t even run when compiled with -O0.

Hein
