Error with interrupt count ?

Hello,

Here is a small sketch that simply increment a counter on every rising edge for a specific pin using interrupt service routine.
The sketch runs just fine using a UNO with train pulse up to 18 kHz.

Running that sketch on the DUE reports more pulses than the actual number of pulses received. Even when using a train frequency as low as 1 kHz. This is very strange than more pulses that the real number is reported.

We have no doubt on the actual number of pulses received since we are using other external counters to monitor that.

Any comment on what happen ?

/*
 This sketch is implementing a pulse train counter.
 
 A running pulse is input via pin #5 (DUE) or pin #2(UNO)
 
 An interrupt service routine (ISR) is configured to be run for each rising edge.
 
 ISR is counting the pulses
 
 Host can retrieve the count value via the "G" command.
 Host can reset the countert value via the "R" command.
 
 Runs perfectly well using UNO with pulse train frequency up to 18 kHz
 
 Using the DUE, even at low pulse frequency like 100 Hz, the DUE is counting more pulses than the actual number 
 of pulse input.
 
 */
#include <stdlib.h>

#define CMD_SIZE 64     // Host communication buffer
char command[CMD_SIZE]; // Host communication buffer
int  inputString =0;    // Host data string size

volatile unsigned int CounterValue = 0; // the Counter variable

int ISRPin = 5;     // DUE: use value 5 corresponding to input line pin #5,
                    // UNO: use value 0 corresponding to input line pin #2,

//---------------------------------------------------------------
void setup() 
{
  Serial.begin(57600);
  for (int i= 0; i < CMD_SIZE; i++)
      command[i] = '\0';

   pinMode(ISRPin, INPUT);
  
   attachInterrupt(ISRPin, CounterISR, RISING); 
}
//---------------------------------------------------------------
void loop() 
{
  serialEvent(); 
}
//---------------------------------------------------------------
void CounterISR()
{
  CounterValue++;
}
//---------------------------------------------------------------
void PrintPluse()
{
   char output[64];
   unsigned int l = CounterValue;
   sprintf(output,"%u",l);
   Serial.println(output);
}
//---------------------------------------------------------------
void commandpaser()
{
  switch(command[0])
  {
    case 'R':
    {
         ResetCount();
       break;
    }
    case 'G':
    {
       PrintPluse();
       break;
    }
  }
}
//---------------------------------------------------------------
void ResetCount()
{
  noInterrupts();
  CounterValue = 0;
  Serial.println("OK,Reset");
  interrupts();
}
//---------------------------------------------------------------
void serialEvent() 
{
   while (Serial.available()) 
   {
     char inChar = (char)Serial.read(); 
     if (inChar == '\n') 
     {
       
        command[inputString++] = inChar;
        commandpaser();
        inputString = 0;
     } 
     else
       command[inputString++] = inChar;
   }
}

Much as I love italics, can you see why we ask you to use code tags when posting code?

Updated with code tag :slight_smile: