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?
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.
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.
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"...