attachInterrupt with CHANGE mode in Teensy 4.0 problem

Status
Not open for further replies.

ric

Member
I am using Teensy 4.0 and I have problems using attachInterrupt with CHANGE mode.
I want an input pin (10) to generate an interrupt when it changes status. In the interrupt service routine (my_isr) I toggle an output pin (3) to see, by my oscilloscope, the timing of the interrupt service routine reactions. I would expect to see the output pin (3) toggling HIGH and LOW at each change of the interrupt pin (10). In reality, on the oscilloscope, I see not just one toggling but several toggling for each status change of the interrupt pin.
It seems to me that one change of the input pin is generating more than one interrupt, rather than generating just one interrupt as I would expect.
I tried studying the manual of the micro and I have found in the GPIO chapter that all digital pins can generate interrupts "on rising" or "on falling" or "on low value" or "on high value". I don't find the possibility of generating interrupts "on changing value".
So I have the following 2 questions:
1) Is there any error in my software below?
2) How can it be possible the use of attachInterrupt with CHANGE mode if the micro has not this possibility on its digital pins?

//--------------------------------------------------------------------------------------
#define PIN_IN 10
#define PIN_OUT 3

void setup()
{
pinMode(PIN_IN , INPUT_PULLUP);
pinMode(PIN_OUT , OUTPUT);
attachInterrupt (digitalPinToInterrupt (PIN_IN), my_isr, CHANGE );
}

void my_isr ()
{
digitalWrite (PIN_OUT, HIGH);
int dummy = 0;
for (int i = 0; i < 1000; i++) dummy ++; // just to create a delay
digitalWrite (PIN_OUT, LOW);
}

void loop() {
}
 
By far the most likely explanation is the interrupt pin is changing multiple times. How are you driving the
PIN_IN ? If by a mechanical switch or button you will see bouncing.

use delayMicroseconds() or delayNanoseconds() rather than a loop to generate a delay
(redundant loops may get optimized away depending on compiler settings)
 
The input is coming from an inductive sensor through an optocoupler. On the oscilloscope, I didn't see bouncing.
I was using the same circuit configuration with Arduino Leonardo and the interrupt pin was working perfectly. Now we want to migrate to Teensy 4.0, because of its better performances, but we have found this unexpected problem. I thought it could be a problem of clearing the interrupt status bit but I don't find the "on change" interrupts in the GPIO chapter of the micro.
 
Your "delay" is not working.
Code:
for (int i = 0; i <  1000; i++) dummy ++;    // just to create a delay
The compiler optimizes this completely away, as it does nothing.
 
A slowly varying signal will also create multiple transitions, logic inputs require fast edges for correct operation.
 
A slowly varying signal will also create multiple transitions, logic inputs require fast edges for correct operation.

thank you Mark, you are right !!!.
With fast varying signal, as interrupt input, the problem disappears:

Thank you so much !!!

Cattura.jpg
 
Typical fast processors like the iMXRT1062 you need to keep edges really fast - for instance unless the GPIO
is set to hysteresis mode the datasheet says a max transition time of 25ns is required - that's pretty severe.
 
Typical fast processors like the iMXRT1062 you need to keep edges really fast - for instance unless the GPIO
is set to hysteresis mode the datasheet says a max transition time of 25ns is required - that's pretty severe.

Is there any way to set a GPIO input to hysteresis mode in Teensy 4.0 ?
 
This is a very interesting and useful matter to avoid multiple false interrupts from external signals. Which is the simplest code to activate the "hysteresis" feature on an interrupt input pin in Teensyduino for Teensy 4.0?
 
Status
Not open for further replies.
Back
Top