Timer interrupt of 500nsec to be generated in Arduino Due

Hi,

How to generate Timer interrupt of 500nsec in arduino due. can anyone help to achieve this?
Sample code will be much helpful to me. Thanks in advance.

Regards,
V. Prakash

There are numerous example sketches in the DUE sub forum for Timer Counters programming.

An example sketch to trigger an interrupt at a 1 MHz frequency:

/******************************************************************/
/*       Timer Counter 2 Channel 2, namely TC8, 1 MHz frequency   */
/******************************************************************/

void setup() {

  pinMode(LED_BUILTIN, OUTPUT);
  tc_setup();

}

void loop() {

}

void tc_setup() {

  PMC->PMC_PCER1 |= PMC_PCER1_PID35;                      // TC8 power ON : Timer Counter 2 channel 2 IS TC8 - see page 38

  TC2->TC_CHANNEL[2].TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK1  // MCK/2 = 42 M Hz, clk on rising edge
                              | TC_CMR_WAVE               // Waveform mode
                              | TC_CMR_WAVSEL_UP_RC;      // UP mode with automatic trigger on RC Compare
 
  TC2->TC_CHANNEL[2].TC_RC = 42;  //<*********************  Frequency = (Mck/2)/TC_RC  Hz

  TC2->TC_CHANNEL[2].TC_IER = TC_IER_CPCS;                // Interrupt on RC compare match
  NVIC_EnableIRQ(TC8_IRQn);
  TC2->TC_CHANNEL[2].TC_CCR = TC_CCR_SWTRG | TC_CCR_CLKEN; // Software trigger TC2 counter and enable
}

void TC8_Handler() {

  static uint32_t Count;

  TC2->TC_CHANNEL[2].TC_SR;                               // Read and clear status register
  if (Count++ == 1000000) {
    Count = 0;
    PIOB->PIO_ODSR ^= PIO_ODSR_P27;                       // Toggle LED every 1 Hz
  }
}

Hi,

Thanks for the reply.

/******************************************************************/
/*       Timer Counter 2 Channel 2, namely TC8, 1 MHz frequency   */
/******************************************************************/

void setup() {

  pinMode(LED_BUILTIN, OUTPUT);
  tc_setup();

}

void loop() {

}

void tc_setup() {

  PMC->PMC_PCER1 |= PMC_PCER1_PID35;                      // TC8 power ON : Timer Counter 2 channel 2 IS TC8 - see page 38

  TC2->TC_CHANNEL[2].TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK1  // MCK/2 = 42 M Hz, clk on rising edge
                              | TC_CMR_WAVE               // Waveform mode
                              | TC_CMR_WAVSEL_UP_RC;      // UP mode with automatic trigger on RC Compare
 
  TC2->TC_CHANNEL[2].TC_RC = 42;  //<*********************  Frequency = (Mck/2)/TC_RC  Hz

  TC2->TC_CHANNEL[2].TC_IER = TC_IER_CPCS;                // Interrupt on RC compare match
  NVIC_EnableIRQ(TC8_IRQn);
  TC2->TC_CHANNEL[2].TC_CCR = TC_CCR_SWTRG | TC_CCR_CLKEN; // Software trigger TC2 counter and enable
}

void TC8_Handler() {

  static uint32_t Count;

  TC2->TC_CHANNEL[2].TC_SR;                               // Read and clear status register
  if (Count++ == 1000000) {
    Count = 0;
    PIOB->PIO_ODSR ^= PIO_ODSR_P27;                       // Toggle LED every 1 Hz
  }
}

Changing the below line to the attached code:

TC2->TC_CHANNEL[2].TC_RC = 21;  //<*********************  Frequency = (Mck/2)/TC_RC  Hz

will generate 500nsec timer interrupt??

Changing the below line to the attached code:

TC2->TC_CHANNEL[2].TC_RC = 1;  //<*********************  Frequency = (Mck/2)/TC_RC  Hz

will generate 23.8nsec timer interrupt??

Please let us know.

Regards,
V. Prakash

What prevents you from trying ?

Hi,

I am having only Arduino Mega board. I had ordered the Arduino due board. It is yet to be received.

Regards,
V. Prakash

I think you are unlikely to get much of a meaningful isr to execute within 500ns. That’s only 42 clock cycles, and each flash memory access can take up to 5, iirc...

(plus at least 12 cycles to get in and out of the ISR (24 cycles total.))

I though when you were using the Mega2560, you were after 1us interrupts. When did it become 500ns?

You seem very determined to try to run ISRs at the very edge of what is actually possible, and far, far, more often than is a good idea, and you've yet to give us a convincing argument as to why...

Hi,
Thanks for the reply. Yes you are right.
I tried in atmega 2560 board.
In 10 usec slot, my code itself taking 18 usec extra.

So I trying in Arduino due board. My requirement is 1 usec slot. From Ur post, I understood tat, in 1usec, only 84 clock cycles are there.

I cannot restrict my code within 84 clock cycle.

So I decided to change my scan rate to 10 usec slot. I had tested with Arduino mega board in 10 usec slot, my code is taking 18usec extra. So I am deciding to go for due board.

Will it possible to do in Arduino board. Will this extra 18usec will be reduced in this due board??

Regards,
V.prakash

Post the code you want to execute within the time lapse.

I would suggest reading this article - let the hardware do the work, not interrupts!

http://www.kerrywong.com/2014/09/21/on-arduino-due-pwm-frequency/