Timing interrupt

Hello there! :slight_smile:

I'm using an Arduino DUE and I'm trying to write a code with timer interrupts!
I want that every 27ms my code does something, starting from when I give an input as a string in the serial monitor.

From internet I found:

void TC8_Handler()
{
TC_GetStatus(TC2,2);
  int a = 3;
  int b = 2;
  int c = a+b;
  Serial.println(c);
}

and this:

void setup(){
Serial.begin(9600);
const int delayPeriod = 42000000/27; 
  pmc_set_writeprotect(false); // Power Management Controller Enable or disable write protect of PMC registers.
  pmc_enable_periph_clk((uint32_t)TC8_IRQn); // Enable the specified peripheral clock.
  TC_Configure(TC2, 2,TC_CMR_WAVE|TC_CMR_WAVSEL_UP_RC|TC_CMR_TCCLKS_TIMER_CLOCK1);
  TC_SetRC(TC2, 2, delayPeriod);
  TC2->TC_CHANNEL[2].TC_IER=TC_IER_CPCS;
  TC2->TC_CHANNEL[2].TC_IMR=TC_IMR_CPCS;
  TC2->TC_CHANNEL[2].TC_IDR=~TC_IER_CPCS;
  TC_Start(TC2, 2);
  NVIC_EnableIRQ(TC8_IRQn); // Enable interrupt.
}

but I can't understand anything... Could someone teach me how to do it please?

Thank you in advance!

A much better approach would be to to use millis() to track elapsed time and then do whatever every 27ms.

You should check out the BlinkWithoutDelay example (File->examples->02.Digital->BlinkWithoutDelay) so see how you track elapsed time without blocking your code.

Your attached sketch is not complete so it will not compile.

blh64:
A much better approach would be to to use millis() to track elapsed time and then do whatever every 27ms.

You should check out the BlinkWithoutDelay example (File->examples->02.Digital->BlinkWithoutDelay) so see how you track elapsed time without blocking your code.

Your attached sketch is not complete so it will not compile.

As I supposed. :frowning:

The fact is that my professor wants that I use a time interrupt instead of a millis(), because he doesn't want that I add an if cicle. What do you think?

If there's no way to use an interrupt, I'll ask him if I can use millis() as you said.

kitama01:
but I can't understand anything...

The chip's Timer Counter (TC) capabilities are described in the datasheet that you can download from here: https://www.microchip.com/wwwproducts/en/ATSAM3X8E

Could you please explain it to me line by line?

The rest of my code is the following:

void loop(){
  String stringOne = "";
  if (Serial.available()){ 
      while (Serial.available()>0){ 
      char a = Serial.read();            // Stores current byte
      stringOne += String(a);            // Append current Byte to message String
      delay(10);
    }
if (stringOne == "A"){
  noInterrupts();
  Serial.print("Hello");
}
    }
}

I hope I didn't miss any bracket.

The output will be, as I write "A" in the serial monitor, "H", and not the all "Hello"...

Did you read the datasheet I linked (Chapter 36)?

Yes, but I didn't understand much.. I'm not an expert, I'm approaching Arduino for the first time...

I solved my problem,

I delete the NVIC_EnableIRQ(TC8_IRQn) part, and I added NVIC_DisableIRQ(TC8_IRQn):

if (stringOne == "A"){
  NVIC_DisableIRQ(TC8_IRQn);
  Serial.print("Hello");
}

now the program prints "Hello" as a whole.