Hello forum,
Just bought the Teensy 4 for my Bachelor Project, where I'm designing a digital synthesizer.
I'm setting up interrupt rutines for handling rotary encoders using the Teensy 4.
I'm fairly new to Teensy and the Arduino Framework, as I've always used AVR and Atmel Studio.
Arduino has the easy "attachInterrupt()" function that I'm using. The code is as simple as it gets:
The usage of Serial in the interrupt is just for test purposes, where I've also tried flagging the counter etc. to make better practice of the interrupt handler. This isn't the issue.
I just exported the program to an Arduino Nano and it worked perfectly.
I've also made a debounce filter in hardware to really eliminate any errors. I've scoped it, and it looks exactly as I expect it to.
Yet the Teensy 4 doesn't react on the interrupt as I expected. I've tried logging the state of the pin, and it even interrupts while being LOW state. Usually the interrupt is activated 3-4 times - even though I have the 5ms software delay/filtering. And the counter is usually incremented and decremented at each turn of the rotary encoder.
I've also increased the software delay/filtering and it's kinda working sometimes - but that's not useful for me at all.
I'm not interested in interrupting both pins and thereby comparing states. As said, this works exactly as expected with an Arduino Nano.
Am I overseeing something? Is it a bug?
Is it possible to use "avr/interrupt.h" instead? Can't find any PCINT in the datasheet.
Thanks, Hans
Just bought the Teensy 4 for my Bachelor Project, where I'm designing a digital synthesizer.
I'm setting up interrupt rutines for handling rotary encoders using the Teensy 4.
I'm fairly new to Teensy and the Arduino Framework, as I've always used AVR and Atmel Studio.
Arduino has the easy "attachInterrupt()" function that I'm using. The code is as simple as it gets:
Code:
#include <Arduino.h>
volatile int counter = 0;
unsigned long lastInterrupt = 0;
unsigned long currentInterrupt;
#define INTERRUPT_PIN 2
void rotaryEncoder()
{
currentInterrupt = millis();
if (currentInterrupt - lastInterrupt < 5)
return;
if (digitalRead(4))
{
counter++;
}
else
{
counter--;
}
lastInterrupt = currentInterrupt;
Serial.print("Counter: ");
Serial.println(counter);
}
void setup() {
// put your setup code here, to run once:
pinMode(4, INPUT);
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), rotaryEncoder, RISING);
}
void loop() {
}
The usage of Serial in the interrupt is just for test purposes, where I've also tried flagging the counter etc. to make better practice of the interrupt handler. This isn't the issue.
I just exported the program to an Arduino Nano and it worked perfectly.
I've also made a debounce filter in hardware to really eliminate any errors. I've scoped it, and it looks exactly as I expect it to.
Yet the Teensy 4 doesn't react on the interrupt as I expected. I've tried logging the state of the pin, and it even interrupts while being LOW state. Usually the interrupt is activated 3-4 times - even though I have the 5ms software delay/filtering. And the counter is usually incremented and decremented at each turn of the rotary encoder.
I've also increased the software delay/filtering and it's kinda working sometimes - but that's not useful for me at all.
I'm not interested in interrupting both pins and thereby comparing states. As said, this works exactly as expected with an Arduino Nano.
Am I overseeing something? Is it a bug?
Is it possible to use "avr/interrupt.h" instead? Can't find any PCINT in the datasheet.
Thanks, Hans