Docs

Micro-controllers, wireless transmission and database

Head

LED Fade PWM Timer with Nucleo F446RE using STM32CubeIDE

Prerequisites

This project assumes you have already installed STM32CubeIDE. You need to have previously done a basic blink sketch with Nucleo STM32F446RE using STM32CubeIDE. I have made a complete video from installing STM32CubeIDE to LED blink program. You can watch it by clicking this link. https://www.youtube.com/watch?v=oAwZ0cjlmN8

STM32CubeIDE Settings

Click Timer → Click TIM2 →

Clock Source set to Internal Clock

Channel1 set to PWM Generation CH1

Configuration → Parameter Settings →

Prescaler set to 840-1

Counter Period 1000

Click pin PA5 and select TIM2_CH1

Additional code on top of STM32CubeIDE generated code

  /* USER CODE BEGIN 2 */
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    int x;
    for(x=0; x<1000; x=x+1)
    {
      __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_1, x);
      HAL_Delay(1);
    }
    for(x=1000; x>0; x=x-1)
    {
      __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_1, x);
      HAL_Delay(1);
    }
    /* USER CODE END WHILE */