Interrupt Falling & Rising always triggered
Hello
I have the problem that no matter if I use RISING or FALLING on interrupts, an interrupt is always triggered.
An encoder 2Bit Gray Code is connected to CH5 & CH6. The interrupt is triggered via CH6.
Generally I just can't get the encoder to run. Because I can not query the inputs in the interrupt routine and if I set a flag the program is too slow.
For control I display "INT2_RISE" on the screen to see when an interrupt was triggered.
//___________________________________________
// Pin Setup Digital
//-------------------------------------------
Indio.digitalMode(CH_1, INPUT_MASKED);
Indio.digitalMode(CH_2, INPUT_MASKED);
Indio.digitalMode(CH_3, INPUT_MASKED);
Indio.digitalMode(CH_4, INPUT_MASKED);
Indio.digitalMode(CH_5, INPUT_MASKED);
Indio.digitalMode(CH_6, INPUT);
//___________________________________________
// Pin Setup Interrupt
//-------------------------------------------
attachInterrupt(8, Encode_RISING, RISING);
void Encode_RISING() {
INT1_enco=1;
SerialUSB.println("INT2_RISE");
}
loop:
if(INT1_enco == 1) //INT_RISING
{
detachInterrupt(8);
Channel5= Indio.digitalRead(5);
Channel6= Indio.digitalRead(6);
if (Indio.digitalRead(6) == HIGH)
{
if (Channel5 == HIGH)
{
position_enco++;
SerialUSB.println("UP_1");
}
else{
position_enco--;
SerialUSB.println("down_1");
}
}
else{
if (Channel5 == LOW)
{
position_enco++;
SerialUSB.println("UP_2");
}else{
position_enco--;
SerialUSB.println("down_2");
}
}
INT1_enco=0;
attachInterrupt(8, Encode_RISING, RISING)
}
Hi, the interrupt on MCU pin D8 is triggered by a change on any digital channel (CH1-8). As far as i know, indeed any digital channel change triggers a FALLING and RISING edge, so as far as i understand it does not matter which one you use, RISING or FALLING.
I suggest another approach for your encoder: connect your encoder to 2 digital channels and attach an interrupt to set a flag when one of the channels changes. In the loop, read the 2 channels only when the flag is set. The read operation of 2 channels takes about 1.3ms, maybe fast enough for your encoder?
Here is an example for such code, please let us know if that works;
#include <Indio.h>
#include <Wire.h>volatile bool flag = 0;
void set_flag() {
flag = 1;
}void setup() {
SerialUSB.begin(115200);
delay(2000);
SerialUSB.println("START");
Indio.digitalMode(7, INPUT);
Indio.digitalMode(8, INPUT);
attachInterrupt(8, set_flag, FALLING); // D8 attached to the interrupt of the expander
}void loop() {
if (flag == 1) {
unsigned long ts = micros();
bool ch7 = Indio.digitalRead(7);
bool ch8 = Indio.digitalRead(8);
unsigned long delta = micros() - ts;
SerialUSB.print(ch7);
SerialUSB.print(ch8);
SerialUSB.println();
flag = 0;
}
}
Your answer
Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!
Keep Informed
About This Forum
This community is for professionals and enthusiasts of our products and services.
Read GuidelinesQuestion tools
Stats
Asked: 8/20/21, 12:21 PM |
Seen: 1460 times |
Last updated: 8/24/21, 7:14 AM |
Thanks for your help. I'll get back to you as soon as I have time to test it.