STM32 BluePill LED flasher acts strange using HAL_GPIO_TogglePin

871 views Asked by At

I have two simple LED flasher programs the flash at 1 sec intervals for my BluePills.One method uses HAL_GPIO_TogglePin to toggle the LED state and the other uses Set and Reset to toggle the LED state. The Set and Reset functions as expected but the HAL_GPIO_TogglePin definitely is working but it has a almost PWM effect part of the time. I have tried both methods on 2 different Bluepills with same results for both.

 while (1)
  {  
        if (__HAL_TIM_GET_COUNTER(&htim1) >= 32000)
        {
          HAL_GPIO_TogglePin(User_LED_GPIO_Port, User_LED_Pin);
        }

and

 while (1)
  {  
        if (__HAL_TIM_GET_COUNTER(&htim1) >= 32000)
        {
              HAL_GPIO_WritePin(User_LED_GPIO_Port, User_LED_Pin, GPIO_PIN_SET);
        }
        else {
              HAL_GPIO_WritePin(User_LED_GPIO_Port, User_LED_Pin, GPIO_PIN_RESET);
        }
1

There are 1 answers

0
Micheal Double On

Thanks Codo, you were 100% right. I modified it to

if ((__HAL_TIM_GET_COUNTER(&htim1) == 32000) || (__HAL_TIM_GET_COUNTER(&htim1)==0))
        {
          HAL_GPIO_TogglePin(User_LED_GPIO_Port, User_LED_Pin);
          timer_val = __HAL_TIM_GET_COUNTER(&htim1);
        }

Funny thing is I originally followed the Digikey example and it did the same thing.

  while (1)
  {
    // If enough time has passed (1 second), toggle LED and get new timestamp
    if (__HAL_TIM_GET_COUNTER(&htim16) - timer_val >= 10000)
    {
      HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
      timer_val = __HAL_TIM_GET_COUNTER(&htim16);
    }

oh well, for what i want, the Set Reset works better anyways. Thanks