Timer Interrupt in Arduino Due

Hello Folks,

I want to use Timer interrupt in Arduino Due and I am referring 2manyProjects Tutorial and found this code, Please find the code below:

// These are the clock frequencies available to the timers /2,/8,/32,/128
// 84Mhz/2 = 42.000 MHz
// 84Mhz/8 = 10.500 MHz
// 84Mhz/32 = 2.625 MHz
// 84Mhz/128 = 656.250 KHz
//
// 42Mhz/44.1Khz = 952.38
// 10.5Mhz/44.1Khz = 238.09 
// 2.625Hmz/44.1Khz = 59.5
// 656Khz/44.1Khz = 14.88 // 131200 / 656000 = .2 (.2 seconds)

// 84Mhz/44.1Khz = 1904 instructions per tick
const int led_pin = 13;
int state = false;
int interruptCtr = 1;
int S = 0;

void setup()
{
  pinMode(led_pin, OUTPUT);
  /* turn on the timer clock in the power management controller */
  pmc_set_writeprotect(false);		 // disable write protection for pmc registers
  pmc_enable_periph_clk(ID_TC7);	 // enable peripheral clock TC7

  /* we want wavesel 01 with RC */
  TC_Configure(/* clock */TC2,/* channel */1, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK4); 
  TC_SetRC(TC2, 1, 131200);
  TC_Start(TC2, 1);

  // enable timer interrupts on the timer
  TC2->TC_CHANNEL[1].TC_IER=TC_IER_CPCS;   // IER = interrupt enable register
  TC2->TC_CHANNEL[1].TC_IDR=~TC_IER_CPCS;  // IDR = interrupt disable register

  /* Enable the interrupt in the nested vector interrupt controller */
  /* TC4_IRQn where 4 is the timer number * timer channels (3) + the channel number (=(1*3)+1) for timer1 channel1 */
  NVIC_EnableIRQ(TC7_IRQn);
}

void loop()
{
  // do nothing timer interrupts will handle the blinking;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// INTERRUPT HANDLERS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TC7_Handler()
{
  // We need to get the status to clear it and allow the interrupt to fire again
  TC_GetStatus(TC2, 1);
  state = !state;
  digitalWrite(led_pin, state);

  if( interruptCtr++ >= 6 )
    {
     interruptCtr = 1;
     S = !S; // are we flashing S or O
     if( S ) // set time till next interrupt
        TC_SetRC(TC2, 1, 131200); // 131200 / 656000 = .2 seconds
     else
        TC_SetRC(TC2, 1, 656000); // 656000/ 656000 = 1 second
    }
}

I have basic understanding of Timer Interrupt. Now when I was referring the tutorial many doubts came into my mind

  1. In the above project why it has selected /128 (I know that 84Mhz/128=656Khz)and why not /2,/8,/32?

2)"// 656Khz/44.1Khz = 14.88 // 131200 / 656000 = .2 (.2 seconds)" I mean how does he got this calculation what does this means?

  1. In TC_Configure Which conditions are taken into consideration selection of TC0,TC1,TC2 and their respective channels and what are the criterias for Clocks are assigned to Timer Counters?

Please Help.

I was actually referencing the same tutorial not too long ago as well. I'm close to being in the same boat as you, so I do not have the exact answers to your questions. Take what I say however you see fit.

  1. I would feel there has to be some sort of power saving choosing /128 over /2 while resolution would be lost. For every 1 tick in the counter, a /128 setup would pass about 1.52us each while the /2 would pass 23.8ns.
    [84Mhz/128=656Khz=1.52us resolution and 84Mhz/2=42Mhz=23.8ns resolution]

  2. That is some extremely odd math. The big kicker here is the 44.1Khz value they are using, and there is no mention where this comes from. The 131200 / 656000 doesn't make sense as well and I feel it is just a radio. These are the "tick" values found earlier in the page, which you would not obtain units of seconds after dividing together.

I would say, if you know when you want the timer interrupt to fire, you should pick a divider that will best suit your needs. For example lets say we want the timer to fire every .7 seconds, we would have the following tick counts to choose from:
84Mhz/2 = 42.000 MHz * .7sec = 29400000 ticks
84Mhz/8 = 10.500 MHz * .7sec = 7350000 ticks
84Mhz/32 = 2.625 MHz * .7sec = 1837500 ticks
84Mhz/128 = 656.250 KHz * .7sec = 459375 ticks

This is just a random example, and depending on the desired time you might have some rounding errors or the counter may have trouble counting up to the desired ticks with an overflow.

3)Sadly, I do not have an answer for this one. We might have to reference the Atmel Datasheet to see if there are any changes in current ratings and what not.

I hope I could give you some form of help.