Counting Pulse in High Frequencies with Arduino DUE

Hi Everybody,

I'm a noob in arduino and this is my first post.
I've searched a lot in this forum and in google, but I can't find a solution to my problem.

In this project, I'm trying to measure the number of pulses in a 1 MHz square wave.
So, for that, I created a square wave in the digital port 7 and read the same wave in the digital port 13.

volatile unsigned int counter = 0 ;
const int z = 13;

void setup() {
  
Serial.begin(115200);
delay(100);

int32_t mask_PWM_pin = digitalPinToBitMask(7);
REG_PMC_PCER1 = 1<<4;               // activate clock for PWM controller
REG_PIOC_PDR |= mask_PWM_pin;  // activate peripheral functions for pin (disables all PIO functionality)
REG_PIOC_ABSR |= mask_PWM_pin; // choose peripheral option B   
REG_PWM_CLK = 0;                     // choose clock rate, 0 -> full MCLK as reference 84MHz
REG_PWM_CMR6 = 0<<9;             // select clock and polarity for PWM channel (pin7) -> (CPOL = 0)
REG_PWM_CPRD6 = 80;                // initialize PWM period -> T = value/84MHz (value: up to 16bit), value=80 -> 1.05MHz
REG_PWM_CDTY6 = 40;                // initialize duty cycle, REG_PWM_CPRD6 / value = duty cycle, for 80/40 = 50%
REG_PWM_ENA = 1<<6;               // enable PWM on PWM channel (pin 7 = PWML6)

delay(10);

pinMode(z, INPUT);
attachInterrupt(z, zrising, FALLING);
}

void loop() {

 Serial.println(counter);
 delay(10);
  
}

void zrising() {
	counter++;
}

The wave has the right frequency but the number of pulse is incorrect.
Lowering the frequency until 100KHz, the digital pin 13 has a correct count.

So my questions are:
1 - What's the maximum sampling frequency when I use the attachInterrupt?
2 - Someone of you have another solution with/without interrupts?

Thanks :slight_smile:

  1. About 153,000 interrupts per second for your example.
  2. Use an external divider (or counter IC) with the divided output connected to your interrupt pin.

Or make your counter a long and not an int.

Mark

dlloyd:

  1. About 153,000 interrupts per second for your example.
  2. Use an external divider (or counter IC) with the divided output connected to your interrupt pin.

Thanks for your answer dlloyd

1 - Can you explain me, why I have 153000 interrupts per second?
It's because of the 1 MHz wave in the digital pin 7?

holmes4:
Or make your counter a long and not an int.

Mark

Thanks Mark

I agree with you, and i changed this variable in my algorithm but, this variable doesn't speed up my interrupt

I have a Due and tested your code. However, the interrupt latency for the Due is best described and tested here:
http://forum.arduino.cc/index.php?topic=157135.0
https://github.com/manitou48/DUEZoo

I'd use an timer in counter mode, reducing the interrupt frequency by 2^8 or 2^16 (depending on timer size).

DrDiettrich:
I'd use an timer in counter mode, reducing the interrupt frequency by 2^8 or 2^16 (depending on timer size).

Have you an example Diettrich??

I'm not familiar with the Due. Provided it has timer/counter hardware:

  1. set up the timer in counter mode
  2. make it interrupt on overflow, add your ISR for counting the overflows
  3. finally the counter contains the low byte/word of the count, your counter the high part

Try this.
See, if it works for you