# Minor timer setup bugfix for PIC24/dsPIC port

**URL:** <https://forums.freertos.org/t/minor-timer-setup-bugfix-for-pic24-dspic-port/3594>\
**Category:** Kernel\
**Created:** [October 6, 2012, 8:12pm UTC](https://forums.freertos.org/t/minor-timer-setup-bugfix-for-pic24-dspic-port/3594 "2012-10-06T20:12:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dnadler](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/dnadler/32/35_2.png) [@dnadler](https://forums.freertos.org/u/dnadler)\
**Post date:** [October 6, 2012, 8:12pm UTC](https://forums.freertos.org/t/minor-timer-setup-bugfix-for-pic24-dspic-port/3594/1 "2012-10-06T20:12:47Z")

</div>

**[dnadler](https://sourceforge.net/u/dnadler/)** wrote on [Saturday, October 06, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/9eebc31a/#6e06):

Hi - My blinking light was running at half speed, and I assumed I’d set up the microprocessor clock wrong. After a couple hours checking all the clock configuration options, I found a small 2x error in the port.c timer setup… Here’s the fix:

```
    // DRN: Timer clock input is system clock >>> DIVIDED BY 2 <<<
    // DRN: See DS39704A-page 14-6: bit 1 TCS:
    // DRN: Timerx Clock Source Select bit: 0 = Internal clock (FOSC/2)
    // DRN: line below fixed to reflect this...
    const unsigned long ulCompareMatch = ( ( (configCPU_CLOCK_HZ/2) / portTIMER_PRESCALE ) / configTICK_RATE_HZ ) - 1; // DRN
    // DRN: Was: const unsigned long ulCompareMatch = ( ( configCPU_CLOCK_HZ / portTIMER_PRESCALE ) / configTICK_RATE_HZ ) - 1;

```

Note 1: the CPU clock on PIC24F is typically 32MHz, but the instruction clock is always 1/2 this value. The timer input takes the CPU clock/2.  
Note 2: The CPU clock can be reduced in “Doze Mode” power-saving - this is not provided for in FreeRTOS.

Hope this is helpful !  
Best Regards, Dave

---

<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 6, 2012, 8:22pm UTC](https://forums.freertos.org/t/minor-timer-setup-bugfix-for-pic24-dspic-port/3594/2 "2012-10-06T20:22:27Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Saturday, October 06, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/9eebc31a/#b1c2):

That is curious - I’m sure I would have noticed that.&nbsp; Can you tell me which PIC part number you are using.&nbsp; Thanks.

Regards.

---

<div class="post-metadata">

**Author:** ![dnadler](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/dnadler/32/35_2.png) [@dnadler](https://forums.freertos.org/u/dnadler)\
**Post date:** [October 10, 2012, 12:36pm UTC](https://forums.freertos.org/t/minor-timer-setup-bugfix-for-pic24-dspic-port/3594/3 "2012-10-10T12:36:42Z")

</div>

**[dnadler](https://sourceforge.net/u/dnadler/)** wrote on [Wednesday, October 10, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/9eebc31a/#9373):

I’m using the PIC24FJ32GB004.  
The documentation for the timer module for PIC24 family is here:[http://ww1.microchip.com/downloads/en/DeviceDoc/39704a.pdf](http://ww1.microchip.com/downloads/en/DeviceDoc/39704a.pdf)  
See section 14.3 Register 14-1: TxCON: Type A Time Base Control bit 1  
The clock definitions for these parts are a bit confusing:  
CPU clock is 32MHz, also feeds peripherals (excepting USB).  
Instruction clock is always half this at 16MHz (unless further divided in DOZE low-power mode).  
Hope that helps,  
Thanks,  
Best Regards, Dave
