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 :)

1 ACCEPTED SOLUTION

Accepted Solutions

Hello again,

Thank you for your explanation!

So first of all, declare your variable (PB1_isPressed) which will be changed within the interrupt routine in Private variable section (View photo attached)

uint8_t PB1_isPressed=0;

0693W00000VOgqQQAT.pngNow in while(1) : implement your code in case PB1_isPressed is set.

Setting PB1_isPressed will be done in the Interrupt routine.

0693W00000VOgqkQAD.pngFinally, set your variable in the Interrupt routine

0693W00000VOgrEQAT.pngI hope that answers your question!

Thank you!

Sarra

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.

View solution in original post

10 REPLIES 10
Sarra.S
ST Employee

Hello @mdiro and welcome to the Community ��

You can start with this Wiki : on how to get started with EXTI.

you can also check a video on Mooc explaining how to toggle an LED when Button is pressed.

Sarra

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.

MM..1
Chief II

Hi your question isnt STM based. Its basic C lang standarts.

For interrupt you need global volatile variable.

But in real hw situation , you need handle false pulse (ESD), too sw need sometimes handle push button down moment or pull up, long press etc.

Next situation is interrupt speed required. ? ... Then your q

  1. no good practice
  2. on stm Interrupt detection for buttons is required only whe low power modes is used and button wake MCU. Here use empty irq routine and test after wake gpio state in code...
S.Ma
Principal

You could use EXTI PR bit directly if polling from main loop... otherwise, use a volatile variable, set by interrupt and cleared by main loop. In theory, you should self disable the interrupt in case someone inject few MHz square wave to hang/break/hack your code with interrupts ddos style. What is simple at first gets refined down the road...

Dear Sarra,

thank you for you answer. This is very clear and I can do so but my problem is a bit different. In this case you act directly in the Interrupt routine (you switch on an LED), in my case I need to pass a variable outside that routine. Would you suggest any other wiki page or video?

Thanks :)

mdiro
Associate III

Thank you very much for your answer. I see what you mean. Would you recommend to go fro the EXTI PR strategy or volatile variable?

Hello again,

Thank you for your explanation!

So first of all, declare your variable (PB1_isPressed) which will be changed within the interrupt routine in Private variable section (View photo attached)

uint8_t PB1_isPressed=0;

0693W00000VOgqQQAT.pngNow in while(1) : implement your code in case PB1_isPressed is set.

Setting PB1_isPressed will be done in the Interrupt routine.

0693W00000VOgqkQAD.pngFinally, set your variable in the Interrupt routine

0693W00000VOgrEQAT.pngI hope that answers your question!

Thank you!

Sarra

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.

S.Ma
Principal

EXTI PR bits are declared volatile I guess. No choice to make.

Loppi
Associate III

From personal preference I would say use a boolean type (TRUE, FALSE or in FreeRTOS pdTRUE, pdFALSE) for your flag. Everything else was already mentioned.

Quick edit: The volatile variable method has a higher compatibility and is easier to read.

hs2
Senior

For correct code you have to speciify volatile for a variable shared between an ISR (callback) and application/task code. Otherwise it might get optimized out. The compiler can't know that the variable might get changed by an ISR and might assume it's constant..