Pages: [1]   Go Down
Author Topic: Error with interrupt count ?  (Read 488 times)
0 Members and 1 Guest are viewing this topic.
Montreal
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?


Code:

/*
 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;
   }
}

« Last Edit: June 20, 2013, 04:33:37 pm by zorglub123 » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25802
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Montreal
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Updated with code tag  smiley
Logged

Pages: [1]   Go Up
Jump to: