Page 1 of 1

Interrupt FALLING not working as excepted

Posted: Sat Jan 16, 2021 11:58 pm
by leouz
Hello all,
I'm programming a stm32F103C6 with platformio
I'm being mad with this script.
i have setup 2 interrupt pins, one to rise the variable "start" value and one to decrease it.
then the a pwm pin will be set with the "start" value.

The problem rise when I change the logic from RAISING to FALLING.
because the Interrupt don't trigger only when the pin change state from HIGH to LOW , but also from LOW to HIGH
in fact the result are the same as CHANGE
anyone excepted the same behaviour?
here my code

Code: Select all

#include <Arduino.h>

#define PWM PA6   //T3
#define INDIETRO PB10  //T4
#define AVANTI PB9  //T2  sono stati invertiti
#define period 1000

int start = 0;
unsigned long tempo = 0;

void newstep (){
  start++;
}

void newstep_i (){
  start--;
}

void setup() {
 Serial.begin(9600);
 attachInterrupt(digitalPinToInterrupt(AVANTI),newstep,FALLING);
 attachInterrupt(digitalPinToInterrupt(INDIETRO),newstep_i,FALLING);
 pinMode(PWM,OUTPUT);
 digitalWrite(PWM, LOW);
 pinMode(AVANTI,INPUT_PULLUP);
 pinMode(INDIETRO,INPUT_PULLUP);


 Serial.println("Ready 1.1.4");
}

void loop() {
  if (start > 255 )start = 255;
    else if (start < 0) start = 0;
analogWrite(PWM, start);

if (millis()>= tempo + period){
  tempo += period;
  Serial.println(start);
}

}

Re: Interrupt FALLING not working as excepted

Posted: Sun Jan 17, 2021 8:57 am
by stevestrong
If you use mechanical switches /pushbuttons then the behavior is normal, becase they cause spuorious transitions low->high->low... when activating them.
You have to debounce those inputs.

Re: Interrupt FALLING not working as excepted

Posted: Sun Jan 17, 2021 10:24 am
by leouz
I'm not using a mechanical switches, I'm using a second board to generate the signal.
the fact is if i set the RISING logic it work as excepted.

Re: Interrupt FALLING not working as excepted

Posted: Fri Jan 22, 2021 12:12 pm
by leouz
No one have encountered problem with the FALLING logic?

Re: Interrupt FALLING not working as excepted

Posted: Fri Jan 22, 2021 1:48 pm
by fpiSTM
Hi,

I've tested this sketch on my BP:

Code: Select all

const byte ledPin = LED_BUILTIN;
const byte interruptPin = PA6;
volatile byte state = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, FALLING);

  // Instantiate HardwareTimer object. Thanks to 'new' instantiation, HardwareTimer is not destructed when setup() function is finished.
  HardwareTimer *MyTim = new HardwareTimer(TIM2);

  // Configure and start PWM
  MyTim->setPWM(1, PA0, 100, 50); // 100 Hertz, 50% dutycycle

}

void loop() {
  digitalWrite(ledPin, state);
}

void blink() {
  state = !state;
}
And there is no issue with FALLING, channel 0 is the PWM and 1 the LED:
screenshot.png
screenshot.png (66.78 KiB) Viewed 4098 times
Of course, I've tested RISING and CHANGE and result is as expected.


I think a potential issue in your sketch is that the start should be volatile.