I am having an issue with correct interrupt behaviour on a Teensy 4.1. I have boiled it down to a small piece of code.
Basically I am attaching an interrupt to a RISING edge of a pin. Using a function generator I send a block of 10 pulses to the pin. I have confirmed with an oscilloscope that the pin sees 10 pulses. However, the interrupt routine is called 16 times!
Any ideas or help would be appreciated.
Here is a screen shot of the input signal, blue signal is the input pin on the teensy 4.1
Here is the code
Basically I am attaching an interrupt to a RISING edge of a pin. Using a function generator I send a block of 10 pulses to the pin. I have confirmed with an oscilloscope that the pin sees 10 pulses. However, the interrupt routine is called 16 times!
Any ideas or help would be appreciated.
Here is a screen shot of the input signal, blue signal is the input pin on the teensy 4.1
Here is the code
Code:
#include <arduino.h>
#include <elapsedMillis.h>
#define ENCA 11
#define ENCB 12
#define LED 13
bool lastEncA;
bool lastEncB;
long countC;
void setup() {
// put your setup code here, to run once:
Serial.begin(57600);
Serial.printf("hello world\n\r");
pinMode(ENCA, INPUT);
pinMode(ENCB, INPUT);
pinMode(LED,OUTPUT);
attachInterrupt(digitalPinToInterrupt(ENCA), encoder_shaft_moved, RISING);
}
const int cntDir = 1;
volatile long rCount = 0;
volatile bool currDir = 0;
void loop() {
static elapsedMillis timeSinceLastChange=0;
static elapsedMillis timeSinceLastDisplay=0;
static elapsedMillis ledTimer=0;
static bool lastLed=0;
static bool sentOne = false;
static long lastDisplayCnt = 0;
if (ledTimer > 300) {
if (lastLed == 1) lastLed = 0;
else lastLed = 1;
digitalWrite(LED,lastLed);
ledTimer = 0;
}
if (rCount != lastDisplayCnt)
timeSinceLastChange = 0;
if ((((long)timeSinceLastChange) > 10) || (((long)timeSinceLastDisplay) > 2000)) {
if ((lastDisplayCnt != rCount) || (((long)timeSinceLastDisplay) > 2000)) {
if ((sentOne== false) || (((long)timeSinceLastDisplay) > 2000))
Serial.printf("%lu: %ld %s\n\r",millis(), rCount,currDir == 1 ?"In":"out");
sentOne = true;
timeSinceLastDisplay = 0;
lastDisplayCnt = rCount;
}
else sentOne = false;
}
}
void encoder_shaft_moved() {
elapsedMillis timeSinceLastChange=0;
bool newA, newB;
// put your main code here, to run repeatedly:
newA = digitalRead(ENCA);
newB = digitalRead(ENCB);
rCount++;
return;
}
Last edited by a moderator: