cancel
Showing results for 
Search instead for 
Did you mean: 

My code is correct for push button ?

CG3
Associate III

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

https://www.st.com/resource/en/user_manual/um2179-stm32-nucleo144-boards-mb1312-stmicroelectronics.pdf

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

   }

1 ACCEPTED SOLUTION

Accepted Solutions
Karl Yamashita
Lead III

@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


_legacyfs_online_stmicro_images_0693W00000bhiQMQAY.pngThen enable interrupts in NVIC


_legacyfs_online_stmicro_images_0693W00000bhiQgQAI.png 

Then set the GPIO mode for interrupt mode on both Rising and falling edge


_legacyfs_online_stmicro_images_0693W00000bhiQlQAI.png 

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

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.

View solution in original post

6 REPLIES 6
MM..1
Chief III

Any GPIO require before use init code. And buttons on nucleo i mean push to 1 not 0.

What your code do ?

Karl Yamashita
Lead III

The user push button connected to PC13 is active high.

if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == GPIO_PIN_SET)

{

...

}

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.
Foued_KH
ST Employee

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.

Karl Yamashita
Lead III

@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


_legacyfs_online_stmicro_images_0693W00000bhiQMQAY.pngThen enable interrupts in NVIC


_legacyfs_online_stmicro_images_0693W00000bhiQgQAI.png 

Then set the GPIO mode for interrupt mode on both Rising and falling edge


_legacyfs_online_stmicro_images_0693W00000bhiQlQAI.png 

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

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.

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.

CG3
Associate III

@Community member​ @KHALSI_Foued​ @MM..1​ thank you for helping