Interrupt FALLING not working as excepted

Post here all questions related to STM32 core if you can't find a relevant section!
Post Reply
leouz
Posts: 3
Joined: Sat Jan 16, 2021 10:59 pm

Interrupt FALLING not working as excepted

Post 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);
}

}
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: Interrupt FALLING not working as excepted

Post 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.
leouz
Posts: 3
Joined: Sat Jan 16, 2021 10:59 pm

Re: Interrupt FALLING not working as excepted

Post 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.
leouz
Posts: 3
Joined: Sat Jan 16, 2021 10:59 pm

Re: Interrupt FALLING not working as excepted

Post by leouz »

No one have encountered problem with the FALLING logic?
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Interrupt FALLING not working as excepted

Post 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 3937 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.
Post Reply

Return to “General discussion”