AnsweredAssumed Answered

What is correct to update PWM pulse width in STM32F0 HAL?

Question asked by Junaid PV on Apr 14, 2018
Latest reply on Apr 15, 2018 by waclawek.jan

Hi,

 

I am trying to learn PWM generation with STM32F030F3P6. Generated code using STM32CubeMX. Basic PWM generation worked fine.

 

Now I am trying to change PWM pulse width dynamically in while loop of main(). Please see my main function. I am trying to fade-in and fade-led. But LED remains turned-off.

 

int main(void)
{
  uint16_t pwm_value = 0;
  uint16_t step = 10;

  HAL_Init();
  SystemClock_Config();

  MX_GPIO_Init();
  MX_TIM14_Init();

  HAL_TIM_PWM_Start(&htim14, TIM_CHANNEL_1);

  while (1)
  {
    HAL_Delay(100);
       if(pwm_value = 0) {
         step = 50;
       }
       if(pwm_value >= 1000) {
         step = -50;
       }
       pwm_value += step;
       __HAL_TIM_SET_COMPARE(&htim14, TIM_CHANNEL_1, pwm_value);
  }
}

 

Here is full main.c file (most of comments are stripped out).

 

#include "main.h"
#include "stm32f0xx_hal.h"

TIM_HandleTypeDef htim14;

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM14_Init(void);
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);

int main(void)
{
  uint16_t pwm_value = 0;
  uint16_t step = 10;

  HAL_Init();
  SystemClock_Config();

  MX_GPIO_Init();
  MX_TIM14_Init();

  HAL_TIM_PWM_Start(&htim14, TIM_CHANNEL_1);

  while (1)
  {
    HAL_Delay(100);
       if(pwm_value = 0) {
         step = 50;
       }
       if(pwm_value >= 1000) {
         step = -50;
       }
       pwm_value += step;
       __HAL_TIM_SET_COMPARE(&htim14, TIM_CHANNEL_1, pwm_value);
  }
}

/**
  * @brief System Clock Configuration
  * @retval None
  */

void SystemClock_Config(void)
{

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;

    /**Initializes the CPU, AHB and APB busses clocks
    */

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Initializes the CPU, AHB and APB busses clocks
    */

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure the Systick interrupt time
    */

  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

    /**Configure the Systick
    */

  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

/* TIM14 init function */
static void MX_TIM14_Init(void)
{

  TIM_OC_InitTypeDef sConfigOC;

  htim14.Instance = TIM14;
  htim14.Init.Prescaler = 7;
  htim14.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim14.Init.Period = 1000;
  htim14.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim14.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim14) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  if (HAL_TIM_PWM_Init(&htim14) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 0;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_PWM_ConfigChannel(&htim14, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  HAL_TIM_MspPostInit(&htim14);

}

/** Configure pins as
        * Analog
        * Input
        * Output
        * EVENT_OUT
        * EXTI
*/

static void MX_GPIO_Init(void)
{

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOF_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();

}

void _Error_Handler(char *file, int line)
{
  while(1)
  {
  }
}

 

PWM generation woks good if I remove code lines within while loop and put any desired value for line:

 

sConfigOC.Pulse = 0;

 

As far as I think, I am not calling __HAL_TIM_SET_COMPARE() right way or something else is required there. Could you please help me to identify that?

Thanks in advance.

Outcomes