0
\$\begingroup\$

I have stm32vldiscovery project. I want to use external interrupt by PA0 (user button). When I press the button (PA0), then function EXTI0_IRQHandler must set ldOn equal 0. But ldOn always is 1. Debuger shows what function EXTI0_IRQHandler runned on. ldOn change value to 0, and again stay equal 1. Why? Have this code: main.c

#include "main.h"
...
uint8_t ldOn = 1;

int main(void)
{

  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();
  HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_SET);

  while (1)
  {
        if(ldOn == 0)
            HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_RESET);
  }
}

it.c

#include "main.h"
...
extern uint8_t ldOn;
void EXTI0_IRQHandler(void)
{
  ldOn = 0;
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
}

When I used stm32f4-disc1 board, this construction works perfectly...

\$\endgroup\$
5
  • 7
    \$\begingroup\$ Hint: Try the volatile qualifier \$\endgroup\$
    – doynax
    Commented Feb 23, 2019 at 12:20
  • \$\begingroup\$ @doynax didn't help.On stm32f4-disc1 board this code works perfectly. \$\endgroup\$
    – Noisy88
    Commented Feb 23, 2019 at 12:32
  • \$\begingroup\$ What are the compiler and linker flags? I suspect your two boards may have different defaults in the setup. \$\endgroup\$ Commented Feb 23, 2019 at 14:51
  • 1
    \$\begingroup\$ Changing a non-volatile variable in an interrupt is Undefined Behviour, so it can produce different results. Next: put the extren declaration in the header. \$\endgroup\$ Commented Feb 24, 2019 at 8:53
  • \$\begingroup\$ Problem was in iwdg. Controller did reset every some miliseconds... I made full erase and reset configuration bits. After that the code did work perfectly. Thanks for the help \$\endgroup\$
    – Noisy88
    Commented May 11, 2019 at 11:06

1 Answer 1

0
\$\begingroup\$

Either declare variable ldOn as volatile or Remove optimizations from compilation and check

It should work. Even after using volatile keyword/removing optimization from compilation if code is not working then you have to triple review your code and check whether somewhere else variable is being modified.

Check this link

\$\endgroup\$
1
  • \$\begingroup\$ Problem was in iwdg. Controller did reset every some miliseconds... I made full erase and reset configuration bits. After that the code did work perfectly. Thanks for the help. \$\endgroup\$
    – Noisy88
    Commented May 11, 2019 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.