2023-04-06 06:32 PM
Thank you for helping and checking in advance:
LED on and Off working well, without if code
I am using
https://os.mbed.com/platforms/ST-Nucleo-L496ZG-P/#board-pinout
according
User push button connect to PC13
in while (1) loop I add this if HAL code,
set PC13 Pull-Up and Input
Am i miss any setting?
if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == 0)
{
// LED ON
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7|GPIO_PIN_14, GPIO_PIN_SET);
HAL_Delay(1500);
// LED OFF
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7|GPIO_PIN_14, GPIO_PIN_RESET);
HAL_Delay(1500);
}
Solved! Go to Solution.
2023-04-07 03:13 AM - edited 2023-11-20 08:46 AM
@CG3 It looks like you just want to blink the LED while the push button is pressed but doing it in a while loop is blocking, so you can't do any other tasks.
So instead use EXTI on PC13
Then enable interrupts in NVIC
Then set the GPIO mode for interrupt mode on both Rising and falling edge
Then follow this YouTube video below on how to debounce a push button and also blink an LED all without using blocking code. So even if you are pressing and holding the button, the LED can be blinking while you can do other tasks like parse UART messages, communicate with I2C devices, etc.
There are a handful of examples that I show even counting button presses. You can create unique tasks and a lot more if you have a vivid imagination.
Debounce a push button using a timercallback
2023-04-06 11:50 PM
Any GPIO require before use init code. And buttons on nucleo i mean push to 1 not 0.
What your code do ?
2023-04-07 01:40 AM
The user push button connected to PC13 is active high.
if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == GPIO_PIN_SET)
{
...
}
2023-04-07 02:25 AM
Hello @CG3 ,
You can try :
/* Wait for User push-button press before starting */
while (HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13) != GPIO_PIN_RESET)
{
}
/* Wait for User push-button release before starting */
while (HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13) != GPIO_PIN_SET)
{
}
// LED ON
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7|GPIO_PIN_14, GPIO_PIN_SET);
HAL_Delay(1500);
// LED OFF
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7|GPIO_PIN_14, GPIO_PIN_RESET);
HAL_Delay(1500);
Foued
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.
2023-04-07 03:13 AM - edited 2023-11-20 08:46 AM
@CG3 It looks like you just want to blink the LED while the push button is pressed but doing it in a while loop is blocking, so you can't do any other tasks.
So instead use EXTI on PC13
Then enable interrupts in NVIC
Then set the GPIO mode for interrupt mode on both Rising and falling edge
Then follow this YouTube video below on how to debounce a push button and also blink an LED all without using blocking code. So even if you are pressing and holding the button, the LED can be blinking while you can do other tasks like parse UART messages, communicate with I2C devices, etc.
There are a handful of examples that I show even counting button presses. You can create unique tasks and a lot more if you have a vivid imagination.
Debounce a push button using a timercallback
2023-04-09 05:59 PM
Great and thank you for helping
Yes I am really new and try to learn it step by step
I just understand
if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == 1)
will on/off after i push blue button and
if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == 0)
stop on/off only while i continue pushing it.
2023-04-09 06:01 PM
@Community member @KHALSI_Foued @MM..1 thank you for helping