Falling edge counting on a timer

Hello,

I'm trying to use Arduino Due to count number of falling edges (and eventually rising as well) every 26 milliseconds from a rising edge. I have a function generator on pin 22 that generates a pulse of width of 2ms every 20ms.
I'm having some problems with interrupts and timers as I'm new to them. Essentially, I use a hardware interrupt on pin 22 to track every falling edge and a timer interrupt that resets the number of falling edges every 26 millisec; however, I need to synchronize my timer interrupt with my rising edges. I thought I could make an interrupt that would trigger on a rising edge and start a counter, but that doesn't seem to work. I appreciate any advice or help.

Also I'm using DueTimer library to help me with timer interrupts.

Thank you :slight_smile:

#include <DueTimer.h>

volatile int i=0;
volatile int countUp=0;
volatile int countDown=0;

void setup() 
{
  
  Serial.begin(9800);  
  pinMode(22, INPUT);
  Timer3.attachInterrupt(counterReset).start(26000);
  attachInterrupt(22, interrupt3, RISING);
  detachInterrupt(22);
  attachInterrupt(22, interrupt1, FALLING);
  
  //attachInterrupt(22, interrupt2, RISING);
  
}

void interrupt1()
{
  countDown++; 
}

void interrupt2()
{
  countUp++;
}

void interrupt3()
{
  Timer3.start();
  i++;
}

void counterReset()
{
  countDown=0;
}


void loop() 
{
  Serial.print("Falling: ");
  Serial.print(countDown);
  Serial.print(" Rising: ");
  Serial.print(countUp);
  
  Serial.print(" ms: ");
  Serial.print(millis());
  Serial.print("    ");
  Serial.print(i);
  

  
  delay(20);
}

All you need is a Timer Capture (TC_CMR in capture mode).

See an example in this thread, reply #3:

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