2022-05-31 05:07 AM
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.
Solved! Go to Solution.
2022-05-31 07:07 AM
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.
2022-05-31 05:34 AM
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:
Does it answer your question?
Regards
/Peter
2022-05-31 06:38 AM
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
2022-05-31 07:07 AM
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.
2022-05-31 07:12 AM
Thank you so much @Peter BENSCH
2022-05-31 07:17 AM
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.
2022-05-31 08:15 AM
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