Skip to main content
NASI
Associate III
May 31, 2022
Solved

Press and release button

Hello,

I need help to implement a function for some buttons, that every time a button is pressed a CAN message will be sent and when the button is released the CAN message will be sent again.

It has to send a CAN message for falling and rising edge (doesn't matter the time between falling and rising time), and I don't want to configure GPIO as GPIO_EXTI.

Thank you.

This topic has been closed for replies.
Peter BENSCH
Peter BENSCHBest answer
ST Technical Moderator
May 31, 2022

What I mean is something like the following code (GPIO-PinState can also be replaced by uint_32):

GPIO_PinState ButtonStateCurrent;
GPIO_PinState ButtonStateLast = GPIO_PIN_RESET;
 
while (1)
{
 ButtonStateCurrent = HAL_GPIO_ReadPin(GPIO_Port, GPIO_Pin);
 if (ButtonStateCurrent != ButtonStateLast)
 {
 Send_CAN_Msg();
 ButtonStateLast = ButtonStateCurrent;
 }
 // any delay
}

BTW: the code will be better readable if you use the button Code Snippet = </> below.

In order 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.

6 replies

Peter BENSCH
ST Technical Moderator
May 31, 2022

Well, if you do not want to configure GPIO as GPIO_EXTI, i.e. you do not want to work with interrupt, you have to work in polling mode:

  • declare pin status as a variable and set it to the start state (e.g. not pressed = 0),
  • query the pin status in a simple while loop (e.g. via HAL_GPIO_ReadPin) and compare it with the variable mentioned before,
  • if changed, store the status in the variable and send the CAN message

Does it answer your question?

Regards

/Peter

In order 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.
NASI
NASIAuthor
Associate III
May 31, 2022

Thank you @Peter BENSCH​ 

Would you please complete the following code based on your suggestion.

BT1 = HAL_GPIO_ReadPin(GPIO_Port, GPIO_Pin);

if(BT1 == 0 && ...)

{

//Button is pressed

Send CAN; //send CAN only once

}

if(BT1 == 1 && ...)

{

Button is released

Send CAN; //send CAN only once

}

Thank you

Peter BENSCH
Peter BENSCHBest answer
ST Technical Moderator
May 31, 2022

What I mean is something like the following code (GPIO-PinState can also be replaced by uint_32):

GPIO_PinState ButtonStateCurrent;
GPIO_PinState ButtonStateLast = GPIO_PIN_RESET;
 
while (1)
{
 ButtonStateCurrent = HAL_GPIO_ReadPin(GPIO_Port, GPIO_Pin);
 if (ButtonStateCurrent != ButtonStateLast)
 {
 Send_CAN_Msg();
 ButtonStateLast = ButtonStateCurrent;
 }
 // any delay
}

BTW: the code will be better readable if you use the button Code Snippet = </> below.

In order 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.
NASI
NASIAuthor
Associate III
May 31, 2022

Thank you so much @Peter BENSCH​ 

NASI
NASIAuthor
Associate III
May 31, 2022

Would you please look at this question and if you could answer it, @Peter BENSCH​ 

 https://community.st.com/s/question/0D53W00001aIZKRSA4/both-can-bus-interrupt

Thank you.

Peter BENSCH
ST Technical Moderator
May 31, 2022

If this question cannot be answered by anyone here in the community, you should perhaps make a personal enquiry in our Online Support so that you can be helped directly.

Regards

/Peter

In order 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.