# Changint the timer in the PIC32 Port

**URL:** <https://forums.freertos.org/t/changint-the-timer-in-the-pic32-port/3007>\
**Category:** Kernel\
**Created:** [June 22, 2009, 3:57pm UTC](https://forums.freertos.org/t/changint-the-timer-in-the-pic32-port/3007 "2009-06-22T15:57:23Z")\
**Posts on this page:** 2\
**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:** [June 22, 2009, 3:57pm UTC](https://forums.freertos.org/t/changint-the-timer-in-the-pic32-port/3007/1 "2009-06-22T15:57:23Z")

</div>

**[gadkins](https://sourceforge.net/u/gadkins/)** wrote on [Monday, June 22, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/857814f7/#b53b):

I was just wondering if anybody had managed to successfully change the timer from timer 1 to another timer (2 or 3). I have tried altering the Port files and have changed the set-up routine (see below) to use timer 2, I have also looked at the ISR ASM code (complete novice here) and can’t see how I would change this if it is indeed need changing, watching the interrupt control reg, you see the timer sets setup correctly, and after the interrupt the reg value goes from 0x8102 to 0x8100 (when operating with timer one reg reads 0x8012 when entering the ISR, this is I believe expected).

The effect when changed is that FreeRTOS does not register the tasks and as such even basically loops only within the RTOS code. I believe that that the timer is working because it will debug within it, but I must be missing something within the rest of the port to cause this behaviour.

I know this seems a bit OTT, but I want to use the 64 pin PIC and need an external pin to drive a timer/counter so have to use timer 1 for this.

Sample from Port.asm

&nbsp;&nbsp;&nbsp; .set&nbsp;&nbsp;&nbsp; nomips16  
&nbsp;&nbsp;&nbsp;&nbsp; .set&nbsp;&nbsp;&nbsp;&nbsp; noreorder  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp; .extern pxCurrentTCB  
&nbsp;&nbsp;&nbsp;&nbsp; .extern vTaskSwitchContext  
&nbsp;&nbsp;&nbsp;&nbsp; .extern vPortIncrementTick  
&nbsp;&nbsp;&nbsp; .extern xISRStackTop  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp; .global vPortStartFirstTask  
&nbsp;&nbsp;&nbsp; .global vPortYieldISR  
&nbsp;&nbsp;&nbsp; .global vT1InterruptHandler

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/

&nbsp;&nbsp;&nbsp;&nbsp; .section&nbsp;&nbsp;&nbsp;&nbsp; .FreeRTOS, "ax", @progbits  
&nbsp;&nbsp;&nbsp;&nbsp; .set&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; noreorder  
&nbsp;&nbsp;&nbsp; .set&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; noat  
&nbsp;&nbsp;&nbsp;&nbsp; .ent&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vT1InterruptHandler  
&nbsp;&nbsp;&nbsp;  
vT1InterruptHandler:

&nbsp;&nbsp;&nbsp; portSAVE\_CONTEXT

&nbsp;&nbsp;&nbsp; jal&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vPortIncrementTick  
&nbsp;&nbsp;&nbsp; nop

&nbsp;&nbsp;&nbsp; portRESTORE\_CONTEXT

&nbsp;&nbsp;&nbsp; .end vT1InterruptHandler

Clears the interrupt

&nbsp;&nbsp;&nbsp; /\* Clear the interrupt in the interrupt controller. \*/  
&nbsp;&nbsp;&nbsp; la&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s6, IFS0CLR  
&nbsp;&nbsp;&nbsp; addiu&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s4, zero, 2  
&nbsp;&nbsp;&nbsp; sw&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s4, (s6)

&nbsp;&nbsp;&nbsp; jal&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vTaskSwitchContext  
&nbsp;&nbsp;&nbsp; nop

Setup code

/\* Place the prototype here to ensure the interrupt vector is correctly installed. \*/  
extern void \_\_attribute\_\_( (interrupt(ipl1), vector(\_TIMER\_2\_VECTOR))) vT1InterruptHandler( void );

/\*  
\* Setup a timer for a regular tick.  
\*/  
void prvSetupTimerInterrupt( void )  
{  
const unsigned portLONG ulCompareMatch = ( (configPERIPHERAL\_CLOCK\_HZ / portTIMER\_PRESCALE) / configTICK\_RATE\_HZ ) - 1;

&nbsp;&nbsp;&nbsp; OpenTimer2( ( T2\_ON | T2\_PS\_1\_8 | T2\_SOURCE\_INT ), ulCompareMatch );  
&nbsp;&nbsp;&nbsp; ConfigIntTimer2( T2\_INT\_ON | configKERNEL\_INTERRUPT\_PRIORITY );

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 28, 2009, 4:07pm UTC](https://forums.freertos.org/t/changint-the-timer-in-the-pic32-port/3007/2 "2009-06-28T16:07:18Z")

</div>

**[marmur99](https://sourceforge.net/u/marmur99/)** wrote on [Sunday, June 28, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/857814f7/#b53b/5332):

Either way, there’s a small bug in a port.c file for PIC32:

void prvSetupTimerInterrupt( void )  
{  
const unsigned portLONG ulCompareMatch = ( (configPERIPHERAL\_CLOCK\_HZ / portTIMER\_PRESCALE) / configTICK\_RATE\_HZ ) - 1;

&nbsp;&nbsp;&nbsp; OpenTimer1( ( T1\_ON | T1\_PS\_1\_8 | T1\_SOURCE\_INT ), ulCompareMatch );

&nbsp;&nbsp;&nbsp; ConfigIntTimer1( T1\_INT\_ON | configKERNEL\_INTERRUPT\_PRIORITY );  
}

According to Microchip libraries documentation, OpenTimerX expects 16bit value as a second parameter thus maximum value is 65535.

@PeripheralClockHz == 80000000 (80MHz) maximum configTICK\_RATE\_HZ would be then 152 Hz. If you use a value lower than 152, then the real tick rate will be different than expected.

I guess it would be good to give people chance to use 1/64 divider.
