cancel
Showing results for 
Search instead for 
Did you mean: 

Change the status of a variable when a button is pressed via interrupt routine.

mdiro
Associate III

I am implementing an application when I need to set high a status variable when a button is pressed. I would like to do so via the interrupt routine of the push button. So, in gpio.c I have

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
    if (GPIO_Pin == GPIO_PIN_0){
    PB1_isPressed = 1;
    }
}

In the main.c I would like to have a statement like:

if (PB1_isPressed ==1){

do something

}

Now my questions are the following:

  1. Is this a good practice?
  2. where do I declare PB1_isPressed? and how?

Sorry for the very basic questions and thank you all for the help in advance :)

10 REPLIES 10

Yes, I agree

the variable might be modified by the interrupt service routine so it must be declared as volatile

thank you for your comment

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.