msoboc4
msoboc4

Reputation: 93

uVision Led lights

This is the first time i am programming a mikrokontroler, i'm using uVison and have an stm32 to program on.,

I have two LED light on it on the pins: PIN_4 and PIN_5 with a tutorial i know how to make one blink (the code below) but i dont know how to make them bot blink with not the same delay. Like i want to make PIN_4 led be wit a delay of 100ms and PIN:5 led with a delay of 50ms. The code below is the code for one Led light.

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */

HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_4); //Toggle the state of pin PC9
  HAL_Delay(100); //delay 100ms     

  }
  /* USER CODE END 3 */

}

Upvotes: 0

Views: 274

Answers (1)

Gürtaç Kadem
Gürtaç Kadem

Reputation: 315

You have 2 options.

First, you can set a timer for counting milliseconds. You can generate a code from STMCubeMX for 50ms timer. Then, in timer callback function, you should set pins to high or low.

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    timer_counter++; //50ms

    HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5);

    if(timer_counter>=2)  //100ms
    {
        HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_4);
        timer_counter = 0;
    }
}

Second option is that delay in main.

/* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */
      HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5); //Toggle the state of pin PB5
      HAL_Delay(50); //delay 50ms     

      HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_4); //Toggle the state of pin PB4
      HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5); //Toggle the state of pin PB5
      HAL_Delay(50);  // delay 50ms
  }
  /* USER CODE END 3 */

}

Upvotes: 1

Related Questions