chipKIT® Development Platform

Inspired by Arduino™

Timer Interrupt UNO32 Frustration

Created Wed, 15 Jan 2014 08:39:09 +0000 by cobusmetnc


Wed, 15 Jan 2014 08:39:09 +0000

I have been struggling to setup a timer interrupt but to no avail. Please tell me what is wrong with this code. I have measured the pins with an oscilloscope and nothing happens. FYI: These are code segments.

void timer(void)
    OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_256, T2_TICK);



extern "C"{
  void __ISR(_TIMER_2_VECTOR, ipl2) Timer2Handler(void)

    intFlag1 = 1;


I do not receive any error. I use MPIDE and have included the following files.

#include <p32xxxx.h>
#include <plib.h>
#include <p32mx320f128h.h>

Thank You


Wed, 15 Jan 2014 10:33:34 +0000

Are you using any libraries that could be also using the same interrupt? Such as the SoftPWMServo library?

Much of what you are doing in that code is already done by the chipKIT core, such as enabling interrupts, and switching to multi-vector mode. You are probably better off scrapping all that plib rubbish and just manipulating the registers directly. You only need to configure the timer and the timer's interrupt, and nothing else.


Wed, 15 Jan 2014 11:50:57 +0000

I tried implementing the following code and it would seem that the timer is only counting to the prescribed period and then it stops completely. I send the info over serial and it only counts up to 2500 (the period), but never resets back to zero. If I comment out the interrupt the timer stop short of 2500 and freezes. This is very strange shouldn't the counter reset on its own?

Here is my code :

#include <plib.h>

int LED5 = 43;

void setup() {
pinMode(LED5, OUTPUT); pinMode(3, OUTPUT); Serial.begin(115200);

//timer2 T2CON =0x0; // stop timer and clear control register T2CONSET = 0x0070; //set prescaler to 256 TMR2 = 0x0; // clear timer register PR2 = 0x09C4; // load period register IPC2SET = 0x0000000D; // set priority level to 3 and subpriority level to 1 15625 IFS0CLR = 0x00000100; // clear timer interupt status flag IEC0SET = 0x00000100; // enable timer interrupts T2CONSET = 0x8000; // start timer }

void loop() { digitalWrite(LED5, HIGH);

Serial.print(ReadTimer2()); Serial.print('\n');


extern "C"{ void __ISR(_TIMER_2_VECTOR,IPL3AUTO) T2_Interrupt_ISR(void) {

if(digitalRead(3) == LOW)

{ digitalWrite(3, HIGH); } else { digitalWrite(3, LOW); } IFS0CLR = 0x00000100; } }

At least there is something happening. So thanks for that one.


Wed, 15 Jan 2014 13:47:33 +0000

I have change everything to Timer1 and the timer is resetting but the interrupt is still not triggering.

Is my interrupt code wrong maybe?

extern "C"{
void __ISR(_TIMER_1_VECTOR,IPL3) T1_Interrupt_ISR(void)

    if(digitalRead(3) == LOW)
    digitalWrite(3, HIGH);
    digitalWrite(3, LOW);
    IFS0CLR = 0x00000100;


Wed, 15 Jan 2014 14:09:14 +0000

Try adding this to the interrupt.

mT1ClearIntFlag();  // Clear interrupt flag


Wed, 15 Jan 2014 14:16:19 +0000

Nope did not work, but thanks for the effort.


Fri, 17 Jan 2014 02:45:35 +0000

here's a program I play with to get my mind thinking in interupts

#include &lt;plib.h&gt;
#define SYS_FREQ (80000000)
#define PB_DIV 2
#define T5_TICK 10000  // T5 TICK = FOSC/PB_DIV/PRESCALE/4
#define TX_MOSFET BIT_4  // TX MOSFET is connected to RD4 pin10
int m;

void setup()
  PORTSetPinsDigitalOut(IOPORT_D, BIT_4);  // TX pulse mosfet control
  OpenTimer5(T5_ON | T5_SOURCE_INT | T5_PS_1_8, T5_TICK);
  ConfigIntTimer5(T5_INT_ON | T5_INT_PRIOR_3);

void loop()

extern "C"
  void __ISR(_TIMER_5_VECTOR,ipl3) Tx_timing(void)
      for(m=0;m&lt;1800;m++){       // TX pulse width  100 = 5us, 600 = 30.00us 
  mT5ClearIntFlag();  // Clear Flag

mpide 0023

put your scope on pin 10

it may help,

how were you triggering your interupt ?



Fri, 17 Jan 2014 07:52:43 +0000

Thank you all for your help! I got it working. Basically everything froze the moment the interrupt was called through the timer. The solution came down to explicitly enabling the interrupt through the ConfigIntTimer() function. Strange thing is I did enable it through setting the appropriate registers. Oh well que sera sera.