chipKIT® Development Platform

Inspired by Arduino™

Timer2 or Timer3 for Compare Match Interrupt?

Created Tue, 18 Dec 2012 17:09:12 +0000 by RoboNegro


RoboNegro

Tue, 18 Dec 2012 17:09:12 +0000

Greetings all,

I am attempting a basic compare match interrupt using Timer2 and the output compare module1 (OCM1). Everything works fine when I use Timer2 as the time base for OCM1. However when I try to change the time base to Timer3 NO interrupt is generated. In the PIC32 manual it states that only Timers2 and Timer3 can be used for a compare match interrupt, so I thought there would be no problem using Timer3. My code is below. Any suggestions?

Here is the code that works using Timer2:

//The following code illustrates how to define initial OC1 pin state for the
//output compare toggle mode of operation in 16-bit mode

#include <plib.h>

void setup()
{ 
  Serial.begin(9600);
  pinMode(12, OUTPUT);
  
  //configure module for OC1 pin low, toggle high
  OC1CON = 0x0001;
  //enable OC1 module
  OC1CONSET = 0x8000;
  
  //configure timer2 for prescaler of 1
  T2CON = 0x0000;
  //turn off OC1 while doing setup
  OC1CON = 0x0000;
  //configure for compare toggle mode
  OC1CON = 0x0003;
  
  //initialize compare register 1 for an interrupt frequency of 30 kHz
  OC1R = 0xA6B;
  //set period
  PR2 = 0xA6B;
  
  //configure interrupt
  
  //clear the OC1 interrupt flag
  IFS0CLR = 0x0040;
  //enable OC1 interrupt
  IEC0SET =0x040;
  //set OC1 interrupt priority to 7, the highest level
  IPC1SET = 0x001C0000;
  
  //set subpriority to 3, maximum
  IPC1SET = 0x00030000;
  
  //start timer2
  T2CONSET = 0x8000;
  //enable OC1
  OC1CONSET = 0x8000;
  	 
  delay(1);  // Slight delay avoids false trigger at start.
}

void loop()
{
}

extern "C"
{

void __ISR(_OUTPUT_COMPARE_1_VECTOR,ipl7) OC1_IntHandler(void)	
  {	 
    // Clear interrupt flag
    IFS0CLR = 0x0040;  
 
    //write pin12 LOW
    LATACLR = 100;     
    //write pin12 HIGH
    LATASET = 100;
  }
}

...and here is the code that doesn't work:

//The following code illustrates how to define initial OC1 pin state for the
//output compare toggle mode of operation in 16-bit mode

#include <plib.h>

void setup()
{ 
  Serial.begin(9600);
  pinMode(12, OUTPUT);
  
  //configure module for OC1 pin low, toggle high
  OC1CON = 0x0001;
  //enable OC1 module
  OC1CONSET = 0x8000;
  
  //configure timer3 for prescaler of 1
  T3CON = 0x0000;
  //turn off OC1 while doing setup
  OC1CON = 0x0000;
  //configure for compare toggle mode
  OC1CON = 0x0003;
  
  //initialize compare register 1 for an interrupt frequency of 30 kHz
  OC1R = 0xA6B;
  //set period
  PR3 = 0xA6B;
  
  //configure interrupt
  
  //clear the OC1 interrupt flag
  IFS0CLR = 0x0040;
  //enable OC1 interrupt
  IEC0SET =0x040;
  //set OC1 interrupt priority to 7, the highest level
  IPC1SET = 0x001C0000;
  
  //set subpriority to 3, maximum
  IPC1SET = 0x00030000;
  
  //start timer3
  T3CONSET = 0x8000;
  //enable OC1
  OC1CONSET = 0x8000;
  	 
  delay(1);  // Slight delay avoids false trigger at start.
}

void loop()
{
}

extern "C"
{

void __ISR(_OUTPUT_COMPARE_1_VECTOR,ipl7) OC1_IntHandler(void)	
  {	 
    // Clear interrupt flag
    IFS0CLR = 0x0040;  
    
    //write pin12 LOW
    LATACLR = 100;     
    //write pin12 HIGH
    LATASET = 100;
  }
}

GeneApperson

Thu, 20 Dec 2012 00:11:48 +0000

It looks like you aren't actually telling the OC1 unit to use Timer3. Bit 3 (OCTSEL) of OCxCON selects which timer to use. I don't see anywhere that you are setting that bit.

When you set OC1CON to 0, you are clearing the bit, which selects Timer2. You have to set the bit to select Timer3.

Try adding OC1CONSET = 0x0008 as part of initializing the output compare unit.

Gene Apperson Digilent