cancel
Showing results for 
Search instead for 
Did you mean: 

Press and release button

NASI
Senior

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.

1 ACCEPTED SOLUTION

Accepted Solutions

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.

View solution in original post

6 REPLIES 6
Peter BENSCH
ST Employee

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
Senior

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

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
Senior

Thank you so much @Peter BENSCH​ 

NASI
Senior

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.

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.