Calling ISR while Using TImer Counter

I am unable to get the interrupts to work on my Arduino Due while using the Timer Counter to generate a Waveform. I tried to enable both RA and RC Compare, but neither is being triggered. I would like the frequency generated to remain at the 1.7MHz it is currently at, but I need to implement some instructions on the edges.

Any help would be appreciated!

/******************************************************************************/
/***               1.7MHz/ 50% Duty cycle PWM thru TIOA0                      ***/
/******************************************************************************/
#define led_pin 13
int interruptCtr = 1;
void setup() {
  Serial.begin(115200);
  pinMode(led_pin, OUTPUT);
  Serial.println("code began");

/*************  Timer Counter 0 Channel 0 to generate PWM pulses thru TIOA0  ************/
    pmc_set_writeprotect(false);     // disable write protection for pmc registers
    
    TC0->TC_CHANNEL[0].TC_IER=TC_IER_CPAS; //enable RA  compare interrupt
    TC0->TC_CHANNEL[0].TC_IER=TC_IER_CPCS; //enable RC compare interrupt 
   
    TC0->TC_CHANNEL[0].TC_IDR=~TC_IER_CPAS;  // IDR = interrupt disable register
    TC0->TC_CHANNEL[0].TC_IDR=~TC_IER_CPCS;  // IDR = interrupt disable register

    TC0->TC_CHANNEL[0].TC_IMR=TC_IMR_CPAS; //enable RA  compare interrupt
    TC0->TC_CHANNEL[0].TC_IMR=TC_IMR_CPCS; //enable RC compare interrupt     
  
  
  PMC->PMC_PCER0 |= PMC_PCER0_PID27;                      // Timer Counter 0 channel 0 IS TC0, TCO power ON
  PMC->PMC_PCER0 |= PMC_PCER0_PID12;                      // PIOB power ON, page 38
 
  PIOB->PIO_PDR |= PIO_PDR_P25;
  PIOB->PIO_ABSR |= PIO_ABSR_P25;                        // PB25 is driven by the TC, peripheral type B, page 858

  TC0->TC_CHANNEL[0].TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK1  // MCK/2, clk on rising edge
                              | TC_CMR_WAVE               // Waveform mode
                              | TC_CMR_WAVSEL_UP_RC        // UP mode with automatic trigger on RC Compare

                              | TC_CMR_ACPA_CLEAR          // Clear TIOA0 on RA compare match
                              
                              | TC_CMR_ACPC_SET;           // Set TIOA0 on RC compare match

  TC0->TC_CHANNEL[0].TC_RC = 24;  //<*********************  Frequency = (Mck/2)/TC_RC  = 1.7 MHz
  TC0->TC_CHANNEL[0].TC_RA = 12;  //<********************   Duty cycle = (TC_RA/TC_RC) * 100 = 50 %

  TC0->TC_CHANNEL[0].TC_CCR = TC_CCR_SWTRG | TC_CCR_CLKEN; // Software trigger TC0 counter and enable
 
    NVIC_EnableIRQ(TC0_IRQn);
  interrupts();
}


ISR(TIMER0_COMPA_vect)
{
  digitalWrite(led_pin,digitalRead(led_pin)^1);  //toggle led pin
  Serial.println("interrupted RA");
  
}


ISR(TIMER0_COMPC_vect)
{
  digitalWrite(led_pin,digitalRead(led_pin)^1);  //toggle led pin
  Serial.println("interrupted RC");
}

void loop() {
 
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

hi,
I am not so familiar with your coding on register level. But I read about not using Serial.print() statements within interrupt routines. An alternative is to set a flag within the interrupt routine and check this flag outside for printing.

There is a special library for timers on a Due. Perhaps this is easier to use.

good luck

SupArdu

A few thoughts about that code:

1/You are mixing low level Sam3x code with AVR interrupt vectors, see the issue ?

2/ The Timer Counter interrupt handler can't get easily triggered above 1.6 MHz ( 42 MHz / 25 = 1.6 MHz).

3/ If you want to detect TIOA0 rising edge and do set/clear some pins above 1.6 MHz, you can poll TC status register with TC_SR bits in loop(). With this method, you can leverageTIOA0 rising edge frequency above 5 MHz.

Here is an example sketch showing this method (reply #11):

https://forum.arduino.cc/index.php?topic=535306.0