chipKIT® Development Platform

Inspired by Arduino™

[RESOLVED] Multi Timer interrupt issue - Max32

Created Wed, 21 Mar 2012 15:19:36 +0000 by sfeltan


sfeltan

Wed, 21 Mar 2012 15:19:36 +0000

Hi,

I try to play with two timers to generate different signals on dedicated output. The issue is that I see only one interrupt, never both of them. I have checked both interrupts runing alone, it's ok but as long as both of them are implemented, only one shows up...

This is my code:

#include <plib.h>
#include <Wiring.h>
#include <Time.h>

#define PositiveOutput50KHz   13  //Pin 13 for 50KHz output
#define NegativeOutput50KHz   12  //Pin 13 for 50KHz output
#define KHZOutput             11
#define NumberOfSamples       40  
#define DutyInMicroSecs       100
#define HardwareDelay          2
#define SAMPLE_RATE 16000
#define SAMPLE_RATE2 15000 


float CalculatedDuty[NumberOfSamples] = {};
float x = 0;
int i = 0;
int debugOneToZero = 0;
int debug = 0;
float result1, result2;
float tmp;
int value = 0;
  
void setup()
{
    Serial.begin(9600);   
  pinMode(PositiveOutput50KHz, OUTPUT); 
  pinMode(NegativeOutput50KHz, OUTPUT); 
  pinMode(KHZOutput, OUTPUT); 
  pinMode(22, OUTPUT);
 
  
  ConfigIntTimer5(T5_ON | T5_INT_PRIOR_3);
  OpenTimer5(T5_ON, F_CPU / SAMPLE_RATE);

  ConfigIntTimer3(T3_ON | T3_INT_PRIOR_3);
  OpenTimer3(T3_ON, F_CPU / SAMPLE_RATE2);

}

void loop()
{
  //do nothing
}



extern "C"
{

void __ISR(_TIMER_5_VECTOR,ipl3) playSample(void)
{
  
  mT5ClearIntFlag();  // Clear interrupt flag
  
  digitalWrite( KHZOutput, ( digitalRead( KHZOutput )^1 ) ); 
  Serial.print("Interrupt 1 \n\r");
}

void __ISR(_TIMER_3_VECTOR,ipl3) Sample(void)
{
  mT3ClearIntFlag();  // Clear interrupt flag
  
  digitalWrite( 22, ( digitalRead( 22 )^1 ) ); 
  Serial.print("Interrupt 2 \n\r");
 
}




} // end extern "C"

Does any one of you has an idea why? I have tried to play with the SAMPLERATE and ipl3 values, doesn't help.

Regards,

Laurent


Ryan K

Wed, 21 Mar 2012 20:17:57 +0000

Hello,

Move the ClearIntFlag functions to the bottom of each ISR and it should work. I just tried it on my Max and it worked fine. I would suggest that you remove the print statements in your ISRs after you finish debugging.

Best Regards, Ryan K


sfeltan

Thu, 22 Mar 2012 08:33:21 +0000

Hi,

Thank's, this was the issue and I have found out yesterday afternoon by going home. I shoulf have updated the topic. The prints are to be removed, it's just to check ithe interrupts are working.

For others, the modification to get both interrupts looks like this:

void __ISR(_TIMER_5_VECTOR,ipl3) playSample(void)
{
  digitalWrite( KHZOutput, ( digitalRead( KHZOutput )^1 ) );
  Serial.print("Interrupt 1 \n\r");
  mT5ClearIntFlag();  // Clear interrupt flag
}

void __ISR(_TIMER_3_VECTOR,ipl3) Sample(void)
{
  digitalWrite( 22, ( digitalRead( 22 )^1 ) );
  Serial.print("Interrupt 2 \n\r");
  mT3ClearIntFlag();  // Clear interrupt flag
}

Next question is: Why I get only one of them? With such issue then I would expect that none of them were working.


Ryan K

Thu, 22 Mar 2012 16:16:23 +0000

Hello,

The reason why only one ISR is ever entered is that since you clear the interrupt flag at the beginning of the ISR and that Serial.print() is a blocking function. By the time Serial.print() is finished the flag has been set again and it just reenters the ISR after exiting. The program is almost always in the ISR.

Best Regards, Ryan K


sfeltan

Thu, 22 Mar 2012 17:39:57 +0000

Ok, thanks a lot for the explanations.