Unable to access variables in ISR

Post here first, or if you can't find a relevant section!
Post Reply
IMESHPEIRIS
Posts: 2
Joined: Mon Nov 04, 2024 1:01 pm

Unable to access variables in ISR

Post by IMESHPEIRIS »

Hello,

I'm trying to access the variable inside the ISR and i want to initiate the PWM signal on D4 pin when the interrupt occurred. also duty cycle of the PWM is control by analog potentiometer as shown in the below code. i have try this code but it wouldn't work (no PWM output).

i have tried code one by one as 1st i have checked that Val1 is update when interrupt occurred and it updated as expected

Code: Select all

void loop() {
  if (val1 == 1){
    Serial.println(val1);
  }
}
void EXTI0_IRQHandler (){
    val1 = true;
  } 
then i have try to read analog value from potentiometer when interrupt occurred and it also worked.

Code: Select all

void loop() {
  if (val1 == 1){
    val = map(analogRead(A0), 0, 4096, 0, 100);
    Serial.println(val);
  }

}

void EXTI0_IRQHandler (){
    val1 = true;
  } 
after that i have try to write pwm signal to D4 pin when interrupt occurred and it also work.

Code: Select all

void loop() {
  if (val1 == 1){
    pulse->setPWM(channel, pin, 5, 40);
    }
}
void EXTI0_IRQHandler (){
    val1 = true;
  } 
but when i try to combine all together it wouldn't work. (please find the full code here.)

Code: Select all

int val =0;
#define InterruptPin 3
#define pin 4
volatile bool val1 = false ;

TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(pin), PinMap_PWM);
uint32_t channel = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(pin), PinMap_PWM));
HardwareTimer *pulse ;

void setup() {
pinMode(InterruptPin, INPUT_PULLDOWN);
pulse = new HardwareTimer(Instance);
attachInterrupt(InterruptPin, EXTI0_IRQHandler, RISING);
analogReadResolution(12);
}

void loop() {
  if (val1 == 1){
    val = map(analogRead(A0), 0, 4096, 0, 100);
    pulse->setPWM(channel, pin, 5, val);
    //Serial.println(val);
  }

}

void EXTI0_IRQHandler (){
    val1 = true;
  } // end of ISR
could you please help me to sort out this issue. also Seral monitor wouldn't work with full code.
fpiSTM
Posts: 1944
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 108
Location: Le Mans
Contact:

Re: Unable to access variables in ISR

Post by fpiSTM »

Hi @IMESHPEIRIS

About the Serial, it misses the "Serial.begin(9600);"

val1 is never reset to false ?

Which board you used ? Hard to help without check which pin you used.

I've made the test with a Nucleo F446RE with this sketch and it works as expected including the serial:

Code: Select all

int val = 0;
#define InterruptPin USER_BTN
#define pin LED_BUILTIN
volatile bool val1 = false;

TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(pin), PinMap_PWM);
uint32_t channel = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(pin), PinMap_PWM));
HardwareTimer *pulse;

void setup() {
  pinMode(InterruptPin, INPUT_PULLDOWN);
  pulse = new HardwareTimer(Instance);
  attachInterrupt(InterruptPin, EXTI0_IRQHandler, RISING);
  analogReadResolution(12);
  Serial.begin(9600);
}

void loop() {
  if (val1) {
    val = map(analogRead(A0), 0, 4096, 0, 100);
    pulse->setPWM(channel, pin, 5, val);
    Serial.println(val);
    val1 = false;
  }
}

void EXTI0_IRQHandler() {
  val1 = true;
}  // end of ISR
Output when I press the button 0 when A0 connected to GND and 99 when connected to 3.3V. 36 is when there is nothing connected to A0,
LED is off when 0, ON when 99 and blink when 36.

Code: Select all

14:33:17.417 -> 32
14:33:32.319 -> 0
14:33:35.545 -> 0
14:33:43.063 -> 99
14:33:43.192 -> 99
14:33:49.644 -> 36
IMESHPEIRIS
Posts: 2
Joined: Mon Nov 04, 2024 1:01 pm

Re: Unable to access variables in ISR

Post by IMESHPEIRIS »

Hi @fpiSTM,

sorry for the missing "Serial.begin(9600);". actually i removed it bcz of serial monitor not working at all.

Also i have test the your code in my STM32F303K8 board by simply changing the
#define InterruptPin USER_BTN
to
#define InterruptPin D3
. but it wouldn't worked too(serial monitor, PWM signal, analog read from potentiometer).

val1 need to be reset to false by another interrupt. but i didn't mentioned it on this code because i need to add it on next stage(I'm developing code step by step :).

Here i'm using STM32F303K8 development board.
push button connected to the D3 pin. push button will trigger the interrupt.
potentiometer Analog out connected to the A0 pin. other 2 pins of potentiometer connected to the 3.3 and GND.
LED connected to the D4 pin in my code. (i checked LED_BUILTIN is varying according to potentiometer analog out when i checked your code)
i have add the connection diagram for your reference.

thanks for your quick reply. please help me to solve this issue.
Attachments
Screenshot 2024-11-05 164848.png
Screenshot 2024-11-05 164848.png (56.83 KiB) Viewed 853 times
Post Reply

Return to “General discussion”